E - 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

题意:

给你一个矩阵,T代表有T组数据,c,r,代表矩阵的大小,k代表障碍物可以通过的时间;

然后下面是地图;

思路:

这也是一道广搜题,但是不同的是障碍物会在k的倍数时变成可以通过,所以在搜索时要注意一下。

代码如下:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

int t,n,m,tt,sx,sy;
char map[1010][101];
int book[101][101][101];

int nextt[4][2]={{1,0},{-1,0},{0,-1},{0,1}};

struct node
{
    int x;
    int y;
    int step;
};

int bfs()
{
    int i;
    node a,next;
    memset(book,0,sizeof book);
    queue<node>Q;
    a.x=sx;
    a.y=sy;
    a.step=0;
    book[a.x][a.y][0]=1;
    Q.push(a);
    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();
        if(map[a.x][a.y]=='G')
            return a.step;
        for(i=0;i<4;i++)
        {
            next=a;
            next.x+=nextt[i][0];
            next.y+=nextt[i][1];
            next.step+=1;
            if(next.x<0||next.x>=n||next.y<0||next.y>=m||book[next.x][next.y][next.step%tt]==1)
                continue;    //把步数取余进行标记判断,正好可以把第一次后再走到这个位置的点排除,相当于剪枝;
            if(map[next.x][next.y]=='Y'||map[next.x][next.y]=='.'||map[next.x][next.y]=='G')
            {                //需要注意男生女生的位置也是可以走的;
                Q.push(next);
                book[next.x][next.y][next.step%tt]=1;
            }
            if(map[next.x][next.y]=='#'&&next.step%tt==0)
            {
                Q.push(next);
                book[next.x][next.y][next.step%tt]=1;
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&tt);
        int i,j;
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='Y')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        int kk=bfs();
        if(kk==-1)
            printf("Please give me another chance!\n");
        else
            printf("%d\n",kk);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81156296