hdu 2579 Dating with girls(2) 【经典三维BFS】

Dating with girls(2)

                                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                           Total Submission(s): 2443    Accepted Submission(s): 697

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2579
Problem Description
If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity! 
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there. 
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.
hdu 2579 Dating with girls(2) 【经典三维BFS】_第1张图片
 

Input
The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.
 

Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
 

Sample Input
   
   
   
   
1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
 

Sample Output
   
   
   
   
7
 

题意: 

给定一个R*C的图,求起点到终点的最小步数。要求不能经过障碍物,障碍物在K的整数倍的时候会暂时消失。

分析:

简单三维的BFS,这个题目因为要判断步数是不是K的整数倍。当下一步的某个位置为障碍物的时候,我们不能直接跳过这个障碍物,因为存在先到其他点,然后在K的整数倍的时候刚好到达这个点的情况,也就是样例的情况。由此可见,必须要三维才能准确地描述状态。另开一维用来描述(i, j)这个点在对K取模这一状态是否被访问过即可。

代码实现:

#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define FIN     freopen("input.txt","r",stdin)
#define CASE(T) int T;for(scanf("%d",&T);T--;)
const int maxn = 100 + 5;
const int INF = 0x3f3f3f3f;
const int dir[4][2] = { -1,0, 1,0, 0,1, 0,-1 };
int R, C, K, ans;
char Map[maxn][maxn];
bool vis[maxn][maxn][15];
struct Node
{
    int x, y, step;
    Node() {}
    Node(int _x, int _y, int _s) : x(_x), y(_y), step(_s) { }
} Now;
queue<Node> Que;
void Read_Graph();
void Solve();
int main()
{
//    FIN;
    CASE(T)
    {
        Read_Graph();
        Solve();
    }
    return 0;
}
void Read_Graph()
{
    scanf("%d %d %d", &R, &C, &K);
    for(int i = 0; i < R; i++)
    {
        scanf("%s", Map[i]);
        for(int j = 0; j < C; j++)
        {
            if(Map[i][j] == 'Y')
                Now = Node(i, j, 0);
        }
    }
}
inline bool InRange(int x, int y)
{
    return x >= 0 && x < R && y >= 0 && y < C;
}
void Solve()
{
    ans = INF;
    memset(vis, false, sizeof(vis));
    Que.push(Now);
    vis[Now.x][Now.y][0] = true;
    while(!Que.empty())
    {
        Now = Que.front();
        Que.pop();
        if(Map[Now.x][Now.y] == 'G')
        {
            ans = min(ans, Now.step);
            continue;
        }
        for(int i = 0; i < 4; i++)
        {
            int xx = Now.x + dir[i][0], yy = Now.y + dir[i][1], ss = Now.step + 1;
            if(InRange(xx, yy) && !vis[xx][yy][ss % K] && ss <= ans)
            {
                if(Map[xx][yy] == '#' && ss % K != 0) continue;
                Que.push(Node(xx, yy, ss));
                vis[xx][yy][ss % K] = true;
            }
        }
    }
    if(ans >= INF)
        printf("Please give me another chance!\n");
    else printf("%d\n", ans);
}

你可能感兴趣的:(三维,bfs)