滴滴青蛙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);
				
	}

} 

#滴滴#
全部评论
当时就不知怎么记录路径,大神竟然把路径放在参数里,真是厉害!佩服!
点赞 回复 分享
发布于 2016-09-18 22:08

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
gcniz:一天写两千行你闹呢
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务