问题描述:
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
大致题意:
Y是起点,G是终点。#是石头。会在时间%k等于0的时候消失,不等于0的时候出现。求最短时间。
其实题意挺浪漫的,原谅我翻译的那么简短。
思路分析:
因为石头会出现和消失,所以有的路可以重复走。
那么我们就要开一个3维的数组来记录,有没有经过。
vis[i][x][y].表示余数为i时,点(x,y)有没有走过。
其他的地方就很是常规的bfs姿势了。
ac代码:
#include<bits/stdc++.h>
using namespace std;
int T,n,m,k,ys,yy,gx,gy;
char mapp[105][105];
bool vis[12][105][105];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct Node
{
int x,y;
int step;
};
bool ok(int x,int y,int t)
{
if(x>=0&&x<n&&y>=0&&y<m&&(mapp[x][y]!='#'||t%k==0)&&!vis[t%k][x][y])
return 1;
return 0;
}
void bfs()
{
queue<Node> Q;
memset(vis,0,sizeof(vis));
Node now,nextt;
now.x=ys;
now.y=yy;
now.step=0;
Q.push(now);
vis[0][ys][yy]=1;
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(now.x==gx && now.y==gy)
{
cout<<now.step<<endl;
return ;
}
for(int i=0;i<4;i++)
{
nextt.x=now.x+dx[i];
nextt.y=now.y+dy[i];
nextt.step=now.step+1;
if(ok(nextt.x,nextt.y,nextt.step))
{
vis[nextt.step%k][nextt.x][nextt.y]=1;
Q.push(nextt);
}
}
}
cout<<"Please give me another chance!"<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--)
{
int i,j;
cin>>n>>m>>k;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>mapp[i][j];
if(mapp[i][j]=='Y')
{
ys=i;
yy=j;
}
if(mapp[i][j]=='G')
{
gx=i;
gy=j;
}
}
}
bfs();
}
}