「java找迷宫路径」java走迷宫

博主:adminadmin 2023-01-17 10:06:08 342

本篇文章给大家谈谈java找迷宫路径,以及java走迷宫对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA迷宫问题,怎么输出所走路径的坐标

在Way函数的if(Maza[Loci][Locj]==0) 语句里面Maza[Loci][Locj]=2;前面加一句

System.out.println("The Maza route is ("+Loci+","+Locj+")");就打印出程序所走路径的坐标了.

求用java语言寻找走出迷宫路线的算法

首先给定一个初始坐标,然后构建一个容器保存坐标值,之后进行迭代,横坐标+1,或者纵坐标+1,这个顺寻自己定义(四个方向),经过的“路径”保存在那个容器中,如果遇到死角,以此往回迭代,在容器中将遇到死角的那个坐标删除。最后找到自己定义的那个迷宫出口坐标。

请帮忙用数据结构(java版)的知识解决这道迷宫问题的程序代码。

我这是用c写的。你可以看看,希望能帮助到你。

#include"stdlib.h"

#include"stdio.h"

#define N 50

#define M 50

int X;

int maze[N+2][M+2];

struct point{

int row,col,predecessor;

}queue[512];

int head=0,tail=0;

void shoudong_maze(int m,int n){

int i,j;

printf("\n\n");

printf("请按行输入迷宫,0表示通路,1表示障碍:\n\n");

for(i=0;im;i++)

for(j=0;jn;j++)

scanf("%d",maze[i][j]);

}

void zidong_maze(int m,int n){

int i,j;

printf("\n迷宫生成中……\n\n");

system("pause");

for(i=0;im;i++)

for(j=0;jn;j++)

maze[i][j]=rand()%2;

//由于rand()产生的随机数是从0到RAND_MAX

//RAND_MAX是定义在stdlib.h中的,其值至少为32767)

//要产生从X到Y的数,只需要这样写:k=rand()%(Y-X+1)+X;

}

void print_maze(int m,int n){

int i,j;

printf("\n迷宫生成结果如下:\n\n");

printf("迷宫入口\n");

printf("↓");

for(i=0;im;i++)

{printf("\n");

for(j=0;jn;j++)

{if(maze[i][j]==0) printf("□");

if(maze[i][j]==1) printf("■");}

}

printf("→迷宫出口\n");

}

void result_maze(int m,int n)

{ int i,j;

printf("迷宫通路(用☆表示)如下所示:\n\t");

for(i=0;im;i++)

{ printf("\n");

for(j=0;jn;j++)

{if(maze[i][j]==0||maze[i][j]==2) printf("□");

if(maze[i][j]==1) printf("■");

if(maze[i][j]==3) printf("☆");

}

}

}

void enqueue(struct point p)

{ queue[tail]=p;

tail++;

}

struct point dequeue()

{ head++;

return queue[head-1];

}

int is_empty()

{ return head==tail;

}

void visit(int row,int col,int maze[52][52])

{ struct point visit_point={row,col,head-1};

maze[row][col]=2;

enqueue(visit_point);

}

int mgpath(int maze[52][52],int m,int n)

{ X=1;

struct point p={0,0,-1};

if(maze[p.row][p.col]==1)

{ printf("\n===============================================\n");

printf("此迷宫无解\n\n");X=0;return 0;}

maze[p.row][p.col]=2;

enqueue(p);

while(!is_empty())

{p=dequeue();

if((p.row==m-1)(p.col==n-1)) break;

if((p.col+1n)(maze[p.row][p.col+1]==0)) visit(p.row,p.col+1,maze);

if((p.row+1m)(maze[p.row+1][p.col]==0)) visit(p.row+1,p.col,maze);

if((p.col-1=0)(maze[p.row][p.col-1]==0)) visit(p.row,p.col-1,maze);

if((p.row-1=0)(maze[p.row-1][p.col]==0)) visit(p.row-1,p.col,maze);

}

if(p.row==m-1p.col==n-1)

{printf("\n==================================================================\n");

printf("迷宫路径为:\n");

printf("(%d,%d)\n",p.row,p.col);

maze[p.row][p.col]=3;

while(p.predecessor!=-1)

{p=queue[p.predecessor];

printf("(%d,%d)\n",p.row,p.col);

maze[p.row][p.col]=3;

}

}

else {printf("\n=============================================================\n");

printf("此迷宫无解!\n\n");X=0;}

return 0;

}

int main()

