CodeForces_590C Three States(BFS)

在面临经济危机的背景下,Berman、Berance和Bertaly三国形成联盟,旨在通过建设道路连接各成员国领土,实现自由通行。本文探讨了如何在地图上找到最优道路建设方案,确保任意两点间可达,同时最小化建设成本。

Three States

time limit per test:5 seconds
memory limit per test:512 megabytes
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 nnn and mmm (1 ≤ n, m ≤ 10001 ≤ n, m ≤ 10001n,m1000) — 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后,枚举新建路径的交点,若str[i][j]==′.′str[i][j]=='.'str[i][j]==.,则ans=a[i][j]+b[i][j]+c[i][j]−2ans = a[i][j]+b[i][j]+c[i][j]-2ans=a[i][j]+b[i][j]+c[i][j]2(当前土地被改变三次),否则ans=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));
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值