import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main
{
public static int move[][]= {{0,1},{0,-1},{1,0},{-1,0}};
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int arr[][]=new int[5][5];
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr.length; j++)
{
arr[i][j]=sc.nextInt();
}
}
Queue<deal> ok=new LinkedList<deal>();
ok.add(new deal(0, 0, 1,"(0, 0)|"));
while(!ok.isEmpty()) {
deal yes=ok.poll();
if (yes.x==4 && yes.y==4)
{
String gg[]=yes.ok.split("\\|");
for (int i = 0; i < gg.length; i++)
{
System.out.println(gg[i]);
}
break;
}
for (int i = 0; i < 4; i++)
{
int o=yes.x+move[i][0];int k =yes.y+move[i][1];
if (o>=0 && k>=0 && o<5 && k<5 && arr[o][k]!=1)
{
StringBuilder gg=new StringBuilder();
gg.append(yes.ok);
gg.append("("+o+", "+k+")"+"|");
ok.add(new deal(o, k, yes.step+1, gg.toString()));
}
}
}
}
}
class deal{
int x,y,step;String ok;
public deal(int x,int y ,int step,String ok) {
this.x=x;this.y=y; this.step=step;this.ok=ok;
}
}
K - 迷宫问题 POJ - 3984 JAVA
最新推荐文章于 2020-12-13 15:47:10 发布