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
题解:输入n,m,k,接下来nXm的地图,点(.)为路,Y男生位置,G女孩位置,#为墙,但是当此时已走时间%k==0时,#可以走,输出最小时间。
用不用优先队列都可以,注意时间和k的关系,以及此时走过的路下一时间可以再走的,所以用个三维数组来标记。
贴代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,k,flag;
int a,b,book[30][110][110];
char mp[110][110];
int nextt[4][2]= {1,0,0,1,-1,0,0,-1};
struct node
{
int x,y,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};//用不用优先队列都可以
void bfs()
{
priority_queue<node>q;
node st,ed;
st.x=a;
st.y=b;
st.step=0;
q.push(st);
while(!q.empty())
{
st=q.top();
q.pop();
ed=st;
if(mp[st.x][st.y]=='G')
{
flag=1;
printf("%d\n",st.step);
return ;
}
for(int i=0; i<4; i++)
{
int tx=st.x+nextt[i][0];
int ty=st.y+nextt[i][1];
int tp=st.step+1;
if(tx<0||ty<0||tx>=n||ty>=m)
continue;
if(tp%k==0)
{
if(book[tp%k][tx][ty]==0)//分清时间标记
{
book[tp%k][tx][ty]=1;
ed.x=tx;
ed.y=ty;
ed.step=tp;
q.push(ed);
}
}
else
{
if(mp[tx][ty]!='#'&&book[tp%k][tx][ty]==0)
{
book[tp%k][tx][ty]=1;
ed.x=tx;
ed.y=ty;
ed.step=tp;
q.push(ed);
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j;
memset(book,0,sizeof(book));
scanf("%d%d%d",&n,&m,&k);
for(i=0; i<n; i++)
{
scanf("%s",mp[i]);
for(j=0; j<m; j++)
if(mp[i][j]=='Y')
{
a=i;
b=j;
}
}
book[0][a][b]=1;
flag=0;
bfs();
if(flag==0)
printf("Please give me another chance!\n");
}
return 0;
}