HDU 1010 DFS

http://acm.hdu.edu.cn/showproblem.php?pid=1010

Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30063 Accepted Submission(s): 8184


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

有一个剪枝 可以把map看成这样: 
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1


从为 0 的格子走一步,必然走向为 1 的格子
从为 1 的格子走一步,必然走向为 0 的格子
即:
0 ->1或1->0 必然是奇数步
0->0 走1->1 必然是偶数步
结论:
所以当遇到从 0 走向 0 但是要求时间是奇数的,或者,
从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达! 

#include <iostream>
#include <math.h>

using namespace std;

int used[8][8];
int Move[4][2]=
{
{-1,0},{0,-1},{1,0},{0,1}
};
int N,M,T,bx,by,ex,ey;

struct Node
{
int x,y;
int count;
};


bool Dfs(int x,int y,int count)
{
int tx,ty;
if (x==ex&&y==ey)
{
if (count==T)
return true;
}
for (int i=0;i<4;i++)
{
tx=x+Move[i][0];
ty=y+Move[i][1];
if (tx>=0&&tx<N&&ty>=0&&ty<M&&!used[tx][ty])
{
used[tx][ty]=1;
if(Dfs(tx,ty,count+1))
return true;
used[tx][ty]=0;
}
}
return false;
}

void Input(int r,int c)
{
memset(used,0,sizeof(used));
char ch;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>ch;
if (ch=='S')
{
bx=i;
by=j;
used[i][j]=1;
}
else if(ch=='D')
{
ex=i;
ey=j;
}
else if (ch=='X')
{
used[i][j]=1;
}
}
}
}

int main(int argc, char* argv[])
{
while(cin>>N>>M>>T)
{
if (N==0&&M==0&&T==0)
break;
Input(N,M);
if (((abs(bx-ex)+abs(by-ey))&1)==
(T&1)) //奇偶校验
{
if (Dfs(bx,by,0))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else
cout<<"NO"<<endl;
}
return 0;
}


 

你可能感兴趣的:(HDU)