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

-1

题意:有三个国家,要建造一些桥,使得三个国家能相互连通,问最少要造多少桥。

思路:共有两种情况,一种是先使得两个国家连通,然后再使它们和另一个连通,还有一种是设一个点,造桥使得这三个国家都到这个点。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 1005
char s[maxn][maxn];
int dist[maxn][maxn][4],vis[maxn][maxn]; //dsit[i][j][num]表示坐标为i,j的点到值为num所对的这个国家的最小要造的桥数
int st[4][2],n,m;
int ans[4][4]; //ans[i][j]表示i国家和j国家要连通所要造的桥的个数
int tab[4][2]={0,1,-1,0,0,-1,1,0};
int q[1111111][3];//0 x,1 y,2 t
void bfs(int num)
{
int i,j,x,y,t,xx,yy,tt;
int front,rear;
front=rear=1;
x=st[num][0];
y=st[num][1];
q[front][0]=x;
q[front][1]=y;
q[front][2]=0;
dist[x][y][num]=0;
vis[x][y]=1;
while(front<=rear){ //这里先把值为num的国家都找出来,这些都是连通的
x=q[front][0];
y=q[front][1];
t=q[front][2];
front++;
for(i=0;i<4;i++){
xx=x+tab[i][0];
yy=y+tab[i][1];
if(xx>=1 && xx<=n && yy>=1 && yy<=m && s[xx][yy]-'0'==num && !vis[xx][yy]){
vis[xx][yy]=1;
dist[xx][yy][num]=0;
rear++;
q[rear][0]=xx;
q[rear][1]=yy;
q[rear][2]=0;
} } }
front=1; //这里值为num的国家要再次放入队列
while(front<=rear){
x=q[front][0];
y=q[front][1];
t=q[front][2];
front++;
for(i=0;i<4;i++){
xx=x+tab[i][0];
yy=y+tab[i][1];
if(xx>=1 && xx<=n && yy>=1 && yy<=m && s[xx][yy]!='#' && !vis[xx][yy]){
vis[xx][yy]=1;
if(s[xx][yy]=='.'){
dist[xx][yy][num]=t+1;
rear++;
q[rear][0]=xx;
q[rear][1]=yy;
q[rear][2]=t+1;
}
else{
int cot=s[xx][yy]-'0';
dist[xx][yy][num]=t+1;
ans[num][cot]=min(ans[num][cot],t+1);
rear++;
q[rear][0]=xx;
q[rear][1]=yy;
q[rear][2]=t+1;
}
}
}
}
} int main()
{
int i,j,ant;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(j=1;j<=m;j++){
if(s[i][j]=='1'){
st[1][0]=i;st[1][1]=j;
}
if(s[i][j]=='2'){
st[2][0]=i;st[2][1]=j;
}
if(s[i][j]=='3'){
st[3][0]=i;st[3][1]=j;
}
dist[i][j][1]=dist[i][j][2]=dist[i][j][3]=inf;
} }
for(i=1;i<=3;i++){
for(j=1;j<=3;j++){
if(i!=j){
ans[i][j]=inf;
}
else ans[i][j]=0;
}
} for(i=1;i<=3;i++){
memset(vis,0,sizeof(vis));
bfs(i);
}
ant=inf;
ant=min(ant,ans[1][2]+ans[1][3]-2);
ant=min(ant,ans[2][1]+ans[2][3]-2);
ant=min(ant,ans[3][1]+ans[3][2]-2);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(s[i][j]!='#'){
if(s[i][j]=='.'){
ant=min(ant,dist[i][j][1]+dist[i][j][2]+dist[i][j][3]-2);
}
else{
ant=min(ant,dist[i][j][1]+dist[i][j][2]+dist[i][j][3]-2);
}
} }
}
if(ant>10000000)printf("-1\n");
else printf("%d\n",ant);
}
return 0;
}

Codeforces Round #327 (Div. 1) C. Three States的更多相关文章

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

    E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...

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

    题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...

  3. 暴搜 - Codeforces Round #327 (Div. 2) E. Three States

    E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...

  4. E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)

    题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路. 分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来 ...

  5. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

  6. Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理

    D. Chip 'n Dale Rescue Rangers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...

  7. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

  8. Codeforces Round #327 (Div. 2) B. Rebranding 水题

    B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...

  9. Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

    http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...

随机推荐

  1. 剑指offer 查找和排序的基本操作:查找排序算法大集合

    重点 查找算法着重掌握:顺序查找.二分查找.哈希表查找.二叉排序树查找. 排序算法着重掌握:冒泡排序.插入排序.归并排序.快速排序. 顺序查找 算法说明 顺序查找适合于存储结构为顺序存储或链接存储的线 ...

  2. 开源:AspNetCore 应用程序热更新升级工具(全网第一份公开的解决方案)

    1:下载.开源.使用教程 下载地址:Github 下载 .其它下载 开源地址:https://github.com/cyq1162/AspNetCoreUpdater 使用教程: 解压AspNetCo ...

  3. Python找对称数——纪念第一次自主编写代码

    2021-01-17 题目: [问题描述]已知10个四位数输出所有对称数及个数 n,例如1221.2332都是对称数[输入形式]10个四位数,以空格分隔开[输出形式]输入的四位数中的所有对称数,对称数 ...

  4. 【Linux】awk想打印制定列以后的所有列

    今天偶然研究awk,有一个文件,文件内容是全篇的1 2 3 4 5 6 7 8 9 0 现在想打印除了第一列意外的所有列 文件内容: [root@localhost ~]# cat test.txt ...

  5. 【Linux】大于2T的磁盘怎么分区?

    环境CentOS7.1 2.9t磁盘 fdisk 只能分区小于2t的磁盘,大于2t的话,就要用到parted 1,将磁盘上原有的分区删除掉: 进入:#parted   /dev/sdb 查看:(par ...

  6. Centos6.9安装ACFS

    安装完oracle 11GR2的RAC后,使用asmca打开图形化界面后,发现Volumes和ASM Cluster File System两个选项卡不能用 原因是因为ACFS不支持CentOS 解决 ...

  7. [Cerc2005]Knights of the Round Table

    题目描述 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少有3个骑士参加,且相互憎恨的骑士不能坐在圆桌的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是大于1的奇数,以防 ...

  8. 两节锂电池保护IC,芯片电路图如何设计

    两节锂电池出了充电电路外,必须搭配的也就是两节锂电池的保护板电路和芯片了.对两节节串联可再充电锂离子/锂聚合物电池的过充电.过放电和过电流进行保护.和电池反接保护功能,这些都是极其重要的. 首先设计两 ...

  9. PAT练习num3-跟奥巴马一起学编程

    美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝"计算机科学教育周"正式启动,奥巴马编写了很简单的计算机 ...

  10. windows中使用django时报错:A server error occurred. Please contact the administrator.

    这是因为在视图函数中使用了get函数,获取了不存在的数据例如:数据库中不存在一条name为hello1的数据,使用如下语句访问message = Message.objects.get(name='h ...