acm之搜索题目3
2016-06-11 15:43
169 查看
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.
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 5S.X.
..X.
..XD
….
3 4 5
S.X.
..X.
…D
0 0 0
Sample Output
NOYES
代码:
#include <iostream> #include <fstream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int dx[] = {-1,0,1,0}; int dy[] = {0,1,0,-1}; int N,M,T; char map[10][10]; int Wall; int di,dj; int si,sj; bool is; void dfs(int si,int sj,int t) { if(si<1||sj<1||si>N||sj>M||t>T||is) return; if(si==di && sj==dj && t==T) { is=true; return; } if ((abs(di-si)+abs(dj-sj))%2!=(T-t)%2) return ; for(int i=0;i<4;i++) { if(map[si+dx[i]][sj+dy[i]]!='X') { map[si+dx[i]][sj+dy[i]]='X'; dfs(si+dx[i],sj+dy[i],t+1); map[si+dx[i]][sj+dy[i]]='.'; } } return; } int main() { while(~scanf("%d %d %d",&N,&M,&T) && N!=0) { Wall=0; for(int i=1;i<=N;i++) { for(int j=1;j<=M;j++) { scanf(" %c",&map[i][j]); if(map[i][j]=='S') { si=i;sj=j; } if(map[i][j]=='X') Wall++; if(map[i][j]=='D') { di=i;dj=j; } } } is=false; if(N*M-Wall<T) is=false; else { map[si][sj]='X'; dfs(si,sj,0); } if(is) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- Remove Duplicates from Sorted List
- 常见异常
- 浅谈SSL与SSH以及各层安全协议
- 如何来看单片机外设A/D转换器ADC0804时序图
- 如何来看单片机外设A/D转换器ADC0804时序图
- 如何来看单片机外设A/D转换器ADC0804时序图
- android 自定义View基础(2)
- mysql中AVG()函数
- Codevs 1140 Jam的计数法 2006年NOIP全国联赛普及组
- ACdream 1216 Beautiful People 【双序列LIS输出下标】
- Ubuntu installed, something should be done...
- 百度天气API使用测试
- Linux下的inode记录
- gem5 - Moby: A Mobile Benchmark Suite for Architectural Simulators
- acm之搜索题目2
- Ubuntu 15.04 下apt-get安装JDK
- [转]libsvm 训练后的模型参数讲解
- Linux - kill命令
- Ubuntu下apt-get安装Java,Tomcat
- Systemd入门教程:命令篇