Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 90328    Accepted Submission(s): 24554

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
 
又是一道搜索题目。使用深度优先搜索,注意要加入剪枝条件。不然非常easy造成超时,同一时候做搜索题要注意回溯。举个样例,若是求经典问题--油田的块数,我们不须要回溯,可是本题是求路径,我们须要用到回溯,不然会答案错误的,这是因为求油田块数的话,我们遍历了,不须要再次遍历,而求路径的话,若是求得的路径不通。须要将标记的路径还原,回溯非常重要!

。。

本题代码例如以下:
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std; int N,M,T,escape,wall;
int
starti,startj,endi,endj;
char
map[101][101];
int
v[101][101];
int
dir[4][2]={1,0,-1,0,0,1,0,-1};
void
dfs(int x,int y,int time)
{
if(
abs(endi-x)+abs(endj-y)>T-time)return;
if((
abs(endi-x)+abs(endj-y))%2!=(T-time)%2)return;
if(
x==endi&&y==endj&&time==T)escape=1;
if(
escape==1)return;
for(int
i=0;i<=3;i++)
{
int
xx=x+dir[i][0];
int
yy=y+dir[i][1];
if(
xx>=1&&xx<=N&&yy>=1&&yy<=M&&map[xx][yy]!='X'&&v[xx][yy]==0)
{

v[xx][yy]=1;
dfs(xx,yy,time+1);
v[xx][yy]=0; //回溯
}
}
return;
}
int main()
{
while(
cin>>N>>M>>T,N||M||T)
{

wall=0;
memset(v,0,sizeof(v));
for(int
i=1;i<=N;i++)
for(int
j=1;j<=M;j++)
{

cin>>map[i][j];
if(
map[i][j]=='S')
{

starti=i;
startj=j;
}
else if(
map[i][j]=='X')
{

wall++;
}
else if(
map[i][j]=='D')
{

endi=i;
endj=j;
}
}

v[starti][startj]=1;
escape=0;
dfs(starti,startj,0);
if(
N*M-wall<T){cout<<"NO"<<endl;continue;}
if(
escape==1)cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return
0;
}

最新文章

  1. 百度UEditor在线编辑器的配置和图片上传
  2. jQuery弹出深色系层菜单
  3. Selenium 元素定位
  4. Access、Hybrid和Trunk
  5. Java JDBC高级特性
  6. DOM Element节点类型详解
  7. virtualenv 和 virtualenvwrapper 实践
  8. [Javascript] Lodash: Refactoring Simple For Loops (_.find, _.findLast, _.filter)
  9. 计算Android屏幕解锁组合数
  10. SharePoint场管理-PowerShell(二)
  11. RBAC打造通用WEB权限
  12. Java设计模式之(一)------单例模式
  13. poj-1146 ID codes
  14. mysql 中通过身份证号码计算年龄
  15. Lab 10-2
  16. ThreadLocal(一):Thread 、ThreadLocal、ThreadLocalMap
  17. 如何取得Oracle并行执行的trace
  18. Notepad++的右键菜单
  19. (转)Python中集合(set)的基本操作以及一些常见的用法
  20. oracle01--单表查询

热门文章

  1. PCB MS SQL CLR聚合函数(函数作用,调用顺序,调用次数) CLR说明
  2. linux shell 编程笔记
  3. Run as ant build每次都执行两次-问题解决
  4. 常用MIME类型(Flv,Mp4的mime类型设置)
  5. Visual Studio切换界面显示语言
  6. Java基础12一IO流
  7. 【转载】【翻译】JavaScript Scoping and Hoisting--JS作用域和变量提升的探讨
  8. Clustered Index Scan 与 Clustered Index Seek
  9. 时序分析:串匹配-KMP算法
  10. AS3.0+PHP写入mySQL