Gym 100947H-Phobia

Description
standard input/output
Announcement
Statements
Sally has a phobia of cockroaches. Every night she is having the same horrible nightmare! Where she is standing alone in a big maze surrounded by millions and millions of cockroaches. She wakes up horrified and screaming when she sees a huge cockroach standing on her foot. After many sleepless nights like that one, she finally decided to ask you for help.

Since you have some experience with psychology (after all you are a programmer, right?) you tell her that the best way to cure her phobia is to fight it. She must be strong and fight these stinky little insects. After many argument she agrees with you and decides that she must overcome her fears. To do so she must reach for her sandals (you know, she needs a weapon for this great battle). Sally marked her sandals with a big red X sign to make sure they are clearly noticeable.

Here comes your job! You thought that you should write a program to help her train for this battle. Given the maze information, your program should tell her whether if she could reach her sandals without being touched by any cockroach.

The maze is an N × M rectangular grid, each cell of this grid is either empty or a wall or a hole. Cockroaches emerge from these holes and spread in all directions. Sally and Cockroaches cannot pass through walls, in other words Sally can only move to an empty cell, and cockroaches can only occupy an empty cell.

Formally, at time t if cell (i, j) is occupied by cockroaches then in time t + 1 cells (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1) as well as cell (i, j) will be occupied by cockroaches if the respective cells are empty and are inside the borders of the maze.

这里写图片描述

In one second, Sally can move to an adjacent empty cell. Formally if at time t Sally is at cell (i, j), then in time t + 1 she can move to one of these cells: (i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1) if the respective cells are empty and are inside the borders of the maze.

One empty cell contains Sally starting position, and One other empty cell Contains her Sandals. Sally’s goal is to reach her sandals without touching any cockroach. Sally touches a cockroach when Sally’s current position is occupied by cockroaches.

Input
The first line will be the number of test cases T.

Each test case contains two integers N and M (1 ≤ N, M ≤ 100) followed by N × M grid that represents the maze where:

‘S’ : sally’s starting position.

‘X’ : Sally’s sandals position.

‘#’ : a Wall.

‘*’ : a hole.

‘.’ : an empty cell.

Output
For each test case print “yes” if Sally can exit from the maze without touching any cockroach or “no” otherwise.

Sample Input
Input
2
5 5
S..#*
…#.
…##
…..
….X
5 5
S..#*
…#.
…#.
…..
….X
Output
yes
no

题意就是问你能不能在黑色的格子追到你之前走到目的地,bfs即可,比较人和黑色格子到达目的地的最短时间,若人比黑色格子快,则可以。

代码

#include <cstdio>
#include <algorithm>
#include <queue>
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
int n,m;
struct node
{
    int x;int y;int step;
};
char maze[105][105];
int used[105][105];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

bool check(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!used[x][y]&&maze[x][y]=='.'||
       x>=0&&x<n&&y>=0&&y<m&&!used[x][y]&&maze[x][y]=='X')
        return true;
    return false;
}

int bfs(int sx,int sy)
{
    queue <node> que;
    while(!que.empty())
        que.pop();
    node a;
    a.x=sx;a.y=sy;a.step=0;
    que.push(a);
    used[sx][sy]=1;
    while(!que.empty())
    {
        node b;
        a=que.front();
        que.pop();
        if(maze[a.x][a.y]=='X')
            return a.step;
        for(int i=0;i<4;i++)
        {
            int nx=a.x+dx[i];
            int ny=a.y+dy[i];
            if(check(nx,ny))
            {
                used[nx][ny]=1;
                b.x=nx;
                b.y=ny;
                b.step=a.step+1;
                que.push(b);
            }
        }

    }
    return 0x7fffffff;
}



int main (void)
{
    int t ;
    cin>>t;
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%s",maze[i]);
        }
        int min1=0x7fffffff;
        int min2=0x7fffffff;
        //int min2;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(maze[i][j]=='*')
                {
                    memset(used,0,sizeof(used));
                    min1=min(min1,bfs(i,j));
                }
                if(maze[i][j]=='S')
                {
                    memset(used,0,sizeof(used));
                    min2=min(min2,bfs(i,j));
                    //min2=bfs(i,j);
                }
            }
        }
        if(min2<min1)
        {
            printf("yes\n");
        }
        else
            printf("no\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值