Hdoj 2579 Dating with Girls (2) (bfs+ three-dimensional marking good)

Source: Internet
Author: User
Dating with Girls (2) Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2659 Accepted Submission (s): 744


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 and the ordinary BFS is different, do not have to mark the open space to walk, but need to mark the repeat of the road when the first walk to A.step%k, Mark, the next time you go to a.step%k, you do not have to go, because the AC code will be repeated:
#include <stdio.h> #include <string.h> #include <math.h> #include <queue> #include <iostream
> #include <algorithm> #define MAXN, using namespace std;
    struct s {int x;
    int y;
    int step;
    friend BOOL operator< (s a,s b) {return a.step>b.step;
}
};
int V[MAXN][MAXN][MAXN];
Char MAP[MAXN][MAXN];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m,k,ans,bz;
    int check (s aa) {if (aa.x>=0&&aa.x<n&&aa.y>=0&&aa.y<m) return 0;
return 1;
    } void BFs (int xx,int yy) {memset (v,0,sizeof (v));
    priority_queue<s>q;
    s A, B;
    int i;
    A.x=xx;
    A.y=yy;
    A.step=0;
    V[0][xx][yy]=1;
    Q.push (a);
        while (!q.empty ()) {a=q.top ();
        Q.pop ();
            if (map[a.x][a.y]== ' G ') {ans=a.step;
            Bz=1;
        Return
            } for (i=0;i<4;i++) {b=a;
          B.X=B.X+DIR[I][0];  B.Y=B.Y+DIR[I][1];
            b.step=b.step+1;
            if (check (b)) continue; if (map[b.x][b.y]== ' # ') {if (b.step%k==0) {if (!v[b.step%k] [b.x]
                        [B.y]) {Q.push (b);
                    V[b.step%k][b.x][b.y]=1;
                }}} else {if (!v[b.step%k][b.x][b.y])
                    {Q.push (b);
                V[b.step%k][b.x][b.y]=1;
    }}}}} int main () {int t,c,d,j,i;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &n,&m,&k);
        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]== ' Y ')
                    {c=i;
             D=j;       Break
        }}} bz=0;
        BFS (C,D);
        if (BZ) printf ("%d\n", ans);
    else printf ("Please give me another chance!\n");
} return 0; }



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.