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. 通过js检测到iframe,使父窗口重定向到index -----------???----------------------

    通过js检测到iframe,使父窗口重定向到index -----------???---------------------- 如果本身已将在iframe中,那么重定向的页面应该直接添加到父级ifr ...

  2. Strom-7 Storm Trident 详细介绍

    一.概要 1.1 Storm(简介)      Storm是一个实时的可靠地分布式流计算框架.      具体就不多说了,举个例子,它的一个典型的大数据实时计算应用场景:从Kafka消息队列读取消息( ...

  3. redis 和 bloom filter

    今天打算使用redis 的bitset搞一个 bloom filter, 这样的好处是可以节省内存,坏处是可能在会有一些数据因为提示重复而无法保存. bloom filter 的大体原理就是通过不同的 ...

  4. codeforces 682D Alyona and Strings

    #include <cstdio> #include <iostream> #include <ctime> #include <vector> #in ...

  5. SQL你必须知道的-函数及类型转换

    use MySchoolTwo    --ISNULL(expression,value) :如果 expression不为空则返回 expression ,否则返回 value.    select ...

  6. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  7. 测试xss和xsf

    xss注入攻击: 123123123 被动注入: 1231231231231231 主动注入: 对不起,你需要登录才能评论 用户名 密码

  8. Probabilistic SVM 与 Kernel Logistic Regression(KLR)

    本篇讲的是SVM与logistic regression的关系. (一) SVM算法概论 首先我们从头梳理一下SVM(一般情况下,SVM指的是soft-margin SVM)这个算法. 这个算法要实现 ...

  9. Codeforces Round #363 (Div. 1) C. LRU

    题意: n个数,长度为k的缓存,每次询问,每个数以pi的概率被选,如果不在缓存区则加入,如果缓存区满了,则第一个进缓存的出来,问10^100次询问以后每个数在缓存的概率 思路: 状压DP,看了hzwe ...

  10. Hbase 基本命令

    启动Hbase:./start-hbase.sh 进入Hbase shell控制台:./hbase shell   查看所有表 list   查看数据库状态:status   查看数据库版本:vers ...