听听 艾薇儿 girlfriend

zoj 2110 Tempter of the Bone

ZOJ Problem Set - 2110
Tempter of the Bone

Time Limit: 1 Second      Memory Limit: 32768 KB

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: Zhejiang Provincial Programming Contest 2004
Submit    Status
//1861184 2009-05-09 13:07:33 Time Limit Exceeded  2110 C++ 1001 184 Wpl 
//1861217 2009-05-09 13:22:44 Accepted  2110 C++ 900 184 Wpl 
//1861239 2009-05-09 13:34:01 Accepted  2110 C++ 10 184 Wpl
//1861244 2009-05-09 13:34:49 Accepted  2110 C++ 0 184 Wpl  
//奇偶剪枝,再次剪枝
#include <iostream>
#include 
<cmath>
#define MAX 7
using namespace std;
int sx,sy,ex,ey;
int n,m,t;
int a[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char map[MAX][MAX];
bool used[MAX][MAX],mark;
int num;
void Init()
{
    
int i,j;
    
for(i=0;i<n;i++)
        
for(j=0;j<m;j++)
        {
            cin
>>map[i][j];
            
if(map[i][j]=='S')
            {
                sx
=i;
                sy
=j;
            }
            
else if(map[i][j]=='D')
            {
                ex
=i;
                ey
=j;
            }
            used[i][j]
=false;
        }
}
bool Bound(int x,int y)
{
    
if(x>=0&&y>=0&&x<n&&y<m)
        
return true;
    
else
        
return false;
}
int Dis(int x1,int y1,int x2,int y2)
{
    
return abs(x1-x2)+abs(y1-y2);
}
void DFS(int x,int y,int step)
{
    
if(mark||num>10000)  //num>10000这个条件是从shǎ崽大牛那学来的,其实这不是正确的途径,用这个方法竟然刷到0ms
        return;
    
if(x==ex&&y==ey&&step==t)
    {
        mark
=true;
        
return ;
    }
    
int k,tx,ty;
    
int dis=Dis(x,y,ex,ey);
    
if(dis>(t-step))
        
return;
    
if(dis%2!=(t-step)%2)
        
return ;
    
for(k=0;k<4;k++)
    {
        tx
=x+a[k][0];
        ty
=y+a[k][1];
        
if(Bound(tx,ty)&&!used[tx][ty]&&map[tx][ty]!='X')
        {
            num
++;
            step
++;
            used[tx][ty]
=true;
            DFS(tx,ty,step);
            step
--;
            used[tx][ty]
=false;
        }
    }    
}
int main()
{
    
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        
if(n==0&&m==0&&t==0)
            
break;
        Init();
        mark
=false;
        used[sx][sy]
=true;
        num
=0;
        DFS(sx,sy,
0);
        
if(mark)
            printf(
"YES\n");
        
else
            printf(
"NO\n");
    }
    
return 0;
}
posted @ 2009-05-09 13:27  往往  阅读(1357)  评论(3编辑  收藏  举报