hdu Tempter of the Bone
很典型的dfs题,但是涉及到很多的剪枝 。
| s | ||||
| | | ||||
| | | ||||
| | | ||||
| + | — | — | — | e |
| s | — | — | — | |
| — | — | + | ||
| | | + | |||
| | | ||||
| + | — | — | — | e |
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"queue"
#include"string.h"
#include"string"
#define mx 105
using namespace std;
char maze[mx][mx];
int n,m,T,sx,sy,ex,ey;
int dir[][]={{,},{-,},{,},{,-}};
bool flag;
bool judge(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&maze[x][y]=='.') return true;
return false;
}
void dfs(int x,int y,int t)
{
if(t>T||flag) return;
if(t==T&&x==ex&&y==ey) {flag=true;return;}
int temp=abs(x-ex)+abs(y-ey);//当前位置到目标位置的最短路径
temp=T-t-temp;
if(temp%) return;//奇偶剪枝,不加这个一直tle也是醉了 。
for(int i=;i<;i++)
{
int dx=x+dir[i][];
int dy=y+dir[i][];
if(judge(dx,dy))
{
maze[dx][dy]='X';
dfs(dx,dy,t+);
maze[dx][dy]='.';
}
}
}
int main()
{ int i,j,blocks;
while(cin>>n>>m>>T,n&&m&&T)
{
flag=false;blocks=;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
cin>>maze[i][j];
if(maze[i][j]=='S')
{
sx=i;sy=j;maze[i][j]='X';
}
else if(maze[i][j]=='D')
{
ex=i;ey=j;maze[i][j]='.';
}
else if(maze[i][j]=='X') blocks++;
}
}
if(n*m-blocks->=T)//如果给定的时间数比能走的格子数还要大的话,就肯定不满足条件了
dfs(sx,sy,);
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}
hdu Tempter of the Bone的更多相关文章
- HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- hdu Tempter of the Bone (奇偶剪枝)
学习链接:http://www.ihypo.net/1554.html https://www.slyar.com/blog/depth-first-search-even-odd-pruning.h ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- Linux C编程(1) vim及gcc命令
1. 输入以下命令可以启动vi: (1) vi:不指定文件名,在保存文件时需要指定文件名. (2) vi 文件名:该文件既可以是已存在的,也可以是新建的. (3) vi ...
- 偷师--先留着。。ssh,nginx,防火墙相关命令
- python 文件操作总结
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- 关于Ue4的深度模板
http://www.unrealchina.net/forum.php?mod=viewthread&tid=100234
- XIII Open Cup named after E.V. Pankratiev. GP of Saratov
A. Box Game 注意到局面总数不超过$50000$,而且每次操作都会改变石子的奇偶性,因此按奇偶可以将状态建成二分图,然后求出最大匹配. 如果状态数是偶数,那么先手必胜,策略就是每次走匹配边, ...
- java 程序访问hdfs错误 hadoop2.2.0
很奇怪的问题,程序在eclipse上跑没问题: 这就代码:FileSystem fs = FileSystem.get(URI.create(hdfs_file), conf , "use ...
- hive Java API
Java连接hive进行操作的方式有多种,接触到了两种: 首先,hive要起动远程服务接口,命令: hive --service hiveserver -p 50000 & 1. 通过jdbc ...
- JDBC连接各种数据库的字符串,就是不好记
JDBC连接各种数据库的字符串大同小异,在此总结一下,备忘. oracle driverClass:oracle.jdbc.driver.OracleDriver url:jdbc:ora ...