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

题目描述

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.

思路

这题巨坑,题目要求就是在T秒的时候恰好找到出口。

我没处理起点,导致DFS会回到起点,WA了20次。。。

AC代码

/*************************************************************************
    > File Name: 1010_2.cpp
      > Author: Netcan
      > Blog: http://www.netcan.xyz
      > Mail: 1469709759@qq.com
      > Created Time: Fri 15 May 2015 20:16:10 PM CST
 ************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;

int N, M, T; // N, M迷宫宽高,T出口打开时间
char Map[10][10];
int direct[][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; // 定义探索的四个方向
bool yes = false; // yes为找到出口的标记
struct Point{
    int i, j;
};
Point S, D; // 起点和出口
void DFS(int i, int j, int cnt) { // 这里的i, j表示当前坐标,cnt为当前步数(1步/s)
    if(i>N || j>M || i<1 || j<1) // 当前坐标在迷宫范围之外则回溯
        return;
    if(cnt == T) { // 到达出口打开时间
        if(i==D.i && j==D.j) // 恰好在出口
            yes = true; // 能出去
        return;
    }
    if(yes) // 如果已经出去了,不必继续搜索
        return;
    int temp = T - cnt - (abs(i-D.i) + abs(j-D.j)); 
    if(temp < 0 || temp & 1) // 剩余步数若小于0或者为奇数则回溯,无法到达出口
        return;
    for(int k=0; k<4; ++k) {
        if(Map[i+direct[k][0]][j+direct[k][1]] != 'X') { // 可以探索
            Map[i+direct[k][0]][j+direct[k][1]] = 'X'; // 标记为已探索
            DFS(i+direct[k][0], j+direct[k][1], cnt+1); // 开始探索
            Map[i+direct[k][0]][j+direct[k][1]] = '.'; // 回溯,恢复探索状态
        }
    }
    return;
}

int main()
{
#ifdef OJ
    freopen("1010.data", "r", stdin);
#endif
    while(cin >> N >> M >> T) {
        if( N==0 && M==0 && T==0)
            break;
        int counts = 0;
        for(int i=1; i<=N; ++i) {
            for(int j=1; j<=M; ++j) {
                cin >> Map[i][j];
                if(Map[i][j] == 'S') {
                    S.i = i;
                    S.j = j;
                }
                else if(Map[i][j] == 'D') {
                    D.i = i;
                    D.j = j;
                }
                else if(Map[i][j] == 'X')
                    ++counts;
            }
        }
        if(M*N - counts <= T) { // 如果可走步数小于总步数T,则不可能。
            cout << "NO" << endl;
            continue;
        }
        yes = false;
        Map[S.i][S.j] = 'X'; // 这里一定要处理起点!不然会回到起点。。错了20次。。
        DFS(S.i, S.j, 0);
        if(yes)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;

    }
    return 0;
}