Tempter of the Bone(DFS + 奇偶剪枝,好题)

来源:互联网 发布:linux telnet命令安装 编辑:程序博客网 时间:2024/04/30 14:04

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 39780    Accepted Submission(s): 10761


原题链接:点击打开链接
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 5S.X...X....D0 0 0
 

Sample Output
NOYES
 

Author
ZHANG, Zheng
 

Source
ZJCPC2004
 

Recommend
JGShining
 
这题是求在规定时间到达出口是否可能,注意不是“规定时间内”!
使用DFS是显然的,当然网上也有人用BFS搞出来了,不过主流还是深搜。这题的关键在于剪枝,不然会超时。剪枝有三个方面:
(1)统计X的个数cnt,因为走一步用一秒,所以T秒就是T步,如果N * M - cnt < T的话,可以到达的点的数量比走的步数少,则不可能到达出口。这个剪枝在这道题可以省掉很多时间(当然如果换一批测试数据效果应该就不一样了)。
(2)第二个是很关键的奇偶剪枝。
假设在某一时刻t,狗狗走到P(xi, yi),又记终点为D(x0, y0)。由P到D的路径可以分解为水平方向和竖直方向(类似于高中物理的位移的正交分解),就是说将P到D的每一条可行路径分解为从直线x = xi到达x = x0,和由直线y = yi到直线y = y0的两个分路径。现在考虑由x = xi到x = x0,当x0 - xi为偶数时,说明x0和xi的奇偶性相同,那么无论通过怎么样的路径由x = xi到x = x0,路径的步数总和必定为偶数(因为每走一步所处方位的x坐标奇偶性就变一次),不然不可能到达x0,,同理,当x0 - xi为奇数时,无论走什么路径,步数总和必为奇数。考查由xi到x0之后,yi到y0的情况不言而知。设由xi到x0的步数为tx,由yi到y0的步数为ty,则tx和ty的奇偶性分别与abs(xi - x0)和abs(yi - y0)的相同,所以总步数tx + ty的奇偶性和abs(xi - x0) + abs(yi - y0)的奇偶性相同。注意,总步数tx + ty实际上就是在P点时剩余的时间t。由此可知,由P到D的总步数,也就是剩余的时间t的奇偶性必定与abs(xi - x0) + abs(yi - y0)的奇偶性相同。不然就无法从P到达D。
(3)最短路径剪枝。
由某一点P(xi, yi)到终点D(x0, y0),最短路径恰恰是上面提到的abs(xi - x0) + abs(yi - y0),这也就是由P到D所需的最短时间,如果它大于当前剩余的时间,就不能到达D。

这题大概还有一个很恶心的地方是,测试数据可能会有多余的空格或换行使得就算用scanf("%c", &c)吃掉一个换行还是会WA,我原来一直WA,拿来一个AC的程序测试了很多例子结果都是ok的,跟AC程序唯一的不同就是它把迷宫的每一行当成字符串输入,这样就能忽略每一行后多余的空格和换行符,而我的只是用scanf吃掉每一行的换行符。一改输入方式立马AC。

AC CODE:
#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define LL long long#define MAXI 2147483647#define MAXL 9223372036854775807#define eps (1e-8)#define dg(i) cout << "*" << i << endl;using namespace std;struct Point{    int x, y;}s, d; //源点,终点char map[8][8]; //迷宫地图int N, M;int move[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; //方位移动int jiou(int i, int j){    return abs(i - d.y) + abs(j - d.x);}bool DFS(int i, int j, int t){    if(!t) return 0; //时间用完    int a = jiou(i, j);    if((a & 1) != (t & 1) || a > t) return 0; //不能通过奇偶检验或所需时间大于剩余时间    int ii, jj, k;    for(k = 0; k < 4; k++)    {        ii = i + move[k][0];        jj = j + move[k][1];        if(ii > -1 && ii < N && jj > -1 && jj < M && map[ii][jj] != 'X')        {            if(map[ii][jj] == 'D' && t == 1)            {                return 1;            }            if(map[ii][jj] == '.')            {                map[ii][jj] = 'X';                if(DFS(ii, jj, t - 1)) return 1;                map[ii][jj] = '.';            }        }    }    return 0;}int main(){    int i, j, T, cnt; //cnt统计X的个数    char c;    while(scanf("%d %d %d", &N, &M, &T) && N && M && T)    {        cnt = 1;        for(i = 0; i < N; i++)        {            scanf("%s", map[i]);        }        for(i = 0; i < N; i++)        {            for(j = 0; j < M; j++)            {                if(map[i][j] == 'X')                {                    cnt++;                }                if(map[i][j] == 'S')                {                    s.x = j; s.y = i;                    map[i][j] = 'X';                }                else if(map[i][j] == 'D')                {                    d.x = j; d.y = i;                }            }        }        if(N * M - cnt < T || !DFS(s.y, s.x, T)) puts("NO");        else puts("YES");    }    return 0;}



原创粉丝点击