"Hdu" Dating with Girls (2) (BFS, note visit[i][j][k], can repeat one position)

Source: Internet
Author: User


Dating with Girls (2) Time limit:2000/1000ms (java/other) Memory limit:32768/32768k (java/other) Total Submission (s): Accepted submission (s): 4Problem 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

664.cpp: Defines the entry point of the console application. #include <cstdio> #include <queue> #define INF 0x3f3f3f3f #define MAX #define MEM (arr,w) memset (arr,w,s
Izeof (arr)) using namespace Std; struct node {int x, y;}
St,ed,ss,tt;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int n,m,k,maxt;
Char Map[max][max];
BOOL VISIT[MAX][MAX][15];
int Step[max][max];
	BOOL Judge (node TT) {if (tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m) return 1;
return 0;
	} void Init () {MEM (visit,false);
	MEM (step,0);
Maxt=inf;
	} void BFS () {queue<node> Q;
	Q.push (ST); while (!
		Q.empty ()) {Ss=q.front ();
		Q.pop ();
		if (SS.X==ED.X&AMP;&AMP;SS.Y==ED.Y) if (STEP[SS.X][SS.Y]&LT;MAXT) maxt=step[ss.x][ss.y];
			for (int i=0;i<4;i++) {tt.x=ss.x+dir[i][0];
			TT.Y=SS.Y+DIR[I][1];
			step[tt.x][tt.y]=step[ss.x][ss.y]+1;
			int d=step[tt.x][tt.y]%k; if (judge (TT) &&!visit[tt.x][tt.y][d]) {if (map[tt.x][tt.y]== ' # ' &&d) {visit[tt.x][tt.y][d]
				=true;continue;
			}	Visit[tt.x][tt.y][d]=true;
			Q.push (TT);
	}}}} int main () {int t,i,j;
	scanf ("%d", &t);
		while (t--) {scanf ("%d%d%d", &n,&m,&k);
			for (i=0;i<n;i++) {scanf ("%s", Map[i]);
				for (j=0;j<m;j++) {if (map[i][j]== ' Y ') st.x=i,st.y=j;
			if (map[i][j]== ' G ') ed.x=i,ed.y=j;
		}} Init ();
		BFS ();
		if (maxt!=inf) printf ("%d\n", maxt);
	Else puts ("Please give me another chance!");
} 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.