Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 99346 Accepted Submission(s): 26930
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.
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.
'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 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
题目大意:给定一个m*n的迷宫,判断一只狗能不能刚好用t秒走出来。
解题思路:一看到这个题目,首先第一个想到的就是DFS,直接写了一个,不出意料,超时。
然后在网上看大神们的解题报告,发现涉及到一个叫奇偶剪枝的东西,看代码看了半天都不知道说的什么。
最后参考下面两个,终于弄懂了。
http://baike.baidu.com/link?url=7TWb3lZ0SOCF9AJndSzptf3j1Q9rMUMiMUspITLDSjLsMW6eFs9aCIBVEMvrEgJ24YPXmpUQLxx0oLoyQ3mbQ_
http://blog.csdn.net/libin56842/article/details/8962512
接着参考上面的大神的代码自己写了一个(说是参考,但是写出来几乎完全一样 T T):
这是第一次自己写的,超时:
#include <iostream>
using namespace std;
int m, n, t, dx, dy, flag, sx, sy;
char map[10][10];
void dfs(int x, int y, int l)
{
int i, j;
if (x < 0 || x >= m || y < 0 || y >= n) return;
if (x == dx&&y == dy && l == t)
flag = 1;
if (flag) return;
int s1 = dy > y ? dy - y : y - dy;
int s2 = dx > x ? dx - x : x - dx;
if (t - l < s1 + s2 || (t - l - s1 - s2) % 2 == 1) return;
for (i = -1; i <= 1; i++)
for (j = -1; j <= 1; j++)
if(i * j == 0 && (i != 0 || j != 0))
if (map[x + i][y + j] && map[x + i][y + j] != 'X')
{
map[x + i][y + j] = 'X';
dfs(x + i, y + j, l + 1);
map[x + i][y + j] = '.';
}
}
int main()
{
int i ,j, wall;
while (cin >> m >> n >> t)
{
flag = 0;//每一次运算首先将标记置零
if (!m && !n && !t) break;
wall = 0;//墙的数量置零
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> map[i][j];
if (map[i][j] == 'X') wall++;
if (map[i][j] == 'D')
{
dx = i;
dy = j;
}
if (map[i][j] == 'S')
{
sx = i;
sy = j;
map[i][j] = 'X';
}
}
map[i][j] == '\0';
getchar();
}
if (m*n - wall < t ) //如果空地的数量小于墙的数量,输出NO,
{
cout << "NO\n";
continue;
}
dfs(sx, sy, 0);
if (flag) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
交上去,超时,然后改成和楼上博主的一样,AC。
不知为何,感觉这两个循环应该没差多少。。。。还好过了。。
#include <iostream>
using namespace std;
int m, n, t, dx, dy, flag, sx, sy;
char map[10][10];
int add[4][2] = { { 0,-1 },{ 0,1 },{ -1,0 },{ 1,0 } };
void dfs(int x, int y, int l)
{
int i, j;
if (x < 0 || x >= m || y < 0 || y >= n) return;
if (x == dx&&y == dy && l == t)
flag = 1;
if (flag) return;
int s1 = dy > y ? dy - y : y - dy;
int s2 = dx > x ? dx - x : x - dx;
if (t - l < s1 + s2 || (t - l - s1 - s2) % 2 == 1) return;
for (i = 0; i < 4; i++)
{
if (map[x + add[i][0]][y + add[i][1]] && map[x + add[i][0]][y + add[i][1]] != 'X')
{
map[x + add[i][0]][y + add[i][1]] = 'X';//走过的地方变为墙
dfs(x + add[i][0], y + add[i][1], l + 1);
map[x + add[i][0]][y + add[i][1]] = '.';//迷宫还原
}
}
}
int main()
{
int i ,j, wall;
while (cin >> m >> n >> t)
{
flag = 0;//每一次运算首先将标记置零
if (!m && !n && !t) break;
wall = 0;//墙的数量置零
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> map[i][j];
if (map[i][j] == 'X') wall++;
if (map[i][j] == 'D')
{
dx = i;
dy = j;
}
if (map[i][j] == 'S')
{
sx = i;
sy = j;
map[i][j] = 'X';
}
}
map[i][j] == '\0';
getchar();
}
if (m*n - wall < t ) //如果空地的数量小于墙的数量,输出NO,
{
cout << "NO\n";
continue;
}
dfs(sx, sy, 0);
if (flag) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}