问题描述
- HUD 1010老超时求高手帮忙
-
import java.util.Scanner; public class Num2 { static int t,y2,x2,v; static char[][] map; public static void dfs(int i,int j,int count){ if(i<0 || j<0 || i>=map.length || j>=map[i].length){ return; } if(((t-count)-(Math.abs(x2-i)+Math.abs(y2-j)))%2 !=0){ return; } if((t-count)<(Math.abs(x2-i)+Math.abs(y2-j))){ return; } if(i==x2 && j==y2 || v==1){ if(count == t){ v = 1; } return; } if(map[i][j] != 'X'){ map[i][j] = 'X'; dfs(i,j+1,count+1); map[i][j] = '.'; dfs(i+1,j,count+1); map[i][j] = '.'; dfs(i,j-1,count+1); map[i][j] = '.'; dfs(i-1,j,count+1); map[i][j] = '.'; } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ v = 0; int x = sc.nextInt(); int y = sc.nextInt(); t = sc.nextInt(); if(x==0 && y==0 && t==0){ break; } int x1 = 0,y1 = 0; map = new char[x][y]; for(int i=0;i<x;i++){ String n = sc.next(); for(int j=0;j<y;j++){ map[i][j] = n.charAt(j); if(n.charAt(j)=='S'){ x1 = i; y1 = j; } if(n.charAt(j)=='D'){ x2 = i; y2 = j; } } } dfs(x1,y1,0); if(v==1){ System.out.println("YES"); }else{ System.out.println("NO"); } } } }
解决方案
有些情况运行没结果输出,求大神帮忙,谢谢
时间: 2024-10-29 04:03:49