! Gym 100625J The minimum number of open doors for two prisoners-bfs-(priority queue +bfs, the distance from each intersection)

Source: Internet
Author: User

Test instructions: Two-dimensional matrix, the guards from the outside to the inside to put two prisoners, asked the middle of the door to open the minimum number of times.

Analysis:

This question from the outside, so long as the matrix edge can go to the point (except the point of the wall) can be used as a starting point, there are two end points, so the direct lifting point of the search is not feasible. The practice is to use three times BFS, respectively, from the outside to each can walk the minimum distance (number of open doors), two prisoners to each of the minimum distance to go, and then traverse the matrix, the three distance add up, update the answer can be. The minimum distance from the matrix to the matrix is treated like this: add a lap to the input matrix and start the BFS from the (0,0) position (this is also because the intersection of the optimal solution may not be inside the input matrix, but outside).

Because the minimum distance here is not the number of steps to go, but the number of open doors, so BFS to use priority queue, otherwise it will WA.

Code:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < string> #include <queue> #define INF 1000000007using namespace Std;int t,n,m;char a[200][200];int d[4][2]={{- 1,0},{1,0},{0,-1},{0,1}};int vis[200][200];int dis[4][200][200];struct node{int x,y;node (int x,int y): x (x), Y (y) {};}; struct Qnode{int x,y;int d;qnode (int x,int y,int D): X (x), Y (y), d (d) {}friend bool operator< (const Qnode &a,const qno De &b) {return a.d>b.d;}}; priority_queue<qnode> q;void bfs (int x,int y,int p) {memset (vis,0,sizeof (VIS)), while (!q.empty ()) Q.pop (); Qnode Tp=qnode (x,y,0); Q.push (TP); Vis[x][y]=1;while (!q.empty ()) {Qnode tmp=q.top (); Q.pop (); for (int i=0;i<4;i++) {int dx =tmp.x+d[i][0];int Dy=tmp.y+d[i][1];if (dx>=0&&dx<=n+1&&dy>=0&&dy<=m+1& &a[dx][dy]!= ' * ' &&!vis[dx][dy]) {vis[dx][dy]=1;if (a[dx][dy]== ' # ') dis[p][dx][dy]=dis[p][tmp.x][tmp.y] +1;else Dis[p][dx][dy]=dis[p][tmp.x][tmp.y];qnode Tmp1=qnode(Dx,dy,dis[p][dx][dy]); Q.push (TMP1);}}} int main () {scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); int ex1=-1,ey1=-1,ex2,ey2;for (int i=0;i<= n+1;i++) a[i][0]= '. ', a[i][m+1]= '. '; for (int i=0;i<=m+1;i++) a[0][i]= '. ', a[n+1][i]= '. for (int i=1;i<=n;i++) scanf ("%s", a[i]+1), and for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {if (a[i][j]== ' $ ') {if ( Ex1==-1) Ex1=i,ey1=j;else ex2=i,ey2=j;}}} memset (dis,0,sizeof (DIS)), BFS (0,0,0), BFS (ex1,ey1,1),//for (int i=0;i<n;i++) {//for (int j=0;j<m;j++) cout< <dis[1][i][j];cout<<endl;} BFS (ex2,ey2,2); int ans=inf;for (int i=0;i<=n+1;i++) {for (int j=0;j<=m+1;j++) {if (a[i][j]== '. ' | | a[i][j]== ' $ ') {int sum=0;for (int k=0;k<3;k++) {sum+=dis[k][i][j];} Ans=min (ans,sum);} else if (a[i][j]== ' # ') {int sum=0;for (int k=0;k<3;k++) {sum+=dis[k][i][j];} Ans=min (ans,sum-2);}}} Cout<<ans<<endl;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! Gym 100625J The minimum number of open doors for two prisoners-bfs-(priority queue +bfs, the distance from each intersection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.