欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dating with girls(2)

程序员文章站 2022-07-14 23:03:45
...

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. 
Dating with girls(2)

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;
}