日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Robots at Warehouse(搜索+vector的使用)

發(fā)布時間:2025/4/16 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Robots at Warehouse(搜索+vector的使用) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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,并且他們之間的存在有一個點能夠連接三個方向,或者只有一個方向的點的個數(shù)不是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\n"); 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\n"); 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\n"); 106 else printf("YES\n"); 107 } 108 } 109 return 0; 110 } View Code

知識點:

由于n,m<=200000,所以無法定義flag數(shù)組進行標記。使用vector容器能夠很好的解決這一問題。

轉(zhuǎn)載于:https://www.cnblogs.com/wang-ya-wei/p/6894941.html

總結(jié)

以上是生活随笔為你收集整理的Robots at Warehouse(搜索+vector的使用)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。