dfs解,反正路只有一条,不用计算最短的
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
int n = sc.nextInt();
int m = sc.nextInt();
int[][] a = new int[n][m];
for (int i = 0; i < n;i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
}
}
List<String> path = new ArrayList<String>();
dfs(0,0, a, path);
for (int i = path.size() -1 ; i >= 0; i--) {
System.out.println(path.get(i));
}
}
}
private static boolean dfs(int i, int j, int[][] a, List<String> path) {
if (i< 0 || i > a.length-1 || j < 0 || j > a[0].length-1 || a[i][j] == 1) {
return false;
}
if (i == a.length-1 && j == a[0].length-1) {
path.add("(" + i +"," + j + ")");
return true;
}
int temp = a[i][j];
a[i][j] = 1;
boolean res = dfs(i + 1, j, a,path) || dfs(i - 1, j, a,path) || dfs(i, j + 1, a,path) || dfs(i, j-1, a,path);
if (res) {
path.add("(" + i +"," + j + ")");
}
a[i][j] = temp;
return res;
}
}