package qinglin.learn.arithmetic;
public class MousedMaze
{
//2 代表的是墙壁,1代表的是路径,0代表的是可以行走的方格
int maze[][] =
{
{2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 0, 2, 0, 2, 0, 2},
{2, 0, 0, 2, 0, 2, 2},
{2, 2, 0, 2, 0, 2, 2},
{2, 0, 0, 0, 0, 0, 2},
{2, 2, 2, 2, 2, 2, 2}
};
int startI = 1, startJ = 1; // 入口
int endI = 5, endJ = 5; // 出口
int success = 0;
final int COLUMN=7;
final int ROW=7;
private void showMaze()
{
for(int i=0;i<COLUMN;i++)
{
for(int j=0;j<ROW;j++)
{
if(maze[i][j]==2)
System.out.print("●"+" ");
else if(maze[i][j]==0)
System.out.print("○"+" ");
else if(maze[i][j]==1)
System.out.print("◇"+" ");
}
System.out.println();
System.out.println();
}
}
private int visit(int i,int j)
{
maze[i][j]=1;
if(i==endI&&j==endJ)
{
success=1;
return success;
}
if(success==0&&maze[i][j+1]==0)
visit(i,j+1);
if(success==0&&maze[i][j-1]==0)
visit(i,j-1);
if(success==0&&maze[i-1][j]==0)
visit(i-1,j);
if(success==0&&maze[i+1][j]==0)
visit(i+1,j);
if(success==0)
maze[i][j]=0;
return success;
}
public static void main(String[] args)
{
MousedMaze mouse=new MousedMaze();
mouse.showMaze();
System.out.println();
System.out.println();
if(mouse.visit(mouse.startI, mouse.startJ)==1)
mouse.showMaze();
}
}
老鼠过迷宫 单解
最新推荐文章于 2020-12-21 11:19:46 发布