Dating with girls(2)
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
思路:这也是一道广搜题,男孩找到女孩才可以约会,这中间有门,当时间为k的倍数时们才可以消失,男孩才可以通过。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int r,c,k,f;
int ne[4][2]={1,0,0,1,-1,0,0,-1};
int book[110][110][20];
char map[110][110];
struct node
{
int x,y,step;
};
void bfs(int x,int y)
{
queue<node>q;
node st,en;
st.x=x;
st.y=y;
st.step=0;
book[x][y][st.step%k]=1;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(map[st.x][st.y]=='G')
{
f=st.step;
return ;
}
for(int i=0;i<4;i++)
{
int tx=st.x+ne[i][0];
int ty=st.y+ne[i][1];
en.step=st.step+1;
if(tx<0||ty<0||tx>=r||ty>=c||book[tx][ty][en.step%k])
continue;
if(map[tx][ty]=='#'&&en.step%k)
continue;
book[tx][ty][en.step%k]=1;
en.x=tx;
en.y=ty;
q.push(en);
}
}
return ;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int i,j;
f=-1;
scanf("%d%d%d",&r,&c,&k);
memset(book,0,sizeof(book));
for(i=0;i<r;i++)
scanf("%s",map[i]);
int x1,y1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(map[i][j]=='Y')
{
x1=i;y1=j;
break;
}
}
}
bfs(x1,y1);
if(f==-1)
printf("Please give me another chance!\n");
else
printf("%d\n",f);
}
}
上一篇: Dating with girls(2)
下一篇: Dating with girls(2)
推荐阅读
-
Symfony2实现在controller中获取url的方法_php实例
-
css图片响应式+垂直水平居中2_html/css_WEB-ITnose
-
全面分析B2B网站SEO优化实战经验分享
-
PHP三元运算的2种写法代码实例_PHP教程
-
popViewController 之 同时pop掉2层viewController
-
Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
-
搜索与图论2
-
yii2 数据库插入操作怎么传入一个数组
-
yii2组件之多图上传插件FileInput的详细使用,yii2fileinput
-
db2admin用户密码最好不要修改_MySQL