本文目录一览:
head first java第24页的泳池迷宫,我做出来的和答案不一样
想了一个晚上才想出来,来简单答一下。
while(x<4)
空格不填
if(x<1)
填“a”," ",
x=x-1
if(x<1)
填"noise"
if(x==1)
填"annoys"
if(x>1)
填"an"," oyster"(这个词前面自带空格)
x=x+2
然后你们在补充一下格式就行啦!
JAVA机器人走迷宫的编程——急求!
深度优先的迷宫图算法。
N年前的老问题了。
10分不值得做。
java最新迷宫问题,英文原版求解答
import java.util.*;
import java.util.regex.*;
class MyPoint
{
public boolean visited=false;
public int parentRow=-1;
public int parentColumn=-1;
public int x;
public int y;
public MyPoint(){}
public MyPoint(int x,int y)
{
this.x=x;
this.y=y;
}
}
class Maze
{
String[][] maze;
final int WIDTH;
final int HEIGHT;
final int START_X=1;
final int START_Y=1;
final int END_X;
final int END_Y;
MyPoint[][] visited;
public Maze()
{
Scanner s=new Scanner(System.in);
WIDTH=s.nextInt();//输入迷宫宽度
END_Y=WIDTH-2;
HEIGHT=s.nextInt();//输入迷宫长度
END_X=HEIGHT-2;
maze=new String[HEIGHT][];
visited=new MyPoint[HEIGHT][];
s.nextLine();//清除输入的回车
for(int i=0;iHEIGHT;++i)//输入迷宫的每一行,每一行中的字符之间用空格隔开
{
String temp=s.nextLine();
maze[i]=temp.split("\\s+");
visited[i]=new MyPoint[WIDTH];
for(int j=0;jWIDTH;++j)
{
visited[i][j]=new MyPoint(i,j);
}
}
System.out.println("input finish.");
for(int i=0;iHEIGHT;++i)
{
System.out.println(Arrays.toString(maze[i]));
}
}
public boolean tomRun()
{
LinkedListMyPoint stack=new LinkedListMyPoint();
stack.push(visited[START_X][START_Y]);
visited[START_X][START_Y].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.x==END_Xt.y==END_Y)
{
result=true;
break;
}
if(visited[t.x-1][t.y].visited==false!maze[t.x-1][t.y].equals("W"))
{
stack.push(visited[t.x-1][t.y]);
visited[t.x-1][t.y].parentRow=t.x;
visited[t.x-1][t.y].parentColumn=t.y;
visited[t.x-1][t.y].visited=true;
}
if(visited[t.x+1][t.y].visited==false!maze[t.x+1][t.y].equals("W"))
{
stack.push(visited[t.x+1][t.y]);
visited[t.x+1][t.y].parentRow=t.x;
visited[t.x+1][t.y].parentColumn=t.y;
visited[t.x+1][t.y].visited=true;
}
if(visited[t.x][t.y-1].visited==false!maze[t.x][t.y-1].equals("W"))
{
stack.push(visited[t.x][t.y-1]);
visited[t.x][t.y-1].parentRow=t.x;
visited[t.x][t.y-1].parentColumn=t.y;
visited[t.x][t.y-1].visited=true;
}
if(visited[t.x][t.y+1].visited==false!maze[t.x][t.y+1].equals("W"))
{
stack.push(visited[t.x][t.y+1]);
visited[t.x][t.y+1].parentRow=t.x;
visited[t.x][t.y+1].parentColumn=t.y;
visited[t.x][t.y+1].visited=true;
}
}
return result;
}
public void show(int x,int y)
{
if(visited[x][y].parentRow==-1)
{
System.out.println("["+x+","+y+"]");
return;
}
show(visited[x][y].parentRow,visited[x][y].parentColumn);
System.out.println("-"+"["+x+","+y+"]");
}
public static void main(String[] args)
{
Maze m=new Maze();
if(m.tomRun())
{
System.out.println("逃生路径如下:");
m.show(m.END_X,m.END_Y);
}
else
System.out.println("无法走出迷宫!");
}
}