HDU1010 --- Tempter of the Bone(dfs+剪枝)
小明做了一个很久很久的梦,醒来后他竟发现自己和朋友在一个摇摇欲坠的大棋盘上,他们必须得想尽一切办法逃离这里。
经过长时间的打探,小明发现,自己所在的棋盘格子上有个机关,上面写着“你只有一次机会,出发后t秒大门会为你敞开”,而他自己所在的棋盘是大小为 N*M 的长方形,他可以向上下左右四个方向移动(不可走有障碍点)。棋盘中有一扇门。根据机关的提示,小明顿时明白了,他和朋友必须在第 t 秒到门口。而这一切,没有回头路!因为一旦他移动了,他刚才所在的点就会消失,并且他不能在一个点上停留超过一秒,不然格子会爆炸。大逃亡开始了,请问小明和朋友能安全的逃出这奇怪的棋盘吗?
Input
输入多组测试数据。每个测试用例的第一行包含三个整数 N、M 和 T ( 1 < N , M < 7 ; 0 < T < 50 ),分别表示棋盘的大小和门打开的时间。接下来的N行给出棋盘布局,每一行包含M个字符。其中
".": 无障碍点
"X": 障碍点
"S": 起点
"D": 门
输入以 3 个 0 结束。这个测试用例不需要处理。
Output
对于每组样例输出一行。
如果小明能够安全逃出,输出 "YES" ,否则输出 "NO"。
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES 代码:
import java.util.Arrays;
import java.util.Scanner; public class Main{
static int n,m,T;
static final int N=10;
static int sx,sy,ex,ey;
static int dx[]={0,0,1,-1};
static int dy[]={1,-1,0,0};
static char map[][]=new char[N][N];
static boolean vis[][]=new boolean[N][N];
static boolean flag; static void dfs(int x,int y,int t){
//这种多if判断的一定加上return
if(flag) return; //已经找到一种走法
if(t>T) return; //大于要求的时间
if(T-t-Math.abs(x-ex)-Math.abs(y-ey)<0) return; //剩余的步数不足以到达终点
if((T-t)%2!=(Math.abs(x-ex)+Math.abs(y-ey))%2) return; //奇偶性一致
if(t==T && x==ex && y==ey){
flag=true;
} for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx<0 || yy<0 || xx>=n || yy>=m ||vis[xx][yy]) continue;
vis[xx][yy]=true;
dfs(xx,yy,t+1);
vis[xx][yy]=false;
}
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
n=scan.nextInt();
m=scan.nextInt();
T=scan.nextInt();
if(n==0 && m==0 &&T==0) break;
for(int i=0;i<n;i++) map[i]=scan.next().toCharArray();
for(int i=0;i<N;i++) Arrays.fill(vis[i],false);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='S'){
sx=i; sy=j;
}
else if(map[i][j]=='D'){
ex=i; ey=j;
}
else if(map[i][j]=='X')
vis[i][j]=true;
vis[sx][sy]=true;
flag=false;
dfs(sx,sy,0);
if(flag) System.out.println("YES");
else System.out.println("NO");
}
}
}
HDU1010 --- Tempter of the Bone(dfs+剪枝)的更多相关文章
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- Tempter of the Bone dfs+剪枝
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- B - Tempter of the Bone(DFS+剪枝)
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 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 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- js删除对象数组
若用remove删除某个对象数组,使用for循环遍历数组中的每个对象进行删除,则必须从数组的最后一个元素倒序删除,否则每次删除都只能删除数组的一半元素,因为把索引为0的子节点删除后那么很自然的原来索引 ...
- 解决Python3.7安装pygame报错You are using pip version 10.0.1, however version 19.1 is available.
背景: 学习python开发中,需要用到pygame插件,因此按照参考书<Python编程实践,从入门到实践>指引安装Pygame包. 但是利用pip 命令安装 .whl 文件时,报错(如 ...
- 离线部署ArcGIS Runtime for Android100.5.0
环境 系统:window 7 JDK:1.8.0_151 Maven:3.6.1 Android Studio:2.3 ArcGIS Runtime SDK for Android:100.5.0 1 ...
- PHP0021:PHP COOKIE 设置修改删除
- XSS跨站测试代码
'><script>alert(document.cookie)</script>='><script>alert(document.cookie)&l ...
- Hadoop学习之路(8)Yarn资源调度系统详解
文章目录 1.Yarn介绍 2.Yarn架构 2.1 .ResourceManager 2.2 .ApplicationMaster 2.3 .NodeManager 2.4 .Container 2 ...
- 位运算基础知识及简单例题(待补全Hamilton)
位运算 +++ 1 : 0000000000...01 2 : 0000000000...10 3 : 0000000000...11 补码 1 + x = 0000000000...00 1 + 1 ...
- CF1227F2 Wrong Answer on test 233 (Hard Version)
题意 \(n\)道题,每道题有\(k\)种选项,其中第\(i\)道题正确答案是\(a_i\),但是填答案的时候填错啦,第一道题的选择填到了第二道题...第\(n\)道题的选择填到了第一道题,求在\(k ...
- 服务器的公网ip 和内网ip
原文地址:https://zhidao.baidu.com/question/814783729071869532.html 服务器公网ip 可以用于域名解析ip,服务器远程登录ip,是最主要的服务器 ...
- 如果linux开机没有ip怎么办
1.vim编辑网卡配置文件,修改如下参数 [root@s25linux tmp]# cd /etc/sysconfig/network-scripts/vim修改此文件,找到如下参数,改为yesONB ...