Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2664 Accepted Submission(s): 745
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/dd753a737c8418c3a103c1d206c17fec.png)
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.
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
恩,题目大意就是说,相当于一迷宫,' Y '为目前所在位置,' G '为目的地,然后' # '是石头不可走的,但是当时间 t 为 k 的整数倍时,石头会消失在这一刻可以走,然后问是否能走到目的地,以及走到目的地的最短时间。恩,没想到此刻的石头再下一次走到时可能是可以走的,然后这次走过的这种情况要标记一下。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 110
using namespace std;
int t,r,c,k,sx,sy,ex,ey;
char mazz[maxn][maxn];
bool vis[maxn][maxn][11];//当遇到'#'时,判断t%k,要标记这种情况已走过
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node
{
int x,y,t;
friend bool operator < (node a,node b)
{
return a.t > b.t;
}
};
int judge(node n)
{
if(n.x<0||n.x>=r||n.y<0||n.y>=c)
return 0;
if(mazz[n.x][n.y]=='#'&&(n.t%k))
return 0;
if(vis[n.x][n.y][n.t%k])
return 0;
return 1;
}
int bfs()
{
node ss,now,next;
priority_queue<node>q;
while(!q.empty())
q.pop();
ss.x=sx;
ss.y=sy;
ss.t=0;
q.push(ss);
vis[sx][sy][0]=true;
while(!q.empty())
{
now=q.top();
q.pop();
if(now.x==ex&&now.y==ey)
return now.t;
for(int i=0;i<4;++i)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.t=now.t+1;
if(judge(next))
{
vis[next.x][next.y][next.t%k]=true;
q.push(next);
}
}
}
return 0;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&r,&c,&k);
memset(vis,false,sizeof(vis));
for(int i=0;i<r;++i)
{
getchar();
for(int j=0;j<c;++j)
{
scanf("%c",&mazz[i][j]);
if(mazz[i][j]=='Y')
sx=i,sy=j;
if(mazz[i][j]=='G')
ex=i,ey=j;
}
}
int tem=bfs();
if(tem)
printf("%d\n",tem);
else
printf("Please give me another chance!\n");
}
return 0;
}