{int i,m,n,cycle=0;

while(cycle!=(-1))

{

printf("********************************************************************************\n");

printf(" ☆欢迎进入迷宫求解系统☆\n");

printf(" 设计者:尹旭 林静波(信息2班)\n");

printf("********************************************************************************\n");

printf(" 手动生成迷宫 请按:1\n");

printf(" 自动生成迷宫 请按:2\n");

printf(" 退出 请按:3\n\n");

printf("********************************************************************************\n");

printf("\n");

printf("请选择你的操作:\n");

scanf("%d",i);

switch(i)

{case 1:printf("\n请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

scanf("%d",n);

while((m=0||m50)||(n=0||n50))

{ printf("\n抱歉,你输入的行列数超出预设范围(0-50,0-50),请重新输入:\n\n");

printf("请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

scanf("%d",n);

}

shoudong_maze(m,n);

print_maze(m,n);

mgpath(maze,m,n);

if(X!=0)

result_maze(m,n);

printf("\n\nPress Enter Contiue!\n");

getchar();

while(getchar()!='\n');

break;

case 2:printf("\n请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

scanf("%d",n);

while((m=0||m50)||(n=0||n50))

{printf("\n抱歉,你输入的行列数超出预设范围(0-50,0-50),请重新输入:\n\n");

printf("请输入行数:");

scanf("%d",m);

printf("\n");

printf("请输入列数:");

scanf("%d",n);

}

zidong_maze(m,n);

print_maze(m,n);

mgpath(maze,m,n);

if(X!=0)

result_maze(m,n);

printf("\n\nPress Enter Contiue!\n");getchar();while(getchar()!='\n');break;

case 3:cycle=(-1);

break;

default:printf("\n");

printf("你的输入有误!\n");

printf("\nPress Enter Contiue!\n");

getchar();

while(getchar()!='\n');break;

}

}

}

java迷宫路径总条数问题

int[][] data是你的迷宫数组,返回值是路径总条数,不需要递归

public int findWayCount(int[][] data) {

int[][] way = new int[data.length][];

for (int m = 0; m data.length; m++) {

way[m] = new int[data[m].length];

for (int n = 0; n data[m].length; n++) {

if (data[m][n] == 0) {

way[m][n] = 0;

} else if (m == 0 n == 0) {

way[m][n] = data[0][0];

} else if (m == 0) {

way[m][n] = way[m][n - 1];

} else if (n == 0) {

way[m][n] = way[m - 1][n];

} else {

way[m][n] = way[m][n - 1] + way[m - 1][n];

}

}

}

急需一java高手帮忙写一迷宫程序

给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。

你看一下吧。

----------------------------------------

package stackpackage;

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Maze {

// 上

private static Point directionTop = new Point(-1, 0);

// 下

private static Point directionBottom = new Point(1, 0);

// 左

private static Point directionLeft = new Point(0, -1);

// 右

private static Point directionRight = new Point(0, 1);

private static Point[] directions = { directionTop, directionRight,

directionBottom, directionLeft };

private static boolean isStop = false;

private static int row = 0;

private static int col = 0;

private static Point startPoint = new Point();

public static void main(String[] args) throws Exception {

FileReader fr = new FileReader("data.txt");

BufferedReader br = new BufferedReader(fr);

int rowIndex = 1;

int[][] maze = null;

while (br.ready()) {

String line = br.readLine();

Scanner sc = new Scanner(line);

if (rowIndex == 1) {

row = sc.nextInt();

col = sc.nextInt();

maze = new int[row][col];

} else {

if (rowIndex row + 2) {

for (int i = 0; i col; i++) {

maze[rowIndex - 2][i] = sc.nextInt();

}

} else {

startPoint.x = sc.nextInt();

startPoint.y = sc.nextInt();

}

}

rowIndex++;

}

ListPoint route = new ArrayListPoint();

route.add(startPoint);

findNext(startPoint);

puzzle(maze, startPoint, route);

System.out.println(route);

}

private static void puzzle(int[][] maze, Point p, ListPoint route) {

if (isStop) {

return;

}

Point[] nextDirections = p.nextDirections;

for (int i = 0; i nextDirections.length; i++) {

if (isStop) {

return;

}

Point direction = nextDirections[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (newP.isEffective() maze[newP.x][newP.y] == 0

!route.contains(newP)) {

newP.before = p;

findNext(newP);

route.add(newP);

if (isExit(newP)) {

isStop = true;

break;

}

puzzle(maze, newP, route);

}

}

if (isStop) {

return;

}

route.remove(route.size() - 1);

}

private static void findNext(Point p) {

int index = 0;

Point[] nextDirections = new Point[3];

for (int i = 0; i nextDirections.length; i++) {

nextDirections[i] = new Point(0, 0);

}

for (int i = 0; i directions.length; i++) {

Point direction = directions[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (newP.isEffective() !newP.equals(p.before) newP.x row

newP.y col) {

nextDirections[index++] = direction;

}

}

p.nextDirections = nextDirections;

}

private static boolean isExit(Point p) {

if (startPoint.equals(p)) {

return false;

}

for (int i = 0; i directions.length; i++) {

Point direction = directions[i];

Point newP = new Point(p.x + direction.x, p.y + direction.y);

if (!newP.equals(p.before)

(newP.x = row || newP.y = col || newP.x 0 || newP.y 0)) {

return true;

}

}

return false;

}

}

class Point {

int x = 0;

int y = 0;

Point[] nextDirections = null;

Point before = null;

public Point() {

}

public Point(int x, int y) {

this.x = x;

this.y = y;

}

public String toString() {

return "" + x + "," + y + "";

}

public boolean isEffective() {

return x = 0 y = 0;

}

public boolean equals(Object obj) {

return equals((Point) obj);

}

public boolean equals(Point p) {

if (p == null) {

return false;

}

return this.x == p.x this.y == p.y;

}

}

java找迷宫路径的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java走迷宫、java找迷宫路径的信息别忘了在本站进行查找喔。