Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 138326 Accepted Submission(s): 37026
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
import java.util.Scanner;
public class Main {
static int N;
static int M;
static int T;
static int dir[][]={{0,1},{-1,0},{0,-1},{1,0}};
static char[][] foot;
static Node[] aim;
static boolean[][] vis;
static int door;
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
door=0;
N=cin.nextInt();
M=cin.nextInt();
T=cin.nextInt();
aim=new Node[T+1];
vis=new boolean[N+1][M+1];
if(N==0&&M==0&&T==0)break;
foot=new char[N+1][M+1];
for(int i=1;i<=N;i++)
{
String str=cin.next();
for(int j=0;j<M;j++)
{
foot[i][j+1]=str.charAt(j);
if(foot[i][j+1]=='D')door=i+j+1;
}
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
if(foot[i][j]=='S'){
aim[0]=new Node(i,j);
vis[i][j]=true;}
}
}
if(dfs(0))System.out.println("YES");
else System.out.println("NO");
}
}
static boolean dfs(int start)
{
if(start==T)
{
if(foot[aim[T].x][aim[T].y]=='D'){
return true;
}
}else
{
Node pre=aim[start];
int rem=T-start;
int need=Math.abs(pre.x+pre.y-door);
if(rem<need||(rem-need)%2!=0)return false; //剪枝
for(int i=0;i<4;i++)
{
int x=pre.x+dir[i][0];
int y=pre.y+dir[i][1];
if(x>0&&y>0&&x<=N&&y<=M&&foot[x][y]!='X'&&!vis[x][y]){
vis[x][y]=true;
aim[start+1]=new Node(x,y);
if(dfs(start+1))return true;
//dfs(start+1);
vis[x][y]=false;
}
}
}
return false;
}
}
class Node
{
int x;
int y;
public Node(int xx,int yy)
{
x=xx;
y=yy;
}
}
1649




被折叠的 条评论
为什么被折叠?



