HDU1010 dfs奇偶剪枝

来源:互联网 发布:java政府驻场开发 编辑:程序博客网 时间:2024/05/16 19:05

Tempter of the Bone

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

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 5
S.X.
..X.
..XD
….
3 4 5
S.X.
..X.
…D
0 0 0

Sample Output
NO
YES

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1010

题意

给定NxM的矩阵和时刻K,矩阵中'S'代表起点,'D'代表终点,X不可走,'.'可以行走,但不能重复走,问从'S'出发,是否能恰好在时刻K时到达'D'。

题解

dfs回溯+奇偶剪枝。关键:    网格中两点的所有可行路径,长必定同奇偶。    根据这一信息,在求解过程中比较    当前点(r, c)到终点(bx, by)之间的最短路abs(bx-r)+abs(by-c)    和    当前还必须要走的路径长度(也就是当前剩余时间)k-cnt    的奇偶性,奇偶性不同时可以剪枝。    判断奇偶性相同用两路径之差&1(或%2)==1即可判断。注意:    (1)有终点的dfs中要用flag标记是否已经到达终点    (2)vis数组记得清零!!!
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#include <math.h>#include <stack>#include <queue>#include <vector>#define INF 0x3f3f3f3f using namespace std;const int maxn = 10;int used[maxn][maxn];char pic[maxn][maxn];int m, n, k;int flag;int ax, ay, bx, by;int dr[4] = {-1,0,1,0};int dc[4] = {0,1,0,-1};void dfs(int r, int c, int cnt){    //flag,判定是否已经到达终点了    if(flag || cnt>k || r<0 || r>=m || c<0 || c>=n) return;    if(bx == r && by == c && cnt == k){        flag = 1;        return;    }    //奇偶剪枝    int temp = abs(bx-r)+abs(by-c);    temp = k-cnt-temp;    if(temp&1) return;    for(int i=0; i<4; i++){        int r1 = r+dr[i];        int c1 = c+dc[i];        if(!used[r1][c1] && pic[r1][c1]!='X'){            used[r1][c1] = 1;            dfs(r1, c1, cnt+1);            used[r1][c1] = 0;        }    }}int main(){    while(scanf("%d%d%d", &m, &n, &k) && (m||n||k)){        memset(used, 0, sizeof(used));        for(int i=0;i<m;i++){            scanf("%s", pic[i]);            for(int j=0;j<n; j++){                if(pic[i][j] == 'S'){                    ax = i;                    ay = j;                    used[ax][ay] = 1;                }                if(pic[i][j] == 'D'){                    bx = i;                    by = j;                }            }        }        int cnt = 0;        flag = 0;        dfs(ax, ay, cnt);        if(flag){            printf("YES\n");        }        else{            printf("NO\n");        }    }    return 0;} 
0 0
原创粉丝点击