欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dating with girls(2)

程序员文章站 2022-07-14 23:03:39
...

If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is 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, then you can date with the girl.Else the girl will date with other boys. What a pity! 
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there. 
There are 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. 
Dating with girls(2)

Input

The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), 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 chance!".

Sample Input

1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.

Sample Output

7

思路:这也是一道广搜题,男孩找到女孩才可以约会,这中间有门,当时间为k的倍数时们才可以消失,男孩才可以通过。

#include<stdio.h> 
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

int r,c,k,f;
int ne[4][2]={1,0,0,1,-1,0,0,-1};
int book[110][110][20];
char map[110][110];

struct node
{
	int x,y,step;
};

void bfs(int x,int y)
{
	queue<node>q;
	node st,en;
	st.x=x;
	st.y=y;
	st.step=0;
	book[x][y][st.step%k]=1;
	q.push(st);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		if(map[st.x][st.y]=='G')
		   {
		   	f=st.step;
		   	return ;
		   }
		for(int i=0;i<4;i++)
		{
			int tx=st.x+ne[i][0];
			int ty=st.y+ne[i][1];
			en.step=st.step+1;
			if(tx<0||ty<0||tx>=r||ty>=c||book[tx][ty][en.step%k])
			       continue;
			if(map[tx][ty]=='#'&&en.step%k)
			   continue;
			book[tx][ty][en.step%k]=1;
			en.x=tx;
			en.y=ty;
			q.push(en);   
		}
	}
	return ;
}
int main()
{
	int n;
    scanf("%d",&n);
	while(n--)
	{
		int i,j;
		f=-1;
		scanf("%d%d%d",&r,&c,&k);
		memset(book,0,sizeof(book));
		for(i=0;i<r;i++)
		 scanf("%s",map[i]);
		 int x1,y1;
		for(i=0;i<r;i++)
		{
			for(j=0;j<c;j++)
			{
				if(map[i][j]=='Y')
				{
					x1=i;y1=j;
					break;
				 } 
			}
		}
		bfs(x1,y1);
		if(f==-1)
		 printf("Please give me another chance!\n");
		else
		printf("%d\n",f);
	}
}