Hdu 2579 Dating with girls (2) "Classic three-dimensional BFS"

Source: Internet
Author: User
Tags bool

Dating with Girls (2) Time limit:2000/1000 MS (java/others) Memory limit:3276 8/32768 K (java/others)
Total submission (s): 2443 Accepted Submission (s): 697

Links: http://acm.hdu.edu.cn/showproblem.php?pid=2579
Problem Description If You had solved the problem Dating with girls (1). I Think you can solve this problem too. This problem was 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 and then you can date with the girl. Else the girl would date with other boys. What a pity!
The Maze is very strange. There is many stones in the maze. The stone'll disappear at time t if it's a multiple of K (2<= K <=), on the other time, stones'll be still th Ere.
There is 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.

Input The first line contain an integer T. Then T cases followed. Each case is begins with three integers r and C (1 <= R, c <=), 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 cha Nce! ".
Sample Input

1 6 6 2 ... Y ..... .#.... ...#.. ...#.. .. #G #.
Sample Output
7

Test Instructions:

Given a graph of r*c, the minimum number of steps from the start to the end. The obstacle is not allowed to pass through the obstacles, and the obstacle disappears temporarily when the integer times of K. Analysis:

Simple three-dimensional BFS, this topic because to determine whether the number of steps is not an integer multiples of K. When the next position is an obstacle, we cannot skip the obstacle directly, because there is a situation where the point is reached at the other point, and then at the integer times of K, which is the case of the sample. Thus, it is necessary to describe the state accurately in three dimensions. Another dimension is used to describe (I,&NBSP;J) Whether this point is accessed in the state of K modulo. code implementation:

#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);
 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.