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

E. Three States
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 3represent 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 test(s)
input
4 5
11..2
#..22
#.323
.#333
output
2
input
1 5
1#2#3
output
-1

题目大意:

有三个国家要建立联盟,每个数字表示一个联盟一共3个,‘.’表示可以修路,‘#’表示不可以修路,问最少要多少要建立多少条路才可以是任意一个联盟的城市到任意其他国家的城市。

解法:

计算每个点到1,2,3的距离,特判等于0的情况,用inf的时候不能太大。

#include "iostream"
#include "cstdio"
#include "string.h"
#include "queue"
#include "math.h"
using namespace std;
const int inf=10000000;
int go[2][4]={{0,-1,0,1},{-1,0,1,0}};
int to[3][1010][1010],n,m;
char map[1010][1010];
typedef pair<int,int>pii;
bool judge(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m||map[x][y]=='#')
		return false;
	return true;
}
void cal(int dist[1010][1010],char type)
{
	queue<pii>que;
	pii now,temp;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(map[i][j]==type)
			{
				que.push(make_pair(i,j));
				dist[i][j]=0;
			}
			else
				dist[i][j]=inf;
		}
	}
	while(!que.empty())
	{
		temp=que.front();
		que.pop();
		for(int i=0;i<4;i++)
		{
			now.first=temp.first+go[0][i];
			now.second=temp.second+go[1][i];
			if(!judge(now.first,now.second))
				continue;
			if(map[now.first][now.second]=='.')
			{
		    	if(dist[now.first][now.second]>dist[temp.first][temp.second]+1)
				{
					dist[now.first][now.second]=dist[temp.first][temp.second]+1;
					que.push(now);
				}
			}
			else
			{
				if(dist[now.first][now.second]>dist[temp.first][temp.second])
				{
					dist[now.first][now.second]=dist[temp.first][temp.second];
				    que.push(now);
				}
			}
		}
	}
}
int main(int argc,char* argv[])
{
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		scanf(" %s",map[i]);
	for(int i=0;i<3;i++)
		cal(to[i],(char)'1'+i);
	int b=inf,c=inf;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(map[i][j]=='2')
				b=min(b,to[0][i][j]);
			else if(map[i][j]=='3')
				c=min(c,to[0][i][j]);
		}
	}
	if(b==0&&c==0)
		printf("0\n");
	else
	{
		int ans=inf;
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(map[i][j]=='.')
					ans=min(ans,(to[0][i][j]+to[1][i][j]+to[2][i][j]-2));
		if(ans>=n*m)
			puts("-1");
		else
			printf("%d\n",ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值