鱼C论坛

 找回密码
 立即注册
查看: 1781|回复: 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与剪枝

  1. #include<cstdio>

  2. using namespace std;
  3. char maze[8][8];
  4. int n,m,t;
  5. bool success;
  6. int go[][2]={1,0,-1,0,0,1,0,-1};
  7. void DFS(int x,int y,int time)
  8. {
  9.         for(int i=0;i<4;i++)
  10.         {
  11.                 int nx=x+go[i][0];
  12.                 int ny=y+go[i][1];
  13.                 if(nx<1 || nx>n || ny<1 || ny>m)
  14.                         continue;
  15.                 if(maze[nx][ny]=='X')
  16.                         continue;
  17.                 if(maze[nx][ny]=='D')
  18.                 {
  19.                         if(time+1==t)
  20.                         {
  21.                                 success=true;
  22.                                 return;
  23.                         }
  24.                         else
  25.                                 continue;
  26.                 }
  27.                 maze[nx][ny]='X';
  28.                 DFS(nx,ny,time+1);
  29.                 maze[nx][ny]='.';
  30.                 if(success)
  31.                         return;
  32.         }
  33. }
  34. int main()
  35. {
  36.         while(scanf("%d%d%d",&n,&m,&t)!=EOF)
  37.         {
  38.                 if(n==0&&m==0&&t==0)
  39.                         break;
  40.                 for(int i=1;i<=n;i++)
  41.                         scanf("%s",maze[i]+1);
  42.                 success=false;
  43.                 int sx,sy;
  44.                 for(int i=1;i<=n;i++)
  45.                         for(int j=1;j<=m;j++)
  46.                                 if(maze[i][j]=='D')
  47.                                 {
  48.                                         sx=i;
  49.                                         sy=j;
  50.                                 }
  51.                 for(int i=1;i<=n;i++)
  52.                         for(int j=1;j<=m;j++)
  53.                                 if(maze[i][j]=='S'&&(i+j)%2==((sx+sy)%2+t%2)%2)
  54.                                 {
  55.                                         maze[i][j]='X';
  56.                                         DFS(i,j,0);
  57.                                 }
  58.                 puts(success==true?"YES":"NO");
  59.         }
  60.         return 0;
  61. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 02:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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