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

Codeforces Round #327 (Div. 2) E. Three States BFS的更多相关文章

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

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

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

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

  3. Codeforces Round #327 (Div. 1) C. Three States

    C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standard inp ...

  4. Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

    Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers ...

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

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

  6. 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 ...

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. 【应聘】阿里巴巴Java面试题目

    原文地址:http://blog.csdn.net/free0sky/article/details/7927275   一.String,StringBuffer, StringBuilder 的区 ...

  2. 延迟加载图片的 jQuery 插件——lazyload.js

    lazyload 这个 jQuery 插件,是用来缓冲加载图片的插件.如果一篇文章很长有很多图片的话,下载图片就需要很多时间.而这款插件,会检测你的滚动情况,只有你要看到那个图片的时 候,它才会从后台 ...

  3. 【转】Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38377229 ,本文出自[张鸿洋的博客] 很多人面试肯定都被问到过,请问Andr ...

  4. MySQL auto_increment的坑

    背景: Innodb引擎使用B_tree结构保存表数据,这样就需要一个唯一键表示每一行记录(比如二级索引记录引用). Innodb表定义中处理主键的逻辑是: 1.如果表定义了主键,就使用主键唯一定位一 ...

  5. 【转】ACE开发环境搭建

    Windows平台 1)        下载ACE源码 ACE官方网址:http://www.cs.wustl.edu/~schmidt/ACE.html ACE下载地址:http://downloa ...

  6. mysql show processlist命令 详解

    SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...

  7. Win7下硬盘安装Centos5.3

    前提声明:一个硬盘最多只能有四个主分区,也就是hda1,hda2,hda3和hda4,逻辑分区都是从hda5开始 一.软件准备:EasyBCD+分区助手+Centos 5.3 ISO镜像文件: 二.W ...

  8. 【Windows核心编程】VirtualAlloc 例子

    // VirtualAlloc.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #in ...

  9. (三)相遇射线的3D碰撞盒

    序 在2D游戏中,我们知道处理碰撞时,需要设置精灵遮罩图.同样,进入3D,处理碰撞时需要3D模型作为“遮罩图”. 索尼克 飞檐走壁   目的 (1)处理模型间的碰撞问题         (2)获取鼠标 ...

  10. python学习之dict的items(),values(),keys()

    Python的字典的items(), keys(), values()都返回一个list >>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'worl ...