# CodeforcesRound#327(Div.2)E.ThreeStates(BFS)

Keywords: iOS

Title:
There are three countries. They want to communicate with each other. They want to communicate with each other. They want to communicate with each other
There is a matrix of n*m, ාstands for the wall, which means there is a matrix of n*m that can't walk, \ #stands for the wall, which means there is a matrix of n*m that can't walk, ාstands for the wall, which means there is no walk
1, 2, 3 for countries, 1, 2, 3 for countries, 1, 2, 3 for countries, and 1, 2, 3 for countries
. for roads that can be opened, it costs 1. For roads that can be opened, it costs 1. For roads that can be opened, it costs 1
What's the minimum cost? What's the minimum cost? What's the minimum cost? What's the minimum cost? What's the minimum cost
Explanation:
The best way for three countries to connect with each other must be at one point. The best way for three countries to connect with each other must be at one point
Then just look at the minimum cost of three countries to the same point. Then just look at the minimum cost of three countries to the same point. Only look at the minimum cost of three countries to the same point
Because every country is a connected block, the first thing to find the minimum cost is BFS. Because every country is a connected block, the first thing to find the minimum cost is BFS. Because every country is a connected block, the first thing to find the minimum cost is BFS

Then we can consider two methods of BFS, then we can consider two methods of BFS, then we can consider two methods of BFS
The first is to stop from BFS at each point and three countries. The first is to stop from BFS at each point and three countries. The first is to stop from BFS at each point and three countries
Find the sum of these values
But obviously, this will tle, because the complexity of enumeration point is O(nm), but obviously, this will tle, because the complexity of enumeration point is O(nm), but obviously, this will tle, because the complexity of enumeration point is O(nm)
The complexity of BFS is O(nm), which eventually becomes O(n2m2) the complexity of BFS is O(nm), which finally becomes o (n ^ 2m ^ 2) the complexity of BFS is O(nm), which finally becomes O(n2m2)

Consider the second. Consider the second. Consider the second
Start BFS from each country, then find the minimum value from three countries to a point, start BFS from each country, then find the minimum value from three countries to a point, start BFS from each country, then find the minimum value from three countries to a point
BFS complexity is O(nm), three countries BFS three times, can guarantee time BFS complexity is O(nm), three countries BFS three times, can guarantee time BFS complexity is O(nm), three countries BFS three times, can guarantee time
At last, as long as the minimum value of enumeration point sum is available, as long as the minimum value of enumeration point sum is available, as long as the minimum value of enumeration point sum is available
However, if the current point is not a country, two countries will build roads at that point repeatedly. If the current point is not a country, two countries will build roads at that point repeatedly. If the current point is not a country, two countries will build roads at that point repeatedly
So we need to subtract two expenses, so we need to subtract two expenses, so we need to subtract two expenses

AC code

```/*
Author:zzugzx
Lang:C++
Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir={{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

int n,m;
int dis;
char g;
queue<pii> q;
void bfs(int x){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(g[i][j]=='0'+x)
q.push(mp(i,j)),dis[i][j][x]=0;
while(!q.empty()){
pii p=q.front();
q.pop();
for(int i=0;i<4;i++){
int dx=p.fi+dir[i];
int dy=p.se+dir[i];
if(dx<1||dx>n||dy<1||dy>m||g[dx][dy]=='#')continue;
int v=(g[dx][dy]=='.');
if(dis[p.fi][p.se][x]+v<dis[dx][dy][x]){
dis[dx][dy][x]=dis[p.fi][p.se][x]+v;
q.push(mp(dx,dy));
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>g[i]+1;
memset(dis,inf,sizeof dis);
bfs(1),bfs(2),bfs(3);
ll ans=inf;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
ll tmp=0;
for(int k=1;k<=3;k++)tmp+=dis[i][j][k];
if(g[i][j]=='.')tmp-=2;
ans=min(ans,tmp);
}
if(ans==inf)cout<<-1;
else cout<<ans;
return 0;
}

```

Posted by phpbeginner0120 on Fri, 19 Jun 2020 00:27:58 -0700