滴滴青蛙AC
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int P = in.nextInt();
int[][] grid = new int[n][m];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
grid[i][j] = in.nextInt();
}
}
System.out.println(findPath(grid,P));
in.close();
}
private static String findPath(int[][] grid, int p) {
int row = grid.length;
int col = grid[0].length;
int[][] num = new int[row][col];
for(int i = 0 ; i < row ; i++){
for(int j = 0 ; j < col ; j++){
num[i][j] = Integer.MIN_VALUE;
}
}
String result = "[0,0]";
String[][] str = new String[row][col];
helper(grid,num,0,0,p,result,str);
if(num[0][col-1] >= 0){
return str[0][col-1];
}
return "Can not escape!";
}
private static void helper(int[][] grid,int[][] num, int row, int col, int p,String result,String[][] str) {
if(row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] != 1 || p < 0 ) return;
if(p < num[row][col]){
return;
}else if(p >= num[row][col]){
num[row][col] = p;
str[row][col] = result;
}
helper(grid,num,row,col+1,p-1,result+",["+(row)+","+(col+1)+"]",str);
helper(grid,num,row-1,col,p-3,result+",["+(row-1)+","+(col)+"]",str);
helper(grid,num,row,col-1,p-1,result+",["+row+","+(col-1)+"]",str);
helper(grid,num,row+1,col,p,result+",["+(row+1)+","+(col)+"]",str);
}
}
#滴滴#
查看20道真题和解析