HDU 2579 Dating with Girls (2) (BFS)

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): 2416 Accepted Submission (s): 690


Problem descriptionif You has 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.

Inputthe 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.
Outputfor cases, if can find the girl, output the least time in seconds, else output "Please give me another Chan Ce! ".
Sample Input
6 2...y.....#...#.......#.....# .... #G #.

Sample Output
7 Direct modulus Judgment
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue >using namespace Std;typedef long long ll;const int maxn = + 5;char fig[maxn][maxn];bool vis[maxn][maxn][15];int DX    [] = {1,0,-1,0};int dy[] = {0,1,0,-1};int R, C, k;int mintime;struct Point {int x, y, time;    Point (int x,int y, int time): x (x), Y (y), time (time) {}};void BFS (int x,int y) {queue<point>que;    Que.push (Point (x, y, 0));        while (! Que.empty ()) {Point e = Que.front ();        Que.pop ();        if (vis[e.x][e.y][e.time% K] | | e.x < 0 | | E.y < 0 | | e.x >= r | | e.y >= c) continue;        if (fig[e.x][e.y] = = ' # ') {if (e.time% k! = 0) Continue;        } vis[e.x][e.y][e.time% K] = true;            if (fig[e.x][e.y] = = ' G ') {mintime = E.time;        Return            } for (int i = 0; i < 4; i + +) {int NX = e.x + dx[i];            int NY = e.y + dy[i]; Que.push (Point (NX,NY, E.time + 1));    }}}void solve (int x,int y) {memset (Vis, false,sizeof (vis));    Mintime =-1;    BFS (x, y);    if (Mintime = =-1) {printf ("Please give me another chance!\n");    } else {printf ("%d\n", mintime);    }}int Main () {int t,ix,iy;    scanf ("%d", &t);        while (T--) {scanf ("%d%d%d", &r, &c, &k);            for (int i = 0; i < R; i + +) {scanf ("%s", Fig[i]);                    for (int j = 0; J < C; j + +) {if (fig[i][j] = = ' Y ') {IX = i;                iy = j;    }}} solve (Ix,iy); }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2579 Dating with Girls (2) (BFS)

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.