题目描述
有一个n*m格的迷宫(表示有n行、m列),其中有可走的也有不可走的,
如果用1表示可以走,0表示不可以走,
文件读入这n*m个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。
现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。
如果一条路都不可行,则输出相应信息(用-l表示无路)。
请统一用 左上右下的顺序拓展,也就是 (0,-1),(-1,0),(0,1),(1,0)
输入
第一行是两个数n,m( 1 < n , m < 15 ),
接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。
输出
所有可行的路径,描述一个点时用(x,y)的形式,除开始点外,
其他的都要用“->”表示方向。
如果没有一条可行的路则输出-1。
样例输入
5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6
样例输出
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
import java.util.Scanner;
public class Main {
static int[][] map;
static int xSize,ySize;
static Node[] way;
static int count;
static int index;
static int end1,end2;
static int begin1,begin2;
static class Node{
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
public Main(int[][] map) {
this.map = map;
this.xSize = map.length;
this.ySize = map[0].length;
count = 0;
way = new Node[1000];
index = 0;
}
public void getDestination(int x, int y) {
map[x][y] = 2;
}
public void dfs(int x, int y) {
if(map[x][y] == 2) {
count++;
for(int i = 0; i < index; i++) {
System.out.print("("+way[i].x+","+way[i].y+")->");
}
System.out.print("("+end1+","+end2+")");
System.out.println();
return ;
}
if(map[x][y] == 0)
return;
map[x][y] = 0;
int num1 = x + 1;
int num2 = y + 1;
way[index] = new Node(num1,num2);
index++;
if(x > 0)
dfs(x-1, y);
if(y > 0)
dfs(x, y-1);
if(x < xSize-1)
dfs(x+1, y);
if(y < ySize-1 )
dfs(x, y+1);
map[x][y] = 1;
index--;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int map[][] = new int[m][n];
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
map[i][j] = in.nextInt();
begin1 = in.nextInt();
begin2 = in.nextInt();
end1 = in.nextInt();
end2 = in.nextInt();
Main main = new Main(map);
main.getDestination(end1-1, end2-1);
main.dfs(begin1-1, begin2-1);
if(count == 0)
System.out.println("-1");
}
}
import java.util.Scanner;
public class find2020 {
static int n,m,start_x,start_y,end_x,end_y;
static int[][] map ;
static int count;
static node[] way;
static int index;
static public class node{
int x,y;
public node(int x,int y) {
this.x = x;
this.y = y;
}
}
public static void dfs(int x,int y) {
if(map[x][y] == 2) {
count++;
for(int i = 0; i < index;i++) {
System.out.print("("+way[i].x+","+way[i].y+")->");
}
System.out.print("("+end_x+","+end_y+")");
System.out.println();
return;
}
if(map[x][y] == 0)
return;
map[x][y] = 0;
int dex_x = x + 1;
int dex_y = y + 1;
way[index] = new node(dex_x, dex_y);
index++;
if(y > 0)
dfs(x, y-1);
if(x > 0)
dfs(x-1, y);
if(y < m-1)
dfs(x, y+1);
if(x < n-1 )
dfs(x+1, y);
map[x][y] = 1;
index--;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
map = new int[n][m];
way = new node[99];
for(int i = 0; i < n; i++)
for(int j = 0; j < m;j ++)
map[i][j] = in.nextInt();
start_x = in.nextInt();
start_y = in.nextInt();
end_x = in.nextInt();
end_y = in.nextInt();
index = 0;
map[end_x-1][end_y-1] = 2;
dfs(start_x-1, start_y-1);
}
}