Gym-100971J

本文探讨了一个关于机器人在网格环境中能否互换位置的问题。通过分析路径连接性和特殊点形态,如环状路径和T型交叉口,提出了一种简单有效的解决方案。文中详细解释了如何通过遍历地图来判断是否能够完成互换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

J. Robots at Warehouse

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Vitaly works at the warehouse. The warehouse can be represented as a grid of n × m cells, each of which either is free or is occupied by a container. From every free cell it's possible to reach every other free cell by moving only through the cells sharing a side. Besides that, there are two robots in the warehouse. The robots are located in different free cells.

Vitaly wants to swap the robots. Robots can move only through free cells sharing a side, moreover, they can't be in the same cell at the same time or move through each other. Find out if the swap can be done.

Input

The first line contains two positive integers n and m (2 ≤ n·m ≤ 200000) — the sizes of the warehouse.

Each of the next n lines contains m characters. The j-th character of the i-th line is «.» if the corresponding cell is free, «#» if there is a container on it, «1» if it's occupied by the first robot, and «2» if it's occupied by the second robot. The characters «1» and «2» appear exactly once in these lines.

Output

Output «YES» (without quotes) if the robots can be swapped, and «NO» (without quotes) if that can't be done.

Examples

input

Copy

5 3

###

#1#

#.#

#2#

###

output

Copy

NO

input

Copy

3 5

#...#

#1.2#

#####

output

Copy

YES

题意:输入n,m 表示 n * m 的矩阵,#表示墙, 。 表示路,1 , 2 表示两个机器人,问1 , 2 机器人能不能交换位置,1 2机器人不能在同一个点,1, 2相邻的时候不能交换位置,而且点都是连在一起的(看其他人博客的时候发现的)

思路:其实1 , 2 两个机器人换位置就有可能两种可能,要不就是有1 2机器人之间两条路(有环),要不就是路上有T字型的路口,因为这样其中一个机器人可以藏在分支上,另个一个机器人就可以通过这里

如果是有环,那么矩阵中的点的旁边都是有两个点的,比如

# # # # #

#。#。#

# 1 # 2 #

#。#。#

# # # # #

像题目中的样例 1 就是有两个点旁边没有两个点的(1 , 2机器人的点),这样可以判断输出NO

T型路口的,只要是有一个这样的存在就可以输出YES,我们可以直接暴力遍历所有点,只要有一个点周围有三个点,就可以输出YES,比如

#  #  #  # #

#   #。#  #

1 。。。2

# #  #  # #

在map[ 3 ] [ 3 ]的点的周围有三个点,所以可以判断能交换位置

坑点:主要是点都是连在一起的,所以少了很多麻烦,网上有人就是BFS搜索路然后回溯查找有没有T型路口,然后再用DFS搜索有没有环,这样就超时了

AC代码:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
    int n,m,i,j;
    bool flag=true;         //一开始设立为true,有环的可能
    cin>>n>>m;
    int ans=0;
    vector<string> map(n);
    for (i=0;i<n;i++)
        cin>>map[i];
    for (i=0;i<n;i++)
        for (j=0;j<m;j++)
            {
                ans=0;                      //判断一个点周围有多少点
                if (map[i][j]=='#')
                    continue;
                if (i!=0&&map[i-1][j]!='#')               //注意边界,   1 , 2机器人一开始的地方也可以走,所以可以看成点
                    ans++;
                if (i!=n-1&&map[i+1][j]!='#')
                    ans++;
                if (j!=0&&map[i][j-1]!='#')
                    ans++;
                if (j!=m-1&&map[i][j+1]!='#')
                    ans++;
                if (ans>=3)                        //有三个点直接输出
                    {
                        cout<<"YES"<<endl;
                        return 0;
                    }
                if (ans==1)                       //当有一个点周围只有一个点,说明是死路,如果所有点都没有其他情况,就可以输出NO
                    flag=false;
            }
    if (flag)                                                //有环的存在没有一个点周围是两个点的,如果有那也是T型路口的情况
        cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值