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