题目大意:有一个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. c# 实现文件批量压缩

    今天改一个网站的功能,网站提供一些微信的素材,每个页面对应一套素材,如果会员一张一张下载,那么网站交互性就有点太差了.所以修改的内容就是提供一个按钮,点击按钮将这套图片和网站信息进行打包下载. 思路: ...

  2. sqlserver批量插入数据问题

    下午做一个批量添加的功能,自动添加的那种,总是没有达到理想情况,后来请教师傅,给了这个批量添加的代码,一改果然好了,敬佩之情油然而生哈.分享下~ insert into t_ShopApplicati ...

  3. netbeans git 配置(ssh方式)

    git出问题了,自己又重新配置了下git. 参考文章: https://netbeans.org/kb/docs/ide/git_zh_CN.html#github

  4. c 的ui

    你这些问题是必须要搞清楚的. 1. 首先,UI的实现在不同的操作系统平台上是有所不同的.Windows的UI是内置于操作系统的,Linux/Unix的UI则是通过一个应用程序实现的.由此看来,变成语言 ...

  5. Maven构建灵活配置文件

    本文解决以下问题: Maven下面启动Main函数: 配置JDK版本 如何配置文件,在开发部署测试各个不同版本间无缝切换配置文件: 启动Main函数 Maven默认是不支持Main函数程序,需要在po ...

  6. Java 文件操作大全

    Java 文件操作大全 //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPat ...

  7. Cocoapod错误 - Xcode6.4

    当我从Xcode 6.3切换到Xcode6.4的时候,因为我是mac上安装了两个不同的版本,现在把Xcode 6.3卸掉了. 现在再次运行pod install命令的时候,提示如下错误:   Upda ...

  8. JNI/NDK开发指南(开山篇)

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/41759643 相信很多做过Java或Android开发的朋友经常会接触到JNI方面的技术, ...

  9. IEEE会议排名(转载)

    不知道谁整理的,我就下了个word.所以就标注不了,引用的哪的了. Rank 1: SIGCOMM: ACM Conf on Comm Architectures, Protocols & A ...

  10. SQL Server 2005 版本的操作系统兼容性详细列表

    操作系统要求(32 位) 此表显示对于每种 32 位版本的 SQL Server 2005,操作系统是否可以运行其服务器软件. 有关如何在 Windows Server 2008 上安装 SQL Se ...