hdu1010 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): 91286    Accepted Submission(s): 24824


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
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1241  1242  1072  1312  1026 
 

解析:注意,是T s恰好到达门那里,并不是 T s内。
          1.01剪枝。
             在方格中从一个点到另一个点,无论你怎样走,总步数的奇偶性总是不变的,比如在9X9的方格中,从(1,1)到(2,2)的最短距离为2,那么无论你怎样走,从(1,1)到(2,2)的步数都是一个偶数。
            由上即得到第一个剪枝:设起点(sx,sy),终点(zx,zy),若abs(sx-zx)+abs(sy-zy)与T的奇偶性不一致,则NO;
          2.若点钱搜索到点(x,y ),已走step步,若step+abs(x-zx)+abs(y-zy)>T则返回0;
代码:
#include<cstdio>
#include<cmath>
#include<cstring>

int n,m,t,sx,sy,zx,zy;
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
char s[10][10];
bool used[10][10];

bool dfs(int x,int y,int step)
{
  if(s[x][y]=='D')return step==t;
  if(step+abs(x-zx)+abs(y-zy)>t)return 0;
  
  int i,j,k;
  for(k=0;k<4;k++)
    {
      i=x+dx[k],j=y+dy[k];
      if(i<0 || i>=n || j<0 || j>=m || 
	     used[i][j] || s[i][j]=='X')continue;
	  used[i][j]=1;
	  if(dfs(i,j,step+1))return 1;
	  used[i][j]=0;
	}
  return 0;
}

int main()
{
  int i,j,k;
  while(scanf("%d%d%d",&n,&m,&t),n!=0)
    {
      for(i=0;i<n;i++)scanf("%s",s[i]);
      for(k=0,i=0;i<n;i++)
        for(j=0;j<m;j++)
          {
            if(s[i][j]=='S')sx=i,sy=j;
            if(s[i][j]=='D')zx=i,zy=j;
            if(s[i][j]=='X')k++;
		  }
      if(n*m-k<t || (abs(zx-sx)+abs(zy-sy)+t)&1)goto d1;
      memset(used,0,sizeof(used));
      used[sx][sy]=1;
	  if(dfs(sx,sy,0))printf("YES\n");
	  else d1:printf("NO\n");
	}
  return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值