E. Three States

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/591/problem/E

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

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.

Sample Input

4 5
11..2
#..22
#.323
.#333

Sample Output

2

HINT

 

题意

在n*m的地图上,有三个国家,现在要让你修路,使得花费最少,就可以让每个国家的点都可以到达其他国家任意点

保证每个国家内部相连,.可以修建道路,#不能修建

题解:

直接暴力枚举地图上的每一个点,暴力出来每个国家到这个点的最小花费就好了

代码

 

#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;

int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
char s[1001][1001];
int dp[3][1001][1001];
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",s[i]);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            dp[0][i][j]=dp[1][i][j]=dp[2][i][j]=1e8;
    for(int k=0;k<3;k++)
    {
        queue<pair<int,int> > Q;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='1'+k)
                {
                    Q.push(make_pair(i,j));
                    dp[k][i][j]=0;
                }
            }
        }
        while(!Q.empty())
        {
            pair<int,int> now = Q.front();
            int x = now.first;
            int y = now.second;
            Q.pop();
            for(int i=0;i<4;i++)
            {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if(xx<0||xx>=n)continue;
                if(yy<0||yy>=m)continue;
                if(s[xx][yy]=='#')continue;
                if(dp[k][xx][yy]>dp[k][x][y] + (s[x][y]=='.'))
                {
                    dp[k][xx][yy] = dp[k][x][y] + (s[x][y]=='.');
                    Q.push(make_pair(xx,yy));
                }
            }
        }
    }
    int ans = 1e9;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            ans = min(ans,dp[0][i][j]+dp[1][i][j]+dp[2][i][j]+(s[i][j]=='.'));
        }
    }
    printf("%d\n",ans>=1e8?-1:ans);
}