Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 112409 Accepted Submission(s): 30509
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.
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.
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
Author
ZHANG, Zheng
Source
ZJCPC2004
老题重做 一直WA
这题主要用到奇偶性剪枝
假设小狗位置是(i,j)
门在(tarI,tarJ)
step=abs(tarI-i,tarJ-j)
如果step是奇数 那小狗无论怎么走 必须要走奇数步才能到达门
如果剩余时间是偶数 肯定不能到达
step是偶数时也同理
restT表示剩余时间
当step%2+restT%2==1时 不可能达到
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
const int N=10;
const int dir[4][2]={
{-1,0},{1,0},{0,-1},{0,1}
};
char mp[N][N];
int n,m,t,stI,stJ,tarI,tarJ;
bool dfs(int i,int j,int restT){
if(restT==0&&i==tarI&&j==tarJ){
return true;
}
char tmp=mp[i][j];
mp[i][j]='X';
int step=abs(i-tarI)+abs(j-tarJ);
if(step>restT||(step%2+restT%2)==1){
mp[i][j]=tmp;//此处忘了将位置重置回去...一直WA
return false;
}
for(int k=0;k<4;++k){
int ni=i+dir[k][0],nj=j+dir[k][1];
if(ni>=0&&ni<n&&nj>=0&&nj<m&&(mp[ni][nj]!='X')){
if(dfs(ni,nj,restT-1)){
return true;
}
}
}
mp[i][j]=tmp;
return false;
}
void init(){
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(mp[i][j]=='S'){
stI=i,stJ=j;
}
if(mp[i][j]=='D'){
tarI=i,tarJ=j;
}
}
}
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
while(~scanf("%d%d%d",&n,&m,&t),n+m+t){
for(int i=0;i<n;++i){
scanf("%s",mp[i]);
}
init();
puts(dfs(stI,stJ,t)?"YES":"NO");
}
return 0;
}