java迷宫代码(迷宫编程代码)
admin 发布:2022-12-19 22:43 200
今天给各位分享java迷宫代码的知识,其中也会对迷宫编程代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、请帮忙用数据结构(java版)的知识解决这道迷宫问题的程序代码。
- 2、Java迷宫算法问题(用栈实现)有算法简述
- 3、求走迷宫问题的算法,要求用Java写的?
- 4、java怎么生成迷宫地图
- 5、急需一java高手帮忙写一迷宫程序
- 6、java老鼠迷宫代码难吗
请帮忙用数据结构(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迷宫算法问题(用栈实现)有算法简述
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("Maze2tong.txt"));
int row = 0;
char[][] Mazemap = new char[12][58];
while (input.hasNext()) {
String line = input.nextLine();
for (int column = 0; column = line.length() - 1; column++) {
char c = line.charAt(column);
Mazemap[row][column] = c;
}
row++;
}
for (int i = 0; i 12; i++) {
for (int j = 0; j 58; j++) {
System.out.print(Mazemap[i][j]);
}
System.out.print("\n");
}
LinkedListTwoTupleInteger, Integer trace = new LinkedListTwoTupleInteger, Integer();
System.out.println(maze(Mazemap, trace));
System.out.println(trace);
}
public static boolean maze(char[][] maze,
ListTwoTupleInteger, Integer trace) {
LinkedListTwoTupleInteger, Integer path = new LinkedListTwoTupleInteger, Integer();
HashSetTwoTupleInteger, Integer traverse = new HashSetTwoTupleInteger, Integer();
for (int i = 0; i maze.length; i++) {
for (int j = 0; j maze[i].length; j++) {
if (maze[i][j] == 'S') {
path.add(new TwoTupleInteger, Integer(i, j));
}
}
}
while (!path.isEmpty()) {
TwoTupleInteger, Integer temp = path.pop();
if (traverse.contains(temp)) {
continue;
} else if (maze[temp.first][temp.second] == 'F') {
trace.add(temp);
return true;
} else if (!traverse.contains(temp)) {
if (temp.second + 1 maze[temp.first].length
maze[temp.first][temp.second + 1] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first,
temp.second + 1));
if (temp.second - 1 0
maze[temp.first][temp.second - 1] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first,
temp.second - 1));
if (temp.first + 1 maze.length
maze[temp.first + 1][temp.second] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first + 1,
temp.second));
if (temp.first - 1 0
maze[temp.first - 1][temp.second] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first - 1,
temp.second));
traverse.add(temp);
trace.add(temp);
}
}
trace.clear();
return false;
}
}
class TwoTupleA, B {
public final A first;
public final B second;
public TwoTuple(A a, B b) {
first = a;
second = b;
}
@Override
public int hashCode() {
return first.hashCode() + second.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TwoTuple)) {
}
return obj instanceof TwoTuple first.equals(((TwoTuple) obj).first)
second.equals(((TwoTuple) obj).second);
}
public String toString() {
return "(" + first + ", " + second + ")";
}
} // /:-
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
class MyPoint
{
public boolean visited=false;
public int parentRow=-1;
public int parentColumn=-1;
public final char content;
public int x;
public int y;
public MyPoint(char c,int x,int y)
{
this.content = c;
this.x = x;
this.y = y;
}
}
public class Maze
{
public static MyPoint[][] getMazeArray(){
Scanner input = null;
MyPoint[][] mazemap = new MyPoint[12][58];
try {
input = new Scanner(new File("Maze2tong.txt"));
int row = 0;
while (input.hasNext()) {
String line = input.nextLine();
for (int column = 0; column = line.length() - 1; column++) {
char c = line.charAt(column);
MyPoint point = new MyPoint(c,row,column);
mazemap[row][column] = point;
}
row++;
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return mazemap;
}
public static boolean tomRun(MyPoint[][] maze,MyPoint end)
{
int x = maze.length;
int y = maze[0].length;
LinkedListMyPoint stack=new LinkedListMyPoint();
for (int i = 0; i maze.length; i++) {
for (int j = 0; j maze[i].length; j++) {
if (maze[i][j].content == 'S') {
stack.push(maze[i][j]);
maze[i][j].visited=true;
}
}
}
boolean result=false;
while(!stack.isEmpty())
{
MyPoint t=stack.pop();
//System.out.println("pop point:"+t.x+" "+t.y+" value:"+maze[t.x][t.y]);
if(t.content == 'F')
{
result=true;
end.x = t.x;
end.y = t.y;
break;
}
if(t.x-1 0 maze[t.x-1][t.y].visited==false maze[t.x-1][t.y].content != 'W')
{
stack.push(maze[t.x-1][t.y]);
maze[t.x-1][t.y].parentRow=t.x;
maze[t.x-1][t.y].parentColumn=t.y;
maze[t.x-1][t.y].visited=true;
}
if(t.x + 1 x maze[t.x+1][t.y].visited==false maze[t.x+1][t.y].content != 'W')
{
stack.push(maze[t.x+1][t.y]);
maze[t.x+1][t.y].parentRow=t.x;
maze[t.x+1][t.y].parentColumn=t.y;
maze[t.x+1][t.y].visited=true;
}
if(t.y - 1 0 maze[t.x][t.y - 1].visited==false maze[t.x][t.y-1].content != 'W')
{
stack.push(maze[t.x][t.y-1]);
maze[t.x][t.y-1].parentRow=t.x;
maze[t.x][t.y-1].parentColumn=t.y;
maze[t.x][t.y-1].visited=true;
}
if( t.y + 1 y maze[t.x][t.y + 1].visited==false maze[t.x][t.y+1].content != 'W')
{
stack.push(maze[t.x][t.y+1]);
maze[t.x][t.y+1].parentRow=t.x;
maze[t.x][t.y+1].parentColumn=t.y;
maze[t.x][t.y+1].visited=true;
}
}
return result;
}
public static void show(int x,int y,MyPoint[][] visited)
{
if(visited[x][y].parentRow==-1)
{
System.out.println("["+x+","+y+"]");
return;
}
show(visited[x][y].parentRow,visited[x][y].parentColumn,visited);
System.out.println("-"+"["+x+","+y+"]");
}
public static void main(String[] args)
{
MyPoint[][] maze = getMazeArray();
MyPoint point = new MyPoint('c',1,1);
if(tomRun(maze,point))
{
System.out.println("逃生路径如下:");
show(point.x,point.y,maze);
}
else
System.out.println("无法走出迷宫!");
}
}
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOOOOOOOOOOOOOOWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
求走迷宫问题的算法,要求用Java写的?
public class Maze { private int[][] maze = null;
private int[] xx = { 1, 0, -1, 0 };
private int[] yy = { 0, 1, 0, -1 };
private Queue queue = null; public Maze(int[][] maze) {
this.maze = maze;
queue = new Queue(maze.length * maze.length);
} public void go() {
Point outPt = new Point(maze.length - 1, maze[0].length - 1);
Point curPt = new Point(0, 0);
Node curNode = new Node(curPt, null);
maze[curPt.x][curPt.y] = 2;
queue.entryQ(curNode); while (!queue.isEmpty()) {
curNode = queue.outQ();
for (int i = 0; i xx.length; ++i) {
Point nextPt = new Point();
nextPt.x = (curNode.point).x + xx[i];
nextPt.y = (curNode.point).y + yy[i];
if (check(nextPt)) {
Node nextNode = new Node(nextPt, curNode);
queue.entryQ(nextNode);
maze[nextPt.x][nextPt.y] = 2;
if (nextPt.equals(outPt)) {
java.util.StackNode stack = new java.util.StackNode();
stack.push(nextNode);
while ((curNode = nextNode.previous) != null) {
nextNode = curNode;
stack.push(curNode);
}
System.out.println("A Path is:");
while (!stack.isEmpty()) {
curNode = stack.pop();
System.out.println(curNode.point);
}
return;
}
}
}
}
System.out.println("Non solution!");
} private boolean check(Point p) {
if (p.x 0 || p.x = maze.length || p.y 0 || p.y = maze[0].length) {
return false;
}
if (maze[p.x][p.y] != 0) {
return false;
}
return true;
} public static void main(String[] args) {
int[][] maze = {
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
};
new Maze(maze).go();
} private class Queue { Node[] array = null;
int size = 0;
int len = 0;
int head = 0;
int tail = 0; public Queue(int n) {
array = new Node[n + 1];
size = n + 1;
} public boolean entryQ(Node node) {
if (isFull()) {
return false;
}
tail = (tail + 1) % size;
array[tail] = node;
len++;
return true;
} public Node outQ() {
if (isEmpty()) {
return null;
}
head = (head + 1) % size;
len--;
return array[head];
} public boolean isEmpty() {
return (len == 0 || head == tail) ? true : false;
} public boolean isFull() {
return ((tail + 1) % size == head) ? true : false;
}
} private class Node { Point point = null;
Node previous = null; public Node() {
this(null,null);
} public Node(Point point, Node node) {
this.point = point;
this.previous = node;
}
} private class Point { int x = 0;
int y = 0; public Point() {
this(0, 0);
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public boolean equals(Point p) {
return (x == p.x) (y == p.y);
} @Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}
java怎么生成迷宫地图
//作者:zhongZw
package cn.zhongZw.model;
import java.util.ArrayList;
import java.util.Random;
public class MazeModel {
private int width = 0;
private int height = 0;
private Random rnd = new Random();
public MazeModel() {
this.width = 50; //迷宫宽度
this.height = 50; //迷宫高度
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public MazeModel(int width, int height) {
super();
this.width = width;
this.height = height;
}
public ArrayList MazePoint getMaze() {
ArrayList MazePoint maze = new ArrayList MazePoint ();
for (int h = 0; h height; h++) {
for (int w = 0; w width; w++) {
MazePoint point = new MazePoint(w, h);
maze.add(point);
}
}
return CreateMaze(maze);
}
private ArrayList MazePoint CreateMaze(ArrayList MazePoint maze) {
int top = 0;
int x = 0;
int y = 0;
ArrayList MazePoint team = new ArrayList MazePoint ();
team.add(maze.get(x + y * width));
while (top = 0) {
int[] val = new int[] {
-1, -1, -1, -1
};
int times = 0;
boolean flag = false;
MazePoint pt = (MazePoint) team.get(top);
x = pt.getX();
y = pt.getY();
pt.visted = true;
ro1: while (times 4) {
int dir = rnd.nextInt(4);
if (val[dir] == dir)
continue;
else
val[dir] = dir;
switch (dir) {
case 0: // 左边
if ((x - 1) = 0 maze.get(x - 1 + y * width).visted == false) {
maze.get(x + y * width).setLeft();
maze.get(x - 1 + y * width).setRight();
team.add(maze.get(x - 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 1: // 右边
if ((x + 1) width maze.get(x + 1 + y * width).visted == false) {
maze.get(x + y * width).setRight();
maze.get(x + 1 + y * width).setLeft();
team.add(maze.get(x + 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 2: // 上边
if ((y - 1) = 0 maze.get(x + (y - 1) * width).visted == false) {
maze.get(x + y * width).setUp();
maze.get(x + (y - 1) * width).setDown();
team.add(maze.get(x + (y - 1) * width));
top++;
flag = true;
break ro1;
}
break;
case 3: // 下边
if ((y + 1) height maze.get(x + (y + 1) * width).visted == false) {
maze.get(x + y * width).setDown();
maze.get(x + (y + 1) * width).setUp();
team.add(maze.get(x + (y + 1) * width));
top++;
flag = true;
break ro1;
}
break;
}
times += 1;
}
if (!flag) {
team.remove(top);
top -= 1;
}
}
return maze;
}
}
迷宫
[java] view plain copy
//作者:zhongZw
//邮箱:zhong317@126.com
package cn.zhongZw.model;
import java.util.*;
import java.lang.*;
public class MazePoint {
private int left = 0;
private int right = 0;
private int up = 0;
private int down = 0;
private int x;
private int y;
public boolean visted;
public MazePoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getLeft() {
return left;
}
public void setLeft() {
this.left = 1;
}
public int getRight() {
return right;
}
public void setRight() {
this.right = 1;
}
public int getUp() {
return up;
}
public void setUp() {
this.up = 1;
}
public int getDown() {
return down;
}
public void setDown() {
this.down = 1;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
急需一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老鼠迷宫代码难吗
非常难。思路:
1、设老鼠的行进路线都是优先选择下-右-上-左。
2、设老鼠很聪明,走过的路线走撒泡尿,表示鼠大爷到此一游,我们可以把数组的值改为3,表示走过,但走不通。
3、这是一个int[8][8]的二位数组,那么开始位置下标是1,1,结束位置是6,6。行和列分别用、j表示。
4、实际路线我们可以设置2表示,我们可以使用递归,让老鼠不断测试路线。
5、最后打印数组,看老鼠的实际路线。
关于java迷宫代码和迷宫编程代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-09网页代码,网页代码快捷键
- 05-06单页网站的代码(完整的网页代码)[20240506更新]
- 05-06个人主页图片代码(个人主页图片代码怎么弄)[20240506更新]
- 05-06提取微信名片代码(微信名片信息提取)[20240506更新]
- 05-06php后台权限管理代码(php管理员权限)[20240506更新]
- 05-06付费观看代码php(付费观看代码)[20240506更新]
- 05-06在线html执行代码(html怎么运行)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06代码源软件库(程序代码库)[20240506更新]
- 05-06点击弹出密码代码(点击弹出密码代码错误)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接