Dating with girls(2)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2707 Accepted Submission(s): 761
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.
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
Source
思路:
这道题和之前做的题的区别就在于石头会消失,那么也就是在需要用vis不仅要标记位置是否走过,而且要将时间和位置同时标记,而且在mp等于#的时候如果时间能够整除k,那么就能过!其他的和之前做的bfs一模一样!
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
int r,c,k;
char mp[105][105];
int vis[105][105][15];
int x,y,ex,ey;
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int ans;
struct node
{
int x,y,step;
}a,temp;
int judge()
{
if(temp.x<1||temp.x>r) return 0;
if(temp.y<1||temp.y>c) return 0;
if(mp[temp.x][temp.y]=='#'&&temp.step%k!=0) return 0;
if(vis[temp.x][temp.y][temp.step%k]==1) return 0;
if(temp.step>=ans) return 0;
}
void bfs()
{
a.x=x;
a.y=y;
a.step=0;
queue<node>q;
q.push(a);
memset(vis,0,sizeof(vis));
vis[x][y][0]=1;
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0;i<4;i++)
{
temp.x=a.x+dx[i];
temp.y=a.y+dy[i];
temp.step=a.step+1;
if(judge())
{
if(temp.x==ex&&temp.y==ey)
{
if(ans>temp.step)
{
ans=temp.step;
}
continue;
}
q.push(temp);
vis[temp.x][temp.y][temp.step%k]=1;
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&r,&c,&k);
getchar();
for(int i=1;i<=r;i++)
{
scanf("%s",mp[i]+1);
for(int j=1;j<=c;j++)
{
if(mp[i][j]=='Y')
{
x=i;
y=j;
}
else if(mp[i][j]=='G')
{
ex=i;
ey=j;
}
}
}
ans=INF;
bfs();
if(ans==INF)
{
printf("Please give me another chance!\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}