Tempter of the Bone

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1560 Accepted Submission(s): 516
 
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
 
Recommend
JGShining
 
/*
天真的我以为bfs就可以
*/
#include<bits/stdc++.h>
#define N 10
using namespace std;
struct node
{
    int x,y,s;
    node(){}
    node(int a,int b,int c)
    {
        x=a;
        y=b;
        s=c;
    }
};
int n,m,k;
int fx,fy,ex,ey;
char mapn[N][N];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool vis[N][N];
bool ok(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||vis[x][y]||mapn[x][y]=='X')
        return true;
    return false;
}
int bfs()
{
    memset(vis,false,sizeof vis);
    queue<node>q;
    q.push(node(fx,fy,k));
    vis[fx][fy]=true;
    while(!q.empty())
    {
        node fr=q.front();
        q.pop();
        //cout<<fr.x<<" "<<fr.y<<endl;
        if(mapn[fr.x][fr.y]=='D'&&fr.s==0)
        {
            //cout<<fr.s<<endl;
            return 1;
        }
        for(int i=0;i<4;i++)
        {
            int vx=fr.x+dir[i][0];
            int vy=fr.y+dir[i][1];
            if(ok(vx,vy)) continue;
            q.push(node(vx,vy,fr.s-1));
            vis[vx][vy]=true;
        }
    }
    return -1;
}
int main()
{
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    while(scanf("%d%d%d",&n,&m,&k)!=EOF&&(n!=0&&m!=0&&k!=0))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",mapn[i]);
            for(int j=0;j<m;j++)
            {
                if(mapn[i][j]=='S')
                {
                    fx=i;
                    fy=j;
                }
                if(mapn[i][j]=='D')
                {
                    ex=i;
                    ey=j;
                }
            }
        }
        //if(k>abs(fx-ex)+abs(fy-ey))
        //    puts("NO");
        //else
        //{
            if(bfs()==-1)
                puts("NO");
            else
                puts("YES");
        //}
    }
}

 

/*
奇偶剪枝
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到0,1到1都是偶数步
所以当0到0时要求时间是奇数,从1到0的时候时间是偶数,可以直接判段是不可达的
*/
#include<bits/stdc++.h>
#define N 10
using namespace std;
int n,m,k;
int ex,ey;
char mapn[N][N];
int w;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int f=0;
void dfs(int x,int y,int s)
{
    if(x<0||x>=n||y<0||y>=m) return ;
    if(x==ex && y==ey && s==k)
        f=0;
    if(!f)
        return ;
    int s1=x-ex;
    int s2=y-ey;
    if(s1<0)
        s1=-s1;
    if(s2<0)
        s2=-s2;
    int cur=k-s-s1-s2;
    if(cur<0||cur&1)
        return ;
    for(int i=0;i<4;i++)
    {
        int fx=x+dir[i][0];
        int fy=y+dir[i][1];
        if(mapn[fx][fy]!='X')
        {
            mapn[fx][fy]='X';
            dfs(fx,fy,s+1);
            mapn[fx][fy]='.';
        }
    }
    return;
}
int main()
{
    int fx,fy;
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        if(n==0&&m==0&&k==0)
            break;
        w=0;
        getchar();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&mapn[i][j]);
                if(mapn[i][j]=='S')
                {
                    fx=i;
                    fy=j;
                }
                if(mapn[i][j]=='D')
                {
                    ex=i;
                    ey=j;
                }
                if(mapn[i][j]=='X')
                    w++;
            }
            getchar();
        }
        if(k+w>=n*m)
        {
            //cout<<"111"<<endl;
            puts("NO");
            continue;
        }
        f=1;
        mapn[fx][fy]='X';
        dfs(fx,fy,0);
        if(f)
            puts("NO");
        else
            puts("YES");
    }
    return 0;
}
/*
4 4 9
S..X
X.X.
..XD
....
*/

 

posted @ 2016-11-15 17:55  勿忘初心0924  阅读(237)  评论(0编辑  收藏  举报