鱼C论坛

 找回密码
 立即注册
查看: 1911|回复: 1

[技术交流] 035:Temple of the bone

[复制链接]
发表于 2018-2-17 17:01:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-17 17:01:34 | 显示全部楼层
dfs与剪枝
#include<cstdio>

using namespace std;
char maze[8][8];
int n,m,t;
bool success;
int go[][2]={1,0,-1,0,0,1,0,-1};
void DFS(int x,int y,int time)
{
        for(int i=0;i<4;i++)
        {
                int nx=x+go[i][0];
                int ny=y+go[i][1];
                if(nx<1 || nx>n || ny<1 || ny>m)
                        continue;
                if(maze[nx][ny]=='X')
                        continue;
                if(maze[nx][ny]=='D')
                {
                        if(time+1==t)
                        {
                                success=true;
                                return;
                        }
                        else
                                continue;
                }
                maze[nx][ny]='X';
                DFS(nx,ny,time+1);
                maze[nx][ny]='.';
                if(success)
                        return;
        }
}
int main()
{
        while(scanf("%d%d%d",&n,&m,&t)!=EOF)
        {
                if(n==0&&m==0&&t==0)
                        break;
                for(int i=1;i<=n;i++)
                        scanf("%s",maze[i]+1);
                success=false;
                int sx,sy;
                for(int i=1;i<=n;i++)
                        for(int j=1;j<=m;j++)
                                if(maze[i][j]=='D')
                                {
                                        sx=i;
                                        sy=j;
                                }
                for(int i=1;i<=n;i++)
                        for(int j=1;j<=m;j++)
                                if(maze[i][j]=='S'&&(i+j)%2==((sx+sy)%2+t%2)%2)
                                {
                                        maze[i][j]='X';
                                        DFS(i,j,0);
                                }
                puts(success==true?"YES":"NO");
        }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-6 07:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表