题目大意:有一个M*N的矩阵,在这个矩阵里面有三个王国,编号分别是123,想知道这三个王国连接起来最少需要再修多少路。

分析:首先求出来每个王国到所有能够到达点至少需要修建多少路,然后枚举所有点求出来最少的即可。

代码如下:

---------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std; const int MAXN = ;
const int oo = 1e9+; int dir[][] = { {,},{,},{-,},{,-} };
char G[MAXN][MAXN];
struct node
{
int step[];
}a[MAXN][MAXN];
struct point
{
int x, y;
}; void BFS(int M, int N, int k)
{///第k个王国到达所有点的最短距离
queue<point> Q;
point q, s; for(int i=; i<M; i++)
for(int j=; j<N; j++)
{
if(G[i][j] == k+'')
{
q.x = i, q.y = j;
a[i][j].step[k] = ;
Q.push(q);
}
} while(Q.size())
{
q = Q.front();Q.pop(); for(int i=; i<; i++)
{
s = q;
s.x += dir[i][];
s.y += dir[i][]; if(s.x>=&&s.x<M && s.y>=&&s.y<N && G[s.x][s.y] != '#')
{
int t = (G[s.x][s.y]=='.' ? :); if(a[s.x][s.y].step[k]==- || a[q.x][q.y].step[k]+t < a[s.x][s.y].step[k])
{///因为王国之间没有不用修路,所有可能会回搜
a[s.x][s.y].step[k] = a[q.x][q.y].step[k] + t;
Q.push(s);
}
}
}
}
} int main()
{
int M, N; scanf("%d%d", &M, &N); for(int i=; i<M; i++)
scanf("%s", G[i]); memset(a, -, sizeof(a)); for(int i=; i<; i++)
BFS(M, N, i); int ans = oo; for(int i=; i<M; i++)
for(int j=; j<N; j++)
{
if(a[i][j].step[]!=- && a[i][j].step[]!=- && a[i][j].step[]!=-)
{///如果这个点三个王国都能够到达
int t = (G[i][j]=='.' ? :);
ans = min(ans, a[i][j].step[]+a[i][j].step[]+a[i][j].step[]-t*);
}
}
if(ans == oo)
ans = -;
printf("%d\n", ans); return ;
}

E. Three States - Codeforces Round #327 (Div. 2) 590C 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. 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 ...

  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. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...

  2. js 作为属性的变量

    当声明一个javascript全局变量时,实际上是定义了全局对象的一个属性. 当使用var声明一个变量时,创建的这个属性是不可配置的,也就是说这个变量无法通过delete运算符来删除.可能你已经注意到 ...

  3. 数据库创建&数据表创建

    --第2_1题创建数据库 create database Student201038897041 on primary (name='student1', filename='F:\coures\SQ ...

  4. PHP 面向对象之自定义类

    所谓面向对象就是什么时候什么东西做什么,我们设计类的时候需要想的就是怎么做的内容,那么怎么样的一个类才算是符合OOP的思想呢,答案是:这个类写好之后,在使用的过程中,能准确的代表一个事物,在书写的时候 ...

  5. (转载)delphi 常用函数(数学)

    delphi 常用函数(数学) Delphi中怎么将实数取整? floor 和 ceil 是 math unit 里的函数,使用前要先 Uses Math.trunc 和 round 是 system ...

  6. Castle ActiveRecord配置文件中连接字符串解密

    使用Castle ActiveRecord通常都是使用配置文件进行数据库连接配置.然后采用如下方式初始化: IConfigurationSource source = ConfigurationMan ...

  7. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  8. python url解析

    >>> url="http://localhost/test.py?a=hello&b=world " >>> result=urlpa ...

  9. python的random函数

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  10. 【Ecmall】ECMall2.x模板制作入门系列(认识ECMall模板)

    ECMall2.x模板制作入门系列之1(认识ECMall模板) 从ECMall2.0全新架构发布以来,随着版本的不断更新,ECMall已经逐渐走向一个稳定时期,是时候整理一些实用教程了.下面给大家带来 ...