ZOJ 2110 Tempter of the Bone
Tempter of the Bone
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
普通的搜索题,写完以后不幸TLE
之后看题解,发现这道题作为一道教程(完全没看)题,专门讲剪枝…… WTF!
于是加了个剪枝过了。300+ms,实际上可以各种花式剪枝把时间压到80ms,但是看写博客的时间就知道我为什么不想多写
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char map[20][20];
int mx[5]={0,-1,0,1,0},
my[5]={0,0,1,0,-1};
int n,m,Time;
int tx,ty;
int flag=0;
void dfs(int x,int y,int t){
if(x==tx && y==ty && t==Time){
flag=1;
return;
}
if(map[x][y]=='X')return;
//
if(t>=Time)return;//剪枝
int temp=(Time-t)-fabs(x-tx)-fabs(y-ty);
if(temp<0 ||temp %2)return; //
int i;
for(i=1;i<=4;i++){
int nx=x+mx[i];
int ny=y+my[i];
if(nx>=0 && nx<n && ny>=0 && ny<m)
// if(map[nx][ny]!='X')
{
map[x][y]='X';
dfs(nx,ny,t+1);
if(flag)return;
map[x][y]='.';
}
}
return;
}
int main(){
while(scanf("%d%d%d",&n,&m,&Time)!=EOF)
{
flag=0;
if(n==0 && m==0 && Time==0)break;
int i,j;
int xx,yy;
for(i=0;i<n;i++){
scanf("%s",map[i]);
for(j=0;j<m;j++){
if(map[i][j]=='S')xx=i,yy=j;
if(map[i][j]=='D')tx=i,ty=j;//目标点
}
}
dfs(xx,yy,0);
if(flag==1)printf("YES\n");
else printf("NO\n");
}
return 0;
}
最新文章
- CSS media queries
- git 教程(12)--分支管理
- Web的Ajax应用开发模式(二)——Ajax开发模式分析
- android:descendantFocusability=”blocksDescendants”的用法
- easyUI API(version 1.5)
- TCP 连接建立和断开,以及状态转换
- 一模 (2) day2
- 关闭HTML5只能提示(form上新增novalidate)
- Tableau学习笔记之二
- ###《VIM实用技巧》
- 十一、mysql输入安全
- Servlet课程0425(四) Servlet实现简单用户登录验证
- 计算机视觉的matlab工具箱及MVG等
- strut2.xml中result param详细设置
- Oracle通过主键id删除记录很慢
- ASP.NET MVC 5 学习教程:添加验证
- WPF类层次结构
- 201521123049 《JAVA程序设计》 第12周学习总结
- c语言-第零次作业
- maya_关于脚本编辑器导入python模块
热门文章
- Jenkins遇到问题二:Jenkins服务器磁盘空间管理策略
- man 在线手册
- [转]Nginx+ThinkPHP不支持PathInfo的解决办法
- 怎么用JS截取字符串中第一个和第二个字母间的部分?
- 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法
- matlab绘制三维图形
- [MetaHook] R_RicochetSprite
- RocEDU.阅读.写作《你的灯亮着吗?》
- 工作随笔——CentOS6.4支持rz sz操作
- 窥探算法之美妙——寻找数组中最小的K个数&;python中巧用最大堆