传送门:1010
Tempter of the Bone
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
思路:dfs搜索。此题考剪枝,dis=(abs(ex-cx)+abs(ey-cy))是到D点最近的距离,(t-tc)-dis是要多走的距离,由题意不能斜走,所以要多走的距离一定时偶数。
代码:
/************************************************
* Author: Ac_sorry
* File:
* Create Date:
* Motto: One heart One life
* CSDN: http://blog.csdn.net/code_or_code
*************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define seed_ 131
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof a)
#define w(i) tree[i].w
#define ls(i) tree[i].ls
#define rs(i) tree[i].rs
using namespace std;
typedef long long LL;
const int N=50010;
int n,m,sx,sy,ex,ey,t;
char gra[30][30];
int vis[30][30];
int go[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int ok;
void dfs(int cx,int cy,int ct)
{
int tmp=(t-ct)-(abs(ex-cx)+abs(ey-cy));
if(ok||ct>t||tmp<0||tmp%2) return;
if(ct==t&&cx==ex&&cy==ey){
ok=1;
return;
}
int x,y;
for(int i=0;i<4;i++)
{
x=cx+go[i][0];
y=cy+go[i][1];
if(x>=0&&x<n&&y>=0&&y<m&&(gra[x][y]=='.'||gra[x][y]=='D'||gra[x][y]=='S')&&vis[x][y]==0)
{
vis[x][y]=1;
dfs(x,y,ct+1);
vis[x][y]=0;
}
}
}
int main()
{
//int T;scanf("%d",&T);
while(cin>>n>>m>>t)
{
if(m==0&&n==0&&t==0)
break;
ok=0;
for(int i=0;i<n;i++){
scanf("%s",gra[i]);
for(int j=0;j<m;j++)
{
if(gra[i][j]=='S')
{
sx=i;
sy=j;
}
else if(gra[i][j]=='D')
{
ex=i;
ey=j;
}
}
}
vis[sx][sy]=1;
dfs(sx,sy,0);
vis[sx][sy]=0;
if(ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}