HDOJ 1010 Tempter of the Bone

一个简单的DFS,但需要根据奇偶性进行剪枝

Tempter of the Bone

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) :    Accepted Submission(s) : 
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
 


0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
处于1上的点移动到处于0上的点需要奇数步,而其他情况需要偶数步。




#include <iostream>
#include <string.h>
#include <cmath>

using namespace std;

char Map[9][9];
int vis[9][9];
int caca[9][9];

int d1[4]={-1,1,0,0};
int d2[4]={0,0,-1,1};

int n,m,t;

int dx,dy;

int OK;

void dfs(int x,int y,int cur)
{
    if(cur==t)
    {
        if(Map[x][y]=='D')
        {
           OK=1;
           return ;
        }
        else
            return ;
    }
    else for(int i=0;i<4;i++)
    {
        int x1=x+d1;
        int y1=y+d2;

        if(Map[x1][y1]!='X'&&vis[x1][y1]==0)
        {
            vis[x1][y1]=1;
            dfs(x1,y1,cur+1);
            if(OK)  return ;
            vis[x1][y1]=0;
        }
    }

}


int main()
{
    int a,b;
    memset(caca,0,sizeof(caca));
    for(int i=0;i<9;i++)
        for(int j=0;j<9;j++)
    {
        int c=i+j;
       if(c==1||c==3||c==5||c==7||c==9||c==11||c==13||c==15)
       {
           caca[j]=1;
       }
    }

while(cin>>n>>m>>t&&n!=0)
{
    OK=0;
    memset(Map,'X',sizeof(Map));
    memset(vis,0,sizeof(vis));



    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
       {
        cin>>Map[j];
        if(Map[j]=='S')  {a=i;b=j;}
        if(Map[j]=='D')  {dx=i;dy=j;}
       }
    bool a1=false;

    if(caca[a]==caca[dx][dy])
    {
      if(t%2==0)
        a1=true;
    }
    else if(caca[a]!=caca[dx][dy])
    {
      if(t%2==1)
        a1=true;
    }

    if(a1==false)  cout<<"NO"<<endl;
    else
    {
        vis[a]=1;
        dfs(a,b,0);
        if(OK==1)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }

}

    return 0;
}
 
原文地址:https://www.cnblogs.com/CKboss/p/3351093.html