Tempter of the Bone hdu 1010 (奇偶剪枝)

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

 

題意:小狗從s走到d,必須恰好走s步,問能否走到。

常規dfs/bfs,由於限定了步數,考慮奇偶剪枝

int k=step-s-fabs(ex-x)-fabs(ey-y);
//step總步數,s當前步數,exey 終點座標;
如果k值爲奇數,則無法到達。
if(k&1) 剪掉

ac:代碼

#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ss second
#define ff first
#define pb push_back
template <class T>
void read(T &x)
{
    static char ch;
    bool neg = false;
    while(!isdigit(ch = getchar())) (ch == '-') && (neg = true);
    for(x = ch - '0'; isdigit(ch = getchar()); x = x * 10 + ch - '0');
    (neg) &&(x = -x);
}
template<class T, class... U> void read(T &head, U &... tail)
{
    read(head), read(tail...);
}
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 205;
const int mod = 1e9 + 7;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int n, m, step;
char mp[maxn][maxn];
bool vis[maxn][maxn];
bool flag;
int sx, sy, ex, ey;

void dfs(int x, int y, int s)
{
    if(flag||s>step)return ;
    if(x==ex&&y==ey&&s==step)
    {
        flag=true;
    }
    int k=step-s-fabs(ex-x)-fabs(ey-y);
    if(k&1) return ;
    vis[x][y]=true;
    int xx, yy;
    for(int i = 0; i < 4; i++)
    {
        xx = x + dx[i]; yy = y + dy[i];
        if(xx >= n || xx < 0 || yy < 0 || yy >= m) continue;
        if(!vis[xx][yy])
        {
            vis[xx][yy]=true;
            dfs(xx,yy,s+1);
            vis[xx][yy]=false;
        }
    }


}

void work()
{
    memset(vis, false, sizeof(vis));
    flag = false;
    for(int i = 0; i < n; i++)
    {
        scanf("%s",mp[i]);
        for(int j=0;j<m;j++)
        {
            if(mp[i][j]=='S') sx=i,sy=j;
            else if(mp[i][j]=='D') ex=i,ey=j;
            else if(mp[i][j]=='X') vis[i][j]=true;
        }
    }
    dfs(sx,sy,0);
    if(flag) printf("YES\n");
    else printf("NO\n");


}

int main()
{
    //int T;read(T);while(T--)
    while(~scanf("%d %d %d", &n, &m, &step))
    {
        if(n==0&&m==0&&step==0) break;
        work();
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章