Try Again

gym 100971 J Robots at Warehouse

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
5 3

###

#1#

#.#

#2#

###
Output
NO
Input
3 5

#...#

#1.2#

#####
Output
YES
bfs标记数组的应用
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int dir[5][3]={{0,1},{0,-1},{-1,0},{1,0}};
int main()
{
    int n,m,pos_one_x,pos_one_y,pos_two_x,pos_two_y;
    scanf("%d%d",&n,&m);
    char ch[n+5][m+5];
    int vis[n+5][m+5],ans[n+5][m+5],tot=0;
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=n;i++)
    {
        scanf("%s",ch[i]+1);
        for(int j=1;j<=m;j++)
        {
            if(ch[i][j]=='1') pos_one_x=i,pos_one_y=j;
            if(ch[i][j]=='2') pos_two_x=i,pos_two_y=j;
        }
    }
    memset(vis,0,sizeof(vis));
    queue<pair<int,int> >q;
    vis[pos_one_x][pos_one_y]=1;
    q.push({pos_one_x,pos_one_y});
    while(!q.empty())
    {
        pair<int,int> p=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=p.first+dir[i][0];
            int yy=p.second+dir[i][1];
            if(xx>0 && xx<=n && yy>0 && yy<=m && !vis[xx][yy] && ch[xx][yy]!='#')
            {
                vis[xx][yy]=1;
                q.push({xx,yy});    
            }
        }
    }
    if(!vis[pos_two_x][pos_two_y]) return 0*printf("NO\n");
    bool flag=false;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            if(vis[i][j])
            {
                if(vis[i-1][j]) ans[i][j]++;
                if(vis[i+1][j]) ans[i][j]++;
                if(vis[i][j-1]) ans[i][j]++;
                if(vis[i][j+1]) ans[i][j]++;
                if(ans[i][j]>=3) flag=true;
                if(ans[i][j]==1) tot++;
            }
        }
    if(tot!=2) flag=true;
    puts(flag?"YES":"NO");
    return 0;
}

 

posted @ 2018-07-16 21:36  十年换你一句好久不见  阅读(295)  评论(0编辑  收藏  举报