啊。滴滴本地通过最后时刻在线不通过。
颜后最后一题没做。啊,牛客网环境比其他的强。
import java.util.*;
public class Main { public static void main(String[] args) { Main main = new Main(); Scanner sc = new Scanner(System.in); while(sc.hasNext()){ Stack<Integer> stack = new Stack<Integer>(); int n = sc.nextInt(); int m = sc.nextInt(); int p = sc.nextInt(); int[][] target = new int[n][m]; int[][] result = new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ target[i][j] = sc.nextInt(); } } result[0][0] =p; stack.push(0); stack.push(0); int i=0; int j =0; while(i<n-1&&j<m-1) { //水平第一行 if (i==0) { if (target[i][j + 1] != 0) { result[i][j + 1] = result[i][j] - 1; stack.push(i); stack.push(++j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } else if (target[i + 1][j] != 0) { result[i + 1][j] = result[i][j]; stack.push(++i); stack.push(j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; }else { j=stack.pop(); i=stack.pop(); continue; } } //除掉水平垂直 if(i!=0&&j!=0){ //向右 if (target[i][j + 1] != 0) { result[i][j + 1] = result[i][j] - 1; stack.push(i); stack.push(++j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } //向上 if (target[i-1][j]!=0){ result[i-1][j] = result[i][j]-3; stack.push(--i); stack.push(j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } //向下 if (target[i + 1][j] != 0) { result[i + 1][j] = result[i][j]; stack.push(++i); stack.push(j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } //向左 if (result[i][j-1]==0&&target[i][j-1]!=0){ result[i][j-1]=result[i][j]-1; stack.push(i); stack.push(--j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } //弹出 else { j=stack.pop(); i=stack.pop(); continue; } } if(i!=0&&j==0){ //向右 if (target[i][j + 1] != 0) { result[i][j + 1] = result[i][j] - 1; stack.push(i); stack.push(++j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } //向下 if (target[i + 1][j] != 0) { result[i + 1][j] = result[i][j]; stack.push(++i); stack.push(j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } //弹出 else { j=stack.pop(); i=stack.pop(); continue; } } } if(j==m-1){ //shang for(int k=j;k>0;k--){ if(target[i][k]==0){ System.out.println("Can not escape!"); }else { result[i-1][j] = result[i][j]-3; stack.push(--i); stack.push(j); if(result[i][j]<0){ System.out.println("Can not escape!"); } continue; } } } Stack<Integer> stack1 = new Stack<Integer>(); while (!stack.isEmpty()){ stack1.push(stack.pop()); } while (!stack1.isEmpty()){ System.out.print("["); System.out.print(stack1.pop()); System.out.print(","); System.out.print(stack1.pop()); System.out.print("]"); if(!stack1.isEmpty()) System.out.print(","); } System.out.println(); } } }纪念一下#滴滴#