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

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

代码如下:

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

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<math.h>
  5. #include<queue>
  6. using namespace std;
  7.  
  8. const int MAXN = ;
  9. const int oo = 1e9+;
  10.  
  11. int dir[][] = { {,},{,},{-,},{,-} };
  12. char G[MAXN][MAXN];
  13. struct node
  14. {
  15. int step[];
  16. }a[MAXN][MAXN];
  17. struct point
  18. {
  19. int x, y;
  20. };
  21.  
  22. void BFS(int M, int N, int k)
  23. {///第k个王国到达所有点的最短距离
  24. queue<point> Q;
  25. point q, s;
  26.  
  27. for(int i=; i<M; i++)
  28. for(int j=; j<N; j++)
  29. {
  30. if(G[i][j] == k+'')
  31. {
  32. q.x = i, q.y = j;
  33. a[i][j].step[k] = ;
  34. Q.push(q);
  35. }
  36. }
  37.  
  38. while(Q.size())
  39. {
  40. q = Q.front();Q.pop();
  41.  
  42. for(int i=; i<; i++)
  43. {
  44. s = q;
  45. s.x += dir[i][];
  46. s.y += dir[i][];
  47.  
  48. if(s.x>=&&s.x<M && s.y>=&&s.y<N && G[s.x][s.y] != '#')
  49. {
  50. int t = (G[s.x][s.y]=='.' ? :);
  51.  
  52. if(a[s.x][s.y].step[k]==- || a[q.x][q.y].step[k]+t < a[s.x][s.y].step[k])
  53. {///因为王国之间没有不用修路,所有可能会回搜
  54. a[s.x][s.y].step[k] = a[q.x][q.y].step[k] + t;
  55. Q.push(s);
  56. }
  57. }
  58. }
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. int M, N;
  65.  
  66. scanf("%d%d", &M, &N);
  67.  
  68. for(int i=; i<M; i++)
  69. scanf("%s", G[i]);
  70.  
  71. memset(a, -, sizeof(a));
  72.  
  73. for(int i=; i<; i++)
  74. BFS(M, N, i);
  75.  
  76. int ans = oo;
  77.  
  78. for(int i=; i<M; i++)
  79. for(int j=; j<N; j++)
  80. {
  81. if(a[i][j].step[]!=- && a[i][j].step[]!=- && a[i][j].step[]!=-)
  82. {///如果这个点三个王国都能够到达
  83. int t = (G[i][j]=='.' ? :);
  84. ans = min(ans, a[i][j].step[]+a[i][j].step[]+a[i][j].step[]-t*);
  85. }
  86. }
  87. if(ans == oo)
  88. ans = -;
  89. printf("%d\n", ans);
  90.  
  91. return ;
  92. }

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. Castle ActiveRecord配置中需要注意的地方

    关于Castle 的开发可参考李会军老师的Castle 开发系列文章,里面有关于ActiveRecord学习实践系列和Castle IOC容器系列两个部分,是比较好的教程. 这里主要说明在Castle ...

  2. bounds的深入研究

    一.bounds的深入研究 1>frame:是以父控件的左上角为原点,描述的是一块区域的可视范围,    bounds:是以自己内容左上角为原点,描述的是可视范围在内容范围显示的区域 2> ...

  3. after I see Little Dorrit

    也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...

  4. POJ 3267 The Cow Lexicon 简单DP

    题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...

  5. docker下使用caffe的命令记录

    查看所有的images sudo docker images 利用某个image生成container sudo docker run -it --net=host -v /home/tingting ...

  6. bzoj 2107: Spoj2832 Find The Determinant III 辗转相除法

    2107: Spoj2832 Find The Determinant III Time Limit: 1 Sec  Memory Limit: 259 MBSubmit: 154  Solved: ...

  7. 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 ...

  8. uva 11922 - Permutation Transformer

    splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...

  9. nodejs对静态文件目录的处理

    Serving static files in Express To serve static files such as images, CSS files, and JavaScript file ...

  10. The APR based Apache Tomcat Native library

    Tomcat启动的时候出现下面这样的提示: 2015-11-06 14:24:12 org.apache.catalina.core.AprLifecycleListener init 信息: The ...