Description

Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 <= N <= 15) islands, which are located on an R x C grid (1 <= R, C <= 50). An island is a maximal connected group of squares on the grid that are marked as 'X', where two 'X's are connected if they share a side. (Thus, two 'X's sharing a corner are not necessarily connected.) Bessie, however, is arriving late, so she is coming in with FJ by helicopter. Thus, she can first land on any of the islands she chooses. She wants to visit all the cows at least once, so she will travel between islands until she has visited all N of the islands at least once. FJ's helicopter doesn't have much fuel left, so he doesn't want to use it until the cows decide to go home. Fortunately, some of the squares in the grid are shallow water, which is denoted by 'S'. Bessie can swim through these squares in the four cardinal directions (north, east, south, west) in order to travel between the islands. She can also travel (in the four cardinal directions) between an island and shallow water, and vice versa. Find the minimum distance Bessie will have to swim in order to visit all of the islands. (The distance Bessie will have to swim is the number of distinct times she is on a square marked 'S'.) After looking at a map of the area, Bessie knows this will be possible.

给你一张r*c的地图,有’S’,’X’,’.’三种地形,所有判定相邻与行走都是四连通的。我们设’X’为陆地,一个’X’连通块为一个岛屿,’S’为浅水,’.’为深水。刚开始你可以降落在任一一块陆地上,在陆地上可以行走,在浅水里可以游泳。并且陆地和浅水之间可以相互通行。但无论如何都不能走到深水。你现在要求通过行走和游泳使得你把所有的岛屿都经过一边。Q:你最少要经过几个浅水区?保证有解。

Input

  • Line 1: Two space-separated integers: R and C.
  • Lines 2..R+1: Line i+1 contains C characters giving row i of the grid. Deep water squares are marked as '.', island squares are marked as 'X', and shallow water squares are marked as 'S'.

Output

  • Line 1: A single integer representing the minimum distance Bessie has to swim to visit all islands.

Sample Input

  1. 5 4
  2. XX.S
  3. .S..
  4. SXSS
  5. S.SX
  6. ..SX

Sample Output

3

HINT

5*4的地图,先走到左上角的岛屿,再向下经过1个’S’区到达中间的岛屿,再向右经过2个’S’区到达右下角的岛屿。(最优路径不一定只有一条)


首先预处理出每个岛屿与岛屿之间的距离,然后考虑到只有N(N<=14)个岛屿,就可以用状压来求

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #define inf 0x7f7f7f7f
  7. using namespace std;
  8. typedef long long ll;
  9. typedef unsigned int ui;
  10. typedef unsigned long long ull;
  11. inline int read(){
  12. int x=0,f=1;char ch=getchar();
  13. for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
  14. for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
  15. return x*f;
  16. }
  17. inline void print(int x){
  18. if (x>=10) print(x/10);
  19. putchar(x%10+'0');
  20. }
  21. const int N=50,M=15;
  22. const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
  23. char map[N+10][N+10];
  24. int Land[N+10][N+10],tot[M+10],dis[N+10][N+10],dist[M+10][M+10];
  25. bool vis[N+10][N+10];
  26. int f[(1<<M)+10][M+10];
  27. int n,m;
  28. struct AC{
  29. int x,y;
  30. void join(int a,int b){x=a,y=b;}
  31. }NewLand[M+10][N*N+10],h[N*N];
  32. void ID_get(int x,int y,int num){
  33. if (x<1||x>n||y<1||y>m||Land[x][y]||map[x][y]!='X') return;
  34. Land[x][y]=num;
  35. NewLand[num][++tot[num]].join(x,y);
  36. for (int i=0;i<4;i++) ID_get(x+dx[i],y+dy[i],num);
  37. }
  38. void spfa(int T){
  39. memset(vis,0,sizeof(vis));
  40. memset(dis,63,sizeof(dis));
  41. int head=1,tail=1;
  42. for (;tail<=tot[T];tail++){
  43. vis[NewLand[T][tail].x][NewLand[T][tail].y]=1;
  44. dis[NewLand[T][tail].x][NewLand[T][tail].y]=0;
  45. h[tail]=NewLand[T][tail];
  46. }
  47. for (;head<=tail;head++){
  48. AC Now=h[head];
  49. for (int k=0;k<4;k++){
  50. int tx=Now.x+dx[k],ty=Now.y+dy[k];
  51. if (tx<1||tx>n||ty<1||ty>m||map[tx][ty]=='.') continue;
  52. if (map[tx][ty]=='S')
  53. if (dis[tx][ty]>dis[Now.x][Now.y]+1){
  54. dis[tx][ty]=dis[Now.x][Now.y]+1;
  55. if (!vis[tx][ty]) h[++tail].join(tx,ty),vis[tx][ty]=1;
  56. }
  57. if (map[tx][ty]=='X'){
  58. if (dis[tx][ty]>dis[Now.x][Now.y]){
  59. dis[tx][ty]=dis[Now.x][Now.y];
  60. if (!vis[tx][ty]) h[++tail].join(tx,ty),vis[tx][ty]=1;
  61. }
  62. dist[T][Land[tx][ty]]=min(dist[T][Land[tx][ty]],dis[tx][ty]);
  63. }
  64. }
  65. vis[Now.x][Now.y]=0;
  66. }
  67. }
  68. int main(){
  69. n=read(),m=read();
  70. int num=0,ans=inf;
  71. for (int i=1;i<=n;i++) scanf("%s",map[i]+1);
  72. for (int i=1;i<=n;i++)
  73. for (int j=1;j<=m;j++)
  74. if (map[i][j]=='X'&&!Land[i][j])
  75. ID_get(i,j,++num);
  76. memset(dist,63,sizeof(dist));
  77. for (int i=1;i<=num;i++) spfa(i);
  78. memset(f,63,sizeof(f));
  79. for (int i=1;i<=num;i++) f[1<<(i-1)][i]=0;
  80. for (int sta=0;sta<1<<num;sta++)
  81. for (int i=1;i<=num;i++)
  82. if (sta&(1<<(i-1)))
  83. for (int j=1;j<=num;j++)
  84. if (i!=j&&(sta&(1<<(j-1))))
  85. f[sta][i]=min(f[sta][i],f[sta^(1<<(i-1))][j]+dist[j][i]);
  86. for (int i=1;i<=num;i++) ans=min(ans,f[(1<<num)-1][i]);
  87. printf("%d\n",ans);
  88. return 0;
  89. }

