Tempter of the Bone
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 7
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.<br><br>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.<br>
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:<br><br>‘X‘: a block of wall, which the doggie cannot
enter; <br>‘S‘: the start point of the doggie; <br>‘D‘: the Door;
or<br>‘.‘: an empty block.<br><br>The input is terminated with
three 0‘s. This test case is not to be processed.<br>
Output
For each test case, print in one line "YES" if the
doggie can survive, or "NO" otherwise.<br>
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
简单题意:
给出地图,问在规定的步数能否正好到达目标位置,
思路分析:
开始的时候不知道,能去掉一部分,剪枝的方法,一直超时, 去掉一部分就可以了,,bfs
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; int flag, sx, sy, dx, dy, num; int n, m, t; char map[10][10]; int is[10][10]; int Dx[4] = {-1,0,1,0}; int Dy[4] = {0,-1,0,1}; void dfs(int nowx,int nowy,int num) { int nextx, nexty; if(flag == 1) return; if(nowx == dx && nowy == dy && num == t) { flag = 1; return; } int min = abs(nowx - dx) + abs(nowy - dy); if(min > t - num || (min + t - num ) % 2 != 0) return; for(int i = 0; i < 4; i++) { nextx = nowx + Dx[i]; nexty = nowy + Dy[i]; if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && !is[nextx][nexty] && map[nextx][nexty] != ‘X‘) { is[nextx][nexty] = 1; dfs(nextx, nexty, num + 1); is[nextx][nexty] = 0; } } } int main() { //freopen("aaa.txt", "r", stdin); while(scanf("%d %d %d", &m, &n, &t)) { if(n ==0 && m == 0 && t == 0) break; int d = 0; for(int i = 0; i < m; i++) { scanf("%s", map[i]); for(int j = 0; j < n; j++) { if(map[i][j]==‘S‘) { sx = i; sy = j; } if(map[i][j] == ‘D‘) { dx = i; dy = j; } if(map[i][j] == ‘X‘) d++; } } if(n * m - num - 1 < t) { printf("NO\n"); continue; } flag = 0; memset(is, 0, sizeof(is)); is[sx][sy] = 1; dfs(sx, sy, 0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }