4.2.8 Dating with girls(2)

Problem Description
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.
 

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

思路:bfs,但是可以重复,我搞的什么特判啊,慢的很简直是,但还是1A了

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

struct qq
{
	int x,y;
};
queue<qq> q;
qq s,e;

const int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}},maxn=110;
int t,n,m,step,k,ax,ay,len,tot,cnt;
char map[maxn][maxn];
bool vis[maxn][maxn],vj[maxn][maxn],flag;

void judge()
{
for (int j=0;j<4;j++)
 {
	e.x=s.x+d[j][0];
   e.y=s.y+d[j][1];
	if (e.x<n && e.x>=0 && e.y<m && e.y>=0)
		if (vis[e.x][e.y]==false)
		{
			if (map[e.x][e.y]=='G') flag=true;
			if (map[e.x][e.y]=='.' || map[e.x][e.y]=='Y')
			{
				vis[e.x][e.y]=true;
				q.push(e);
				continue;
			}
			if (map[e.x][e.y]=='#' && step%k==0) //是#,但是是k的倍数,所以能走上去
			{
				if (vj[e.x][e.y]==false) 
				{
					cnt++;
					vj[e.x][e.y]=true;
				}
				vis[e.x][e.y]=true;
			   q.push(e);
				continue;
			}
		}
 }
}

void bfs()
{
while (!q.empty())
q.pop();
   s.x=ax; s.y=ay; flag=false; 
	cnt=0;
	q.push(s);
    while (!q.empty() && step<=60000 && cnt<=tot)	
	 {
		 memset(vis,false,sizeof(vis));
		 step++;
		 len=q.size();
	//	 printf("-----------------------------\n");
	//	 printf("STEP:%d len:%d\n",step,len);
		 for (int i=0;i<len;i++)
		 {
		    s=q.front();
		//	 printf("x:%d y:%d\n",s.x,s.y);
		    q.pop();
			 judge();
			 if (flag==true)
			 {
	//			 printf("stttttttttttttttttteeeeeeeeeeeep:%d\n",step);
				 printf("%d\n",step);
				 return;
			 }
		 }
	 }
//printf("###############################\nPlease give me another chance!\n");
printf("Please give me another chance!\n");
}


void init()
{
  scanf("%d",&t);
  while (t--)
  {
	  scanf("%d%d%d",&n,&m,&k);
	  tot=0;
	  memset(vj,false,sizeof(vj));
	  memset(map,'\0',sizeof(map));
	  for (int i=0;i<n;i++)
	  {
		  scanf("%s",map[i]);
		  for (int j=0;j<m;j++)
		  {
			  if (map[i][j]=='Y')
			  {
				  ax=i;
				  ay=j;
			  }
			  if (map[i][j]=='#') tot++;
		  }

	  }
	  step=0;
	  bfs();
//	  printf("step:%d\n",step);
  }
}

int main ()
{
init();
return 0;
}
原文地址:https://www.cnblogs.com/cssystem/p/2827294.html