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
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
char s[110][110];
int vis[110][110][20];
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int n,m,tt,tx,ty;
struct node
{
int x;
int y;
int step;
};
void bfs()
{
memset(vis,0,sizeof(vis));
queue<node>Q;
node now,next;
now.x=tx;
now.y=ty;
now.step=0;
Q.push(now);
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(s[now.x][now.y]=='G')//如果找到就输出走的步数
{
cout<<now.step<<endl;
return ;
}
for(int i=0; i<4; i++)
{
next.x=now.x+dir[i][0];//下一个位置坐标
next.y=now.y+dir[i][1];
next.step=now.step+1;
int num=next.step%tt;
if(next.x<n&&next.x>=0&&next.y<m&&next.y>=0&&!vis[next.x][next.y][num])//判断是否出界和是否走过
{
vis[next.x][next.y][num]=1;
if(s[next.x][next.y]=='#'&&num)
continue;
Q.push(next);
}
}
}
cout<<"Please give me another chance!"<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m>>tt;
for(int i=0; i<n; i++)
cin>>s[i];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(s[i][j]=='Y')//标记Y的位置
{
tx=i;
ty=j;
}
}
}
bfs();//广搜
}
return 0;
}
上一篇: python基础学习(字符串、列表、元组、字典、集合)
下一篇: 详解事务的传播
推荐阅读
-
4999元起 小米10 Pro首销战绩公布:55秒全平台销售额突破2亿元
-
TCP-IP详解 卷2 :实现 pdf下载
-
[20181124]关于降序索引问题2.txt
-
微信公众号内测文章“打赏”功能 小费最低2元
-
SpringBoot2.x+Redis+nginx实现session共享和负载均衡
-
SSH开发模式——Struts2(第二小节)
-
SSH开发模式——Struts2(第一小节)
-
三星TabPro S2曝光:12英寸显示屏+7代i5
-
惠普暗影精灵II代Pro怎么样?惠普暗影精灵2pro游戏性能详细评测图解
-
Spring Boot Security OAuth2 实现支持JWT令牌的授权服务器