[Usaco2013 Jan]Island Travels的更多相关文章

  1. BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS

    BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...

  2. bzoj 3048[Usaco2013 Jan]Cow Lineup 思想,乱搞 stl

    3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 168[Subm ...

  3. bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法

    3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 225  Solved: 159[Subm ...

  4. BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针

    BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针 Description Farmer John's N cows (1 <= N <= 100,000) ...

  5. 洛谷P3070 [USACO13JAN]岛游记Island Travels

    P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...

  6. [bzoj 3048] [Usaco2013 Jan]Cow Lineup

    [bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...

  7. 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP

    这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...

  8. [Luogu3070][USACO13JAN]岛游记Island Travels

    题目描述 Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 < ...

  9. 【BZOJ 3050】【USACO2013 Jan】Seating 线段树

    线段树维护4个标记, 昨天互测时题意理解错了,今天上午才发现. #include<cstdio> #include<cstring> #include<algorithm ...

随机推荐

  1. 提示:“请检查浏览器代理设置”/xx-net

    1.删除已导入的证书文件(运行certmgr.msc和certlm.msc,然后自己找到xxnet删),2.更新3.3.1(或是自己找到那行代码取消注释,楼下有人提及)3.删除data文件夹(下的ce ...

  2. uva live 12846 A Daisy Puzzle Game

    假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...

  3. IE将開始屏蔽旧版ActiveX控件

    微软IE团队上周宣布将在IE中屏蔽旧版本号的ActiveX控件以加强IE的安全性.首先会被禁用的旧版本号ActiveX控件包括: J2SE 1.4, 低于update 43 的版本号 J2SE 5.0 ...

  4. Android Studio一些简单设置

          简单设置   1.默认主题设置 默认的 Android Studio 为灰色界面,能够选择使用炫酷的黑色界面. Settings --> Appearance --> Them ...

  5. 【HRS项目】Axure兴许问题解决---与SVN结合

    上一篇博客介绍了Axure的团队开发用法,http://blog.csdn.net/u013036274/article/details/50999139,可是再用的时候发现会出现这种问题,例如以下图 ...

  6. NoSQL之Redis探析

    下载地址:wget http://download.redis.io/releases/redis-2.8.8.tar.gz安装steps:1 下载Official Website : http:// ...

  7. Windows Server 2012 R2 安装.NET Framework 3.5报错

    简单记录一下,Windows Server 2012 R2 安装.NET Framework 3.5报错,下面是解决方法 载入ISO文件Windows Server 2012 R2,而且在安装的过程中 ...

  8. 2016/2/24 css画三角形 border的上右下左的调整 以及内区域的无限变小 边界透明

    网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. 今天 ...

  9. ubuntu安装jdk 1.6

    linux下安装JDK1.6 1. 去http://java.sun.com/j2se/1.4.2/download.html 下载一个Linux Platform的JDK,建议下载RPM自解压格式的 ...

  10. struts2的一些小问题

    1.action和ValueStack的关系2.ValueStack的类set()方法和setValue()方法的区别3.ValueStack的类push()方法的作用4.从ValueStack对象中 ...