题目大意:有一个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. jQuery iframe 自适应高宽度

    Html <iframe id="你的id" src="你要嵌入的页面" scrolling="no" frameborder=&qu ...

  2. jquery 之效果

    // jquery 之效果 .css()既可以获取值,如 .css('fontSize'), 又可以设置内置属性,既可用驼峰式,也可以用连字符版,如 .css('background-color', ...

  3. PHP中,JS和CSS优化工具Minify的使用方法

    为减少HTTP请求,我们往往需要合并和压缩多个JS和CSS文件,下面记录下网上关于实现这个功能的PHP源码以及开源项目Minify的使用方法 一.实现合并和压缩多个JS和CSS文件的代码请参考 1.一 ...

  4. networkRequest

    void TPHttp::applyResource(){    QJsonObject json;    json.insert("ProductCode","MoVd ...

  5. Using .NET 4's Lazy<T> 实现单实例

    Using .NET 4's Lazy<T> type Explanation of the following code: If you're using .NET 4 (or high ...

  6. iis post 请求.html文件报405

    其实本地文件默认是不允许post请求的,但是需要配置一下,配置如下: 我的iis版本是8.5             当然默认也是不能post请求  *.html或是*.json的的文件的,这个问题困 ...

  7. WebService学习整理(一)——客户端三种调用方式整理

    1 WebService基础 1.1 作用 1,       WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. 2, ...

  8. hdu 1824

    也是一道2-sat的入门题: 不过题目描述的不清楚,看了别人的题解才知道题意: 和上面的那题差不多,一个模板: 代码: #include<cstdio> #include<stack ...

  9. js node

    http://blogs.msdn.com/b/scott_hanselman/archive/2011/11/29/window-iis-node-js.aspx http://www.16kan. ...

  10. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...