zoukankan      html  css  js  c++  java
  • Robots at Warehouse(搜索+vector的使用)

    Vitaly works at the warehouse. The warehouse can be represented as a grid of n × mcells, 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.

    Example

    Input
    5 3

    ###
    #1#
    #.#
    #2#
    ###
    Output
    NO
    Input
    3 5

    #...#
    #1.2#
    #####
    Output
    YES
    题意:1和2要进行位置的交换,问你是否能够成功?
    思路:如果在1能够到达2,并且他们之间的存在有一个点能够连接三个方向,或者只有一个方向的点的个数不是2个,则是YES,否则是NO。
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <stack>
      5 #include <vector>
      6 #include <algorithm>
      7 #include<queue>
      8 using namespace std;
      9 const int maxn=200005;
     10 string s[maxn];
     11 typedef pair<int,int>P;
     12 P p;
     13 queue<P>que;
     14 vector<int>vis[maxn];
     15 int n,m,sx,sy,ex,ey;
     16 int a[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
     17 void bfs()
     18 {
     19     for(int i=0; i<n; i++)
     20     {
     21         vis[i].clear();
     22         for(int j=0; j<m; j++)
     23             vis[i].push_back(0);
     24     }
     25     while(!que.empty())
     26         que.pop();
     27     vis[sx][sy]=1;
     28     p.first=sx,p.second=sy;
     29     que.push(p);
     30     int flag=0;
     31     while(!que.empty())
     32     {
     33         p=que.front();
     34         que.pop();
     35         int tx=p.first;
     36         int ty=p.second;
     37         for(int i=0; i<4; i++)
     38         {
     39             int xx=tx+a[i][0],yy=ty+a[i][1];
     40             if(!(0<=xx&&xx<n&&0<=yy&&yy<m)||s[xx][yy]=='#'||vis[xx][yy])
     41                 continue;
     42             vis[xx][yy]=1;
     43             p.first=xx;
     44             p.second=yy;
     45             que.push(p);
     46         }
     47     }
     48 }
     49 int main()
     50 {
     51     while(~scanf("%d%d",&n,&m))
     52     {
     53         for(int i=0; i<n; i++)
     54         {
     55             cin>>s[i];
     56             for(int j=0; j<m; j++)
     57             {
     58                 if(s[i][j]=='1')
     59                 {
     60                     sx=i,sy=j;
     61                 }
     62                 else if(s[i][j]=='2')
     63                 {
     64                     ex=i,ey=j;
     65                 }
     66             }
     67         }
     68         bfs();
     69         if(!vis[ex][ey])
     70         {
     71             printf("NO
    ");
     72             continue;
     73         }
     74         int flag=0,ans=0,cnt;
     75         for(int i=0;i<n;i++)
     76         {
     77             for(int j=0;j<m;j++)
     78             {
     79                 if(flag)
     80                     break;
     81                 cnt=0;
     82                 if(vis[i][j])
     83                 {
     84                     for(int k=0;k<4;k++)
     85                     {
     86                         if(!(0<=i+a[k][0]&&i+a[k][0]<n&&0<=j+a[k][1]&&j+a[k][1]<m)||s[i+a[k][0]][j+a[k][1]]=='#')
     87                             continue;
     88                         if(vis[i+a[k][0]][j+a[k][1]])
     89                             cnt++;
     90                     }
     91                     if(cnt>2)
     92                     {
     93                         flag=1;
     94                         printf("YES
    ");
     95                         break;
     96                     }
     97                     if(cnt==1) ans++;
     98                 }
     99             }
    100             if(flag)
    101                 break;
    102         }
    103         if(flag==0)
    104         {
    105             if(ans==2) printf("NO
    ");
    106             else printf("YES
    ");
    107         }
    108     }
    109     return 0;
    110 }
    View Code

    知识点:

    由于n,m<=200000,所以无法定义flag数组进行标记。使用vector容器能够很好的解决这一问题。

  • 相关阅读:
    《C#从现象到本质》读书笔记(八)第10章反射
    《C#从现象到本质》读书笔记(七)第9章 泛型
    《C#从现象到本质》读书笔记(六)第8章委托和事件
    《C#从现象到本质》读书笔记(五)第5章字符串第6章垃圾回收第7章异常与异常处理
    求1+2+……+n的和
    回溯法的应用举例
    回溯法
    翻转单词顺序列
    左旋转字符串
    和为S的两个数字
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6894941.html
Copyright © 2011-2022 走看看