Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 79712 Accepted Submission(s): 21753
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
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.
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.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'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.
'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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
题目类型:深搜+剪枝(奇偶剪枝)
题意:在一个二维坐标内,给定起点终点,问能否恰好在给定时间T时刻从起点到达终点到达,到达某一点时不能停留,每到达一点时间加一
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char a[10][10];
int visit[10][10];
int d[4][2]={{-1,0},{0,-1},{1,0},{0,1}};//方向
int N,M,T;
int S1,S2; //进洞口坐标
int D1,D2; //出洞口坐标
int flag;
void dfs(int row,int col,int t)
{
int i;
if(flag)
return ;
if(row<0||row>N||col<0||col>M)
return ;
int b=fabs(D1-row)+fabs(D2-col) ;//奇偶剪枝,我不会按照网上别人代码写的
if(b>T-t||(T-t+b)%2!=0)
{
return ;
}
if(row==D1&&col==D2)
{
if(t==T)
{
flag=1;
return ;
}
}
for(i=0;i<4;i++)
{
int x=row+d[i][0] ; //切记不能row+=d[i][0];
int y=col+d[i][1] ;
if(x>=0&&x<=N-1&&y>=0&&y<=M-1&&!visit[x][y])
{
visit[x][y]=1;
dfs(x,y,t+1);
visit[x][y]=0;
}
}
}
int main()
{
int i,j,num;
while(scanf("%d%d%d",&N,&M,&T)!=EOF&&M&&N&&T)
{
num=0;
memset(visit,0,sizeof(visit));
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
char ch;
cin>>ch;
a[i][j]=ch;
if(a[i][j]=='S')
{
S1=i;
S2=j;
}
if(a[i][j]=='D')
{
D1=i;
D2=j;
}
if(a[i][j]=='X')
{
visit[i][j]=1;
num++;
}
}
}
if(N*M-num-1<T) //“.”的数目少于时间T
{
cout<<"NO"<<endl;
continue;
}
flag=0;
visit[S1][S2]=1;
dfs(S1,S2,0);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}