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的倍数,障碍物会消失
思路:简单的广搜。
开个三维数组,第三维记录走到那个位置时,是否是K的倍数。
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char mp[105][105];
int t,n,m,k,sx,sy,ex,ey,book[105][105][15];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
struct node{
int x,y;
int step;
};
int bfs(int x,int y,int step)
{
book[x][y][0]=1;
queue<node>q;
node st,en;
st.x=x;
st.y=y;
st.step=step;
while(!q.empty())
q.pop();
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==ex&&st.y==ey)
return st.step;
for(int i=0;i<4;i++)
{
en.x=st.x+dir[i][0];
en.y=st.y+dir[i][1];
if(en.x>=0&&en.x<n&&en.y>=0&&(mp[en.x][en.y]!='#'||(st.step+1)%k==0)&&en.y<m&&!book[en.x][en.y][(st.step+1)%k]) //判断条件
{
en.step=st.step+1;
book[en.x][en.y][en.step%k]=1;
q.push(en);
}
}
}
return 0;
}
int main()
{
scanf("%d",&t);
while(t--)
{
memset(book,0,sizeof(book));
scanf("%d %d %d",&n,&m,&k);
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(mp[i][j]=='Y'){
sx=i;
sy=j;
}
else if(mp[i][j]=='G'){
ex=i;
ey=j;
}
}
int flag=bfs(sx,sy,0); //标记能否找到
if(flag) printf("%d\n",flag);
else printf("Please give me another chance!\n");
}
return 0;
}
上一篇: js基础知识
下一篇: Dating with girls(2)
推荐阅读
-
xmlplus组件设计系列之按钮(2)
-
常用JS加密编码算法代码第2/2页
-
javascript又一解密过程,推荐的,会这个基本上好多都能解决了第2/2页
-
yii2安装详细流程
-
世界十大最凶名犬:斗牛梗排名第4,藏獒第2
-
VSCode出现ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none)错误
-
Vue CLI2升级至Vue CLI3的方法步骤
-
华为watch 2使用教程,华为watch 2有哪些优缺点!
-
SpringBoot2.X Kotlin系列之数据校验和异常处理详解
-
详解Struts2拦截器机制