Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 53106    Accepted Submission(s): 14281

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
讲解:注意看清题的意思,题中说的是要,在正好的时间内,找到D,而不是小于或等于D
   特殊数据
6 6 37
S.....
......
......
......
......
D.....
NO
意思是在正好等于37 的时候 没有找到D
 #include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
int fangxiang[][]={{-,},{,},{,-},{,}};
const int MAX=;
char map[MAX][MAX];
int mark[MAX][MAX];
int n,m,t;
int start_x,start_y;
int end_x,end_y;
using namespace std;
bool DFS(int x,int y,int step)
{
int i,a,b;
if(map[x][y]=='D' && step==t)
return true;
if(x<||x>n || y< || y>m)//判断到最后了,还没有找到
return false;
if(step>=t)//剪枝1:当step>=T时还没有找到D点
return false;
if(t-step<(abs(x-end_x)+abs(y-end_y)))//剪枝2:还需要的步数比理论上的最短距离还小
return false;
if((t-step-(abs(x-end_x)+abs(y-end_y)))%!=) //剪枝3:比理论上的最短距离多出来的必是偶数
return false;
for(i=;i<;i++)
{
a=x+fangxiang[i][];
b=y+fangxiang[i][];
if(a<=n && a>= && b>= && b<=m && map[a][b]!='X' && !mark[a][b]) //判断三个条件:1.检验_x,_y是否越界。2.看vis[][]是否访问过。3.看map[][]是否是墙
{
mark[a][b]=;
if(DFS(a,b,step+))
return true;
else
mark[a][b]=;
}
}
return false;
}
int main()
{
int i,j;
while(cin>>n>>m>>t && n+m+t)
{
memset(mark,,sizeof(mark));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
start_x=i;
start_y=j;
}
if(map[i][j]=='D')
{
end_x=i;
end_y=j;
}
}
}
mark[start_x][start_y]=;
if(DFS(start_x,start_y,))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

最新文章

  1. codeblocks+Mingw 下配置开源c++单元测试工具 google test
  2. [问题2014S08] 解答
  3. 如何把apk编译时间和最后次git commit的sha值,写入到app中
  4. [转]SEP 11.x 迁移
  5. PL/SQL Select into 异常处理
  6. poj 2369 Permutations 更换水称号
  7. Fragment与Activity交互(使用接口)
  8. 在LINQ查询中LINQ之Group By的用法
  9. Ubuntu 设置内核版本的GRUB默认启动
  10. javascript项目实战---ajax实现无刷新分页
  11. MyDAL - .IsExistAsync() 使用
  12. 10款 Mac 系统优化清理工具软件推荐和下载
  13. ReSharper 10.0.0.1 Ultimate 完美破解补丁使用方法
  14. 几个优化SQL查询的方法
  15. sql server查询某年某月有多少天
  16. Java加密技术(一)——加密介绍
  17. 让你的APP和你的服务器畅快通讯
  18. r语言 包说明
  19. NIS - 深入了解如何搭建NIS环境
  20. Qt计算两个时间差

热门文章

  1. 【C++系列小结】面向过程的编程风格
  2. Python Module和Package辨析
  3. FIS3 构建 工程化
  4. jdeveloper优化:
  5. epoll+socket的简单测试例子
  6. login.jsp
  7. PHP生成缩略图、加水印
  8. LNMP的的编译安装全过程
  9. CoInitialize和CoInitializeEx
  10. NYOJ-------笨蛋难题四