HDU 1010 Tempter of the Bone 深搜+奇偶剪枝
来源:互联网 发布:手机麻将辅助软件 编辑:程序博客网 时间:2024/05/21 09:54
Total Submission(s): 91284 Accepted Submission(s): 24823
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.
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.
'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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
Author
ZHANG, Zheng
题意:就是在t步之内从S走到D;
思路:从开始处进行深搜,剪枝很多最主要的有奇偶剪枝
AC代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#include <cstdio>
#include <cmath>
using namespace std;
int n,m,t;
char gra[10][10];
int sx,sy,ex,ey;
int sum;
int tag;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
char gra[10][10];
int sx,sy,ex,ey;
int sum;
int tag;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
int ok(int x,int y){
if(x<0||y<0||x>=n||y>=m){
return 0;
}
if(gra[x][y]=='X'){
return 0;
}
return 1;
}
if(x<0||y<0||x>=n||y>=m){
return 0;
}
if(gra[x][y]=='X'){
return 0;
}
return 1;
}
void dfs(int x,int y){
if(x == ex&&y == ey&&sum == t){
tag = 1;
return ;
}
if(tag==1){ return ;}//找到即退出,不然会超时
if(sum >= t){
return ;
}
int tt = t-sum-abs(x-ex)-abs(y-ey);//计算当前位置距离目标的距离(即偏移距离)
if(tt < 0 || tt%2!=0){ return ; }
for(int i = 0;i < 4;i++){
if(ok(x+xx[i],y+yy[i])){
gra[x][y]='X';
sum++;
dfs(x+xx[i],y+yy[i]);
gra[x][y]='.';
sum--;
}
}
}
int main(){
while(~scanf("%d%d%d",&n,&m,&t)&&n&&m&&t){
for(int i = 0;i < n;i++){
scanf("%s",gra[i]);
for(int j = 0;j < m;j++){
if(gra[i][j] == 'S'){
sx = i;
sy = j;
}
if(gra[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
sum = 0;
tag = 0;
dfs(sx,sy);
if(tag){
printf("YES\n");continue;
}
printf("NO\n");
}
return 0;
}
if(x == ex&&y == ey&&sum == t){
tag = 1;
return ;
}
if(tag==1){ return ;}//找到即退出,不然会超时
if(sum >= t){
return ;
}
int tt = t-sum-abs(x-ex)-abs(y-ey);//计算当前位置距离目标的距离(即偏移距离)
if(tt < 0 || tt%2!=0){ return ; }
for(int i = 0;i < 4;i++){
if(ok(x+xx[i],y+yy[i])){
gra[x][y]='X';
sum++;
dfs(x+xx[i],y+yy[i]);
gra[x][y]='.';
sum--;
}
}
}
int main(){
while(~scanf("%d%d%d",&n,&m,&t)&&n&&m&&t){
for(int i = 0;i < n;i++){
scanf("%s",gra[i]);
for(int j = 0;j < m;j++){
if(gra[i][j] == 'S'){
sx = i;
sy = j;
}
if(gra[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
sum = 0;
tag = 0;
dfs(sx,sy);
if(tag){
printf("YES\n");continue;
}
printf("NO\n");
}
return 0;
}
注意:fsbs()在G++上可以ac,但在c++上不行,改成abs()就可以了。理由:abs是求整数的绝对值,fabs 是求实数的绝对值,主要的是类型不对,还有几个剪枝位置不能调换,
我就疯了这个题先是TLE后面就是WA,就是因为我把tt写成t,这是粗心。。。以后自习认真的做,之前自己还写过这个的博客又忘了这些东西。。。。。。。。
AC代码:
0 0
- HDU 1010 Tempter of the Bone 深搜+奇偶剪枝
- HDU 1010 Tempter of the Bone 【深搜+奇偶剪枝】
- hdu 1010Tempter of the Bone(经典奇偶剪枝)
- hdu 1010 Tempter of the Bone 奇偶剪枝
- HDU 1010 Tempter of the Bone(奇偶剪枝)
- HDU 1010 Tempter of the Bone - (DFS) 奇偶剪枝
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- HDU--1010 -- Tempter of the Bone [DFS+经典奇偶剪枝]
- HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)
- Hdu 1010 Tempter of the Bone (DFS 经典奇偶剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- HDU 1010 Tempter of the Bone( DFS + 奇偶剪枝 )
- hdu 1010 Tempter of the Bone(dfs + 奇偶剪枝)
- HDU 1010 Tempter of the Bone ( dfs + 奇偶剪枝 )
- HDU 1010 Tempter of the Bone (DFS+奇偶剪枝)
- 【奇偶剪枝】HDU 1010 Tempter of the Bone
- 自学的一些网站,自己在学,慢慢往里面填
- 黑马程序员之多线程
- 【面向对象】多态公有继承、抽象基类
- android studio在模拟器上显示中文乱码的解决方案
- 关于一个查找数组中是否存在某一个元素的问题
- HDU 1010 Tempter of the Bone 深搜+奇偶剪枝
- PAT Advanced 1013
- 2015.9.2组队赛 1006题(dp:前缀和优化,空间压缩)
- UVAlive6935
- mybatis-SQL语句映射文件(1)resultMap
- 英方法语学习
- 动态输出html一些效果失效的处理
- Windows Server 2008 R2开关机取消登录时要按Ctrl+Alt+Delete组合键登录的方法
- CentOS6.5 x64 Hadoop学习过程