Codeforces Round #327 (Div. 2) E. Three States bfs

E. Three States

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

The first line of the input contains the dimensions of the map n and m (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.

input
4 5
11..2
#..22
#.323
.#333
output
2
 
题意:给你n*m的图,123表示三种状态,点表示可以修建的道路,问你将三种状态链接起来的最小修建道路数是多少
题解:我们找出每种状态到每个点的最小步数,最后答案就是取最小
 
#include<cstdio>
#include<queue>
using namespace std;

const int inf = 1e7 + 10;
const int N = 1010;
const int M = 1e6 + 10;
char map[N][N];
int n, m, dp[3][N][N], val[N][N];
int xl[4]={-1,1,0,0};
int yl[4]={0,0,-1,1};

struct Node{
    int x, y, u;
    Node(){}
    Node(int x, int y, int u):x(x),y(y),u(u){}
    bool operator < (const Node &A)const{
        return u > A.u;
    }
};

void solve(int x){
    char ch = x + '1';
    priority_queue<Node> Q;
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            if(map[i][j] == ch){
                dp[x][i][j] = 0;
                Q.push(Node(i, j, 0));
            }
        }
    }
    int a, b, c, d;
    Node nd;
    while(!Q.empty()){
        nd = Q.top(); Q.pop();
        a = nd.x;
        b = nd.y;
        for(int i=0; i<4; i++){
            c = a + xl[i];
            d = b + yl[i];
            if(c<0 || c>=n || d<0 || d>=m)  continue;
            if(map[c][d] =='#' || dp[x][c][d]!=inf)    continue;
            dp[x][c][d] = nd.u + val[c][d];
            Q.push(Node(c, d, dp[x][c][d]));
        }
    }
}

inline void Min(int &a, int b){
    if(a > b)   a = b;
}

int main(){
    while(~scanf("%d %d", &n, &m)){
        for(int i=0; i<n; i++){
            scanf("%s", map[i]);
            for(int j=0; j<m; j++){
                if(map[i][j]=='.')  val[i][j] = 1;
                else {
                    val[i][j] = 0;
                }
                for(int k=0; k<3; k++){
                    dp[k][i][j] = inf;
                }
            }
        }
        for(int i=0; i<3; i++){
            solve(i);
        }
        int ans = inf;
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                if(map[i][j] == '.'){
                    Min(ans, dp[0][i][j] + dp[1][i][j] + dp[2][i][j] - 2);
                } else {
                    Min(ans, dp[0][i][j] + dp[1][i][j] + dp[2][i][j]);
                }
            }
        }
        if(ans == inf)  ans = -1;
        printf("%d
", ans);
    }
    return 0;
}
代码
 
原文地址:https://www.cnblogs.com/zxhl/p/4923494.html