Hdu 2579 Dating with Girls (2) (BFS)

Source: Internet
Author: User
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 Simple BFS
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> #define INF 1e9
using namespace Std;
Char maze[110][110];
int n,m,k; int Sx,sy,ex,ey;///sx,sy Record starting point, Ex,ey record end int ans[110][110][12];///ans[i][j][p] means "the time required to reach (i,j) position the remainder of the K-surplus is P" in this state,
The minimum time required to reach this state is the struct node {int x,y,time;};
int f[4][2]={1,0,-1,0,0,1,0,-1};
    int BFs () {queue<node> q;
    Node Now,next;
    now.x=sx,now.y=sy,now.time=0;
    Q.push (now); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) for (int p=0;p<=k;p++) ans[i][j][p]=in
    F
    ans[sx][sy][0]=0;
        while (Q.size ()) {Now=q.front ();
        Q.pop ();
            if (Now.x==ex&&now.y==ey) return now.time;///because it is BFS, so the first time to reach the end is definitely the least time for (int i=0;i<4;i++) {
            int mx=now.x+f[i][0];
            int my=now.y+f[i][1]; if (mx<=0| | mx>n| | my<=0| |
            MY&GT;M) continue;
            Next.x=mx,next.y=my; Next.time=now.time+1;
            int kk=next.time%k;
                if (maze[mx][my]!= ' # ' &&ans[mx][my][kk]>next.time) {ans[mx][my][kk]=next.time;
            Q.push (next); } else if (maze[mx][my]== ' # ' &&kk==0&&ans[mx][my][kk]>next.time)///This position is a stone, then you should see the total time here is not K's
                Multiples, is the word stone will disappear, can walk {ans[mx][my][kk]=next.time;
            Q.push (next);
}}} return-1;
    } int main () {int T;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &n,&m,&k);
            for (int i=1;i<=n;i++) {scanf ("%s", maze[i]+1);
                    for (int j=1;j<=m;j++) {if (maze[i][j]== ' Y ') {sx=i;
                Sy=j;
                    } if (maze[i][j]== ' G ') {ex=i;
                Ey=j;
   }
            }
        }     int Res=bfs ();
        if (Res==-1) puts ("Please give me another chance!");
    else printf ("%d\n", res);
} 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.