B - Tempter of the Bone(DFS+剪枝)
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.
InputThe 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.
OutputFor 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
学到了剪枝的一些知识。
AC代码
1 #include<iostream>
2 #include<cstring>
3 #include<algorithm>
4 #define N 10
5
6 using namespace std;
7
8 int n,m,t,end_i,end_j;
9 bool visited[N][N],flag,ans;
10 char mapp[N][N];
11
12 int abs(int a,int b)
13 {
14 if(a<b) return b-a;
15 else return a-b;
16 }
17
18 void DFS(int i,int j,int c)
19 {
20 if(flag) return ;
21 if(c>t) return ;
22 if(i<0||i>=n||j<0||j>=m) {return ;}
23 if(mapp[i][j]=='D'&&c==t) {flag=ans=true; return ;}
24 int temp=abs(i-end_i)+abs(j-end_j);
25 temp=t-temp-c;
26 if(temp&1) return ;//奇偶剪枝
27
28 if(!visited[i-1][j]&&mapp[i-1][j]!='X')
29 {
30 visited[i-1][j]=true;
31 DFS(i-1,j,c+1);
32 visited[i-1][j]=false;
33 }
34 if(!visited[i+1][j]&&mapp[i+1][j]!='X')
35 {
36 visited[i+1][j]=true;
37 DFS(i+1,j,c+1);
38 visited[i+1][j]=false;
39 }
40 if(!visited[i][j-1]&&mapp[i][j-1]!='X')
41 {
42 visited[i][j-1]=true;
43 DFS(i,j-1,c+1);
44 visited[i][j-1]=false;
45 }
46 if(!visited[i][j+1]&&mapp[i][j+1]!='X')
47 {
48 visited[i][j+1]=true;
49 DFS(i,j+1,c+1);
50 visited[i][j+1]=false;
51 }
52 }
53
54 int main()
55 {
56 int i,j,x,y,k;
57 while(cin>>m>>n>>t&&(m||n||t))
58 {
59 memset(visited,false,sizeof(visited));
60 k=0;
61 for(i=0;i<n;i++)
62 {
63 for(j=0;j<m;j++)
64 {
65 cin>>mapp[i][j];
66 if(mapp[i][j]=='S')
67 {
68 x=i;y=j;
69 visited[i][j]=true;
70 }
71 if(mapp[i][j]=='D')
72 {
73 end_i=i;end_j=j;
74 }
75 if(mapp[i][j]=='X')k++;
76 }
77 }
78 ans=flag=false;
79 if(n*m-k-1>=t) DFS(x,y,0);
80 if(ans) cout<<"YES"<<endl;
81 else cout<<"NO"<<endl;
82 }
83 return 0;
84 }
B - 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//一个很好理解剪枝思想的博客 ...
- 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 ...
- 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 ...
- Tempter of the Bone(dfs奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- M - Tempter of the Bone(DFS,奇偶剪枝)
M - Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 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 ...
- HDOJ.1010 Tempter of the Bone (DFS)
Tempter of the Bone [从零开始DFS(1)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tem ...
- zoj 2110 Tempter of the Bone (dfs)
Tempter of the Bone Time Limit: 2 Seconds Memory Limit: 65536 KB The doggie found a bone in an ...
随机推荐
- SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...
- localforage indexedDB如何使用索引
简单介绍下localForage.localForage 是一个 JavaScript 库,通过简单类似 localStorage API 的异步存储来改进你的 Web 应用程序的离线体验.它能存储多 ...
- Python爬虫系统化学习(4)
Python爬虫系统化学习(4) 在之前的学习过程中,我们学习了如何爬取页面,对页面进行解析并且提取我们需要的数据. 在通过解析得到我们想要的数据后,最重要的步骤就是保存数据. 一般的数据存储方式有两 ...
- linux开启FTP服务
目录 打开FTP服务 客户端链接时会出现的问题 打开FTP服务 先ping,查看网络是否联通 打开ssh服务 查看一些服务的状态 #查看ssh状态 service sshd status #防火墙的状 ...
- 深入浅出新一代跨平台抓包&调式利器Fiddler Everywhere
什么是Fiddler Everywhere? Fiddler Everywhere is a web debugging proxy for macOS, Windows, and Linux. Ca ...
- (Java基础--Spring阶段)常见面试题题目及解析整理(2021.03.12)
题目整理 Java基础进阶阶段 基础概念类 1.JDK1.8新特性? 2.面向对象和面向过程的区别? 3.什么是值传递和引用传递? 4.什么是不可变对象? 5.讲讲类的实例化顺序? 6.java 创建 ...
- Memory Networks02 记忆网络经典论文
目录 1 Recurrent Entity Network Introduction 模型构建 Input Encoder Dynamic Memory Output Model 总结 2 hiera ...
- [SNOI2019] 通信
一.题目 点此看题 二.解法 一看就是傻逼补流模型,不会真的有人这个图都建不出来吧 别走啊,我不阴阳怪气了,如果你不知道怎么建这里有图嘛(思路来源是餐巾计划问题): 其中标红的边数量级很大,因为 \( ...
- 利用kali系统制作最简单的OS
制作最简单的OS 一.生成.bin文件 1.创建Boot.asm文件,输入相应的汇编代码 org 07c00h ; mov ax, cs mov ds, ax mov es, ax call Disp ...
- Python的web开发
一.Web开发 Tcp udp Cs即客户端.服务器端编程,客户端和服务器端之间需要使用socket,约定协议.版本(协议使用的是tcp或者udp).Tcp协议和udp协议,指定地址和 ...