Three States
Problem Description
The famous global economic crisis is approaching rapidly, so the states of Berman, Berance and Bertaly formed an alliance and allowed the residents of all member states to freely pass through the territory of any of them. In addition, it was decided that a road between the states should be built to guarantee so that one could any point of any country can be reached from any point of any other State.
Since roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of n rows and m columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable.
Your task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells.
It is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it.
Input
he first line of the input contains the dimensions of the map n n n and m m m ( 1 ≤ n , m ≤ 1000 1 ≤ n, m ≤ 1000 1 ≤ n, m ≤ 1000) — the number of rows and columns respectively.
Each of the next n lines contain m characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character ‘.’ corresponds to the cell where it is allowed to build a road and the character ‘#’ means no construction is allowed in this cell.
Output
Print a single integer — the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1.
Sample Input
4 5
11…2
#…22
#.323
.#333
Sample Output
2
题意
在一个n*m大小的地图上,有3个国家,每个国家至少有一块领土,且国家之间的领土连通。国家之外的土地分为两类,一类为’.’,可以建设道路,一类为’#’,不可建设道路。给出完整的地图,求最少改变多少土地建设道路可使三个国家连通。
题解
分别以3个国家为起始点进行BFS。
a
[
i
]
[
j
]
a[i][j]
a[i][j]代表从起始点到
(
i
,
j
)
(i,j)
(i,j)的位置,最少需要改变多少格土地。因为若下一步走到的是国家的领土,则无需改变土地。所以在更新时,可以使用双端队列,将下一步无需更改土地放置在队首即可。
BFS后,枚举新建路径的交点,若
s
t
r
[
i
]
[
j
]
=
=
′
.
′
str[i][j]=='.'
str[i][j]==′.′,则
a
n
s
=
a
[
i
]
[
j
]
+
b
[
i
]
[
j
]
+
c
[
i
]
[
j
]
−
2
ans = a[i][j]+b[i][j]+c[i][j]-2
ans=a[i][j]+b[i][j]+c[i][j]−2(当前土地被改变三次),否则
a
n
s
=
a
[
i
]
[
j
]
+
b
[
i
]
[
j
]
+
c
[
i
]
[
j
]
ans = a[i][j]+b[i][j]+c[i][j]
ans=a[i][j]+b[i][j]+c[i][j]。取ans最小值即为所求。
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<ctime>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1020;
const int mod = 998244353;
struct node{
int x, y, p;
node(){}
node(int a, int b, int c):x(a),y(b),p(c){}
bool operator <(node b)const{
return p>b.p;
}
};
int n, m, a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];
int dx[4]={-1,0,1,0}, dy[4]={0,-1,0,1};
char str[maxn][maxn];
void bfs(char ch, int a[][maxn]);
int main()
{
int i, j, k, ans = INF;
scanf("%d%d", &n, &m);
for(i=0;i<n;i++)
scanf(" %s", str[i]);
memset(a, -1, sizeof(a));
memset(b, -1, sizeof(b));
memset(c, -1, sizeof(c));
bfs('1', a);
bfs('2', b);
bfs('3', c);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(a[i][j]!=-1 && b[i][j]!=-1 && c[i][j]!=-1){
if(str[i][j] == '.')ans = min(ans, a[i][j]+b[i][j]+c[i][j]-2);
else ans = min(ans, a[i][j]+b[i][j]+c[i][j]);
}
if(ans == INF)printf("-1\n");
else printf("%d\n", ans);
return 0;
}
void bfs(char ch, int a[][maxn])
{
deque<P> que;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(str[i][j] == ch){
que.push_back(P(i, j));
a[i][j] = 0;
}
while(que.size())
{
P p = que.front();que.pop_front();
for(int i=0;i<4;i++){
int nx = p.first + dx[i], ny = p.second+dy[i];
if(nx<0 || nx>=n || ny<0 || ny>=m || str[nx][ny] == '#' || a[nx][ny]!=-1)continue;
if(str[nx][ny] != '.'){
a[nx][ny] = a[p.first][p.second];
que.push_front(P(nx,ny));
}
else{
a[nx][ny] = a[p.first][p.second]+1;
que.push_back(P(nx,ny));
}
}
}
}