E. Three States - Codeforces Round #327 (Div. 2) 590C States(广搜)
题目大意:有一个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(广搜)的更多相关文章
- 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 ...
- Codeforces Round #327 (Div. 2) E. Three States
题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...
- 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
E. Three States Problem's Link Mean: 在一个N*M的方格内,有五种字符:'1','2','3','.','#'. 现在要你在'.'的地方修路,使得至少存在一个块'1 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #327 (Div. 2) B. Rebranding 水题
B. Rebranding Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/problem ...
- Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing
http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...
随机推荐
- Castle ActiveRecord配置中需要注意的地方
关于Castle 的开发可参考李会军老师的Castle 开发系列文章,里面有关于ActiveRecord学习实践系列和Castle IOC容器系列两个部分,是比较好的教程. 这里主要说明在Castle ...
- bounds的深入研究
一.bounds的深入研究 1>frame:是以父控件的左上角为原点,描述的是一块区域的可视范围, bounds:是以自己内容左上角为原点,描述的是可视范围在内容范围显示的区域 2> ...
- after I see Little Dorrit
也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...
- POJ 3267 The Cow Lexicon 简单DP
题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...
- docker下使用caffe的命令记录
查看所有的images sudo docker images 利用某个image生成container sudo docker run -it --net=host -v /home/tingting ...
- bzoj 2107: Spoj2832 Find The Determinant III 辗转相除法
2107: Spoj2832 Find The Determinant III Time Limit: 1 Sec Memory Limit: 259 MBSubmit: 154 Solved: ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...
- uva 11922 - Permutation Transformer
splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...
- nodejs对静态文件目录的处理
Serving static files in Express To serve static files such as images, CSS files, and JavaScript file ...
- The APR based Apache Tomcat Native library
Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...