-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontest-10170.cpp
85 lines (85 loc) · 1.59 KB
/
contest-10170.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
const long long INF=1e9+10,MAX=100,MOD=1e9+7;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<pii,ll> piii;
void OUT(long double o,int x){
cout<<fixed<<setprecision(x)<<o;
return;
}
char a[MAX][MAX];
ll X[20]={0,-1,1,0};
ll Y[20]={-1,0,0,1};
ll n,m,di[20][20];
vector<pii> vec[20];
void dfs0(pii v,int d){
a[v.F][v.S]=char(int('0')+d);
for(int i=0;i<4;i++){
if((v.F+X[i]<=n and v.F+X[i]>0) and (v.S+Y[i]<=m and (v.S+Y[i]>0 and a[v.F+X[i]][v.S+Y[i]]=='X')))
dfs0({v.F+X[i],v.S+Y[i]},d);
}
return;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)
cin>>a[i][j];
}
ll d=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]=='X'){
dfs0({i,j},d);
d++;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]=='0')
vec[0].pb({i,j});
if(a[i][j]=='1')
vec[1].pb({i,j});
if(a[i][j]=='2')
vec[2].pb({i,j});
}
}
ll mi=INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]=='.'){
ll ans=0;
for(int k=0;k<3;k++){
ll q=INF;
for(pii h:vec[k])
q=min(q,abs(i-h.F)+abs(j-h.S));
ans+=q;
}
mi=min(ans-2,mi);
}
}
}
for(int i=0;i<3;i++){
for(int j=i+1;j<3;j++){
ll ans=INF;
for(pii k:vec[i]){
for(pii h:vec[j])
ans=min(ans,abs(k.F-h.F)+abs(k.S-h.S));
}
di[i][j]=ans;
}
}
mi=min(mi,di[0][1]+di[0][2]-2);
mi=min(mi,di[0][1]+di[1][2]-2);
mi=min(mi,di[1][2]+di[0][2]-2);
cout<<mi;
return 0;
}