Hdoj 2579 Dating with girls (2) "bfs&& three-dimensional array marker"

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): 2656 Accepted Submission (s): 741


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 analysis: BFS plus tag band backtracking. Code:
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cstdlib


> Using namespace std;
int dis[4][2]={1,0,-1,0,0,1,0,-1};
int vis[110][110][110];
Char map[110][110];
	struct node{int x;
	int y;
int step;
}a,b;
int Sx,sy,ex,ey;

int r,c,k;
	void BFs () {memset (vis,0,sizeof (VIS));
	queue<node>q;
	A.x=sx;a.y=sy;a.step=0;
	Q.push (a);
		while (!q.empty ()) {A=q.front ();
		Q.pop ();
			if (map[a.x][a.y]== ' G ') {printf ("%d\n", a.step);
		Return
	     	} for (int i=0;i<4;i++) {b.x=a.x+dis[i][0];
	     	B.Y=A.Y+DIS[I][1];
	     	b.step=a.step+1;
	     	int s=b.step%k; if (B.x<r&&b.x>=0&&b.y<c&&b.y>=0&&!vis[b.x][b.y][s]) {vis[b.x][
	     		B.y][s]=1;
	     		if (map[b.x][b.y]== ' # ' &&s) continue;
	     	Q.push (b);
}}} printf ("Please give me another chance!\n");
	} int main () {int t;
	scanf ("%d", &t); while (t--) {scanf ("%d%d%d ", &r,&c,&k);
		for (int i=0;i<r;i++) scanf ("%s", Map[i]);
		  	for (int i=0;i<r;i++) for (int j=0;j<c;j++) {if (map[i][j]== ' Y ') sx=i,sy=j;
		  if (map[i][j]== ' G ') ex=i,ey=j; 
	} BFS ();
} 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.