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

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

5 4
XX.S
.S..
SXSS
S.SX
..SX
INPUT DETAILS: There are three islands with shallow water paths connecting some of them.

Sample Output

3
OUTPUT DETAILS: Bessie can travel from the island in the top left to the one in the middle, swimming 1 unit,
and then travel from the middle island to the one in the bottom right, swimming 2 units, for a total of 3 units.

HINT

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

设f[i][j]表示连通状态为i当前在j这个岛屿的最小花费。
BFS处理出两个岛屿之间有多少个浅水区。
状压DP即可。
 
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define _min(x,y) ((x)<(y)?(x):(y))
int tx[]={1,0,0,-1};
int ty[]={0,1,-1,0};
int map[55][55],dis[17][17],f[1<<15][17],idx[55][55],vis[55][55],dep[55][55],tot,Q[5050],cnt,n,m,l,r,p1,mp[1<<15];
char ch[55];
void bfs(int sx,int sy) {
int i; tot++; cnt++;
l=r=0;
Q[r++]=sx; Q[r++]=sy; idx[sx][sy]=cnt;
while(l<r) {
int x=Q[l++],y=Q[l++]; vis[x][y]=tot;
for(i=0;i<4;i++) {
int dx=x+tx[i],dy=y+ty[i];
if(dx<1||dx>n||dy<1||dy>m) continue;
if(map[dx][dy]==0&&vis[dx][dy]!=tot) {
vis[dx][dy]=tot; idx[dx][dy]=cnt;
Q[r++]=dx; Q[r++]=dy;
}
}
}
}
void dfs(int x,int y,int d) {
int i;Q[r++]=x; Q[r++]=y; dep[x][y]=d;
int tmp=idx[x][y];
dis[p1][tmp]=_min(dis[p1][tmp],d);
for(i=0;i<4;i++) {
int dx=x+tx[i],dy=y+ty[i];
if(dx>=1&&dx<=n&&dy>=1&&dy<=m) {
if(map[dx][dy]==0&&vis[dx][dy]!=tot) {
vis[dx][dy]=tot;
dfs(dx,dy,d);
}
}
}
}
void get_dis() {
p1++;
int i,j; tot++; dis[p1][p1]=0;l=r=0;
for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(idx[i][j]==p1) Q[r++]=i,Q[r++]=j,vis[i][j]=tot,dep[i][j]=0;
while(l<r) {
int x=Q[l++],y=Q[l++];
for(i=0;i<4;i++) {
int dx=x+tx[i],dy=y+ty[i];
if(dx<1||dx>n||dy<1||dy>m) continue;
if(map[dx][dy]==1&&vis[dx][dy]!=tot) {
vis[dx][dy]=tot;
dfs(dx,dy,dep[x][y]+1);
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
int i,j,k,o,q;
for(i=1;i<=n;i++) {
scanf("%s",ch+1);
for(j=1;j<=m;j++) {
if(ch[j]=='.') map[i][j]=2;
else if(ch[j]=='X') map[i][j]=0;
else map[i][j]=1;
}
}
for(i=1;i<=n;i++) {
for(j=1;j<=m;j++) {
if(!vis[i][j]&&map[i][j]==0) bfs(i,j);
}
}
memset(f,0x3f,sizeof(f));
for(i=1;i<=cnt;i++) f[1<<(i-1)][i]=0,mp[1<<(i-1)]=i;
memset(dis,0x3f,sizeof(dis));
for(i=1;i<=cnt;i++) get_dis();
int mask=(1<<cnt)-1;
for(i=1;i<mask;i++) {
for(o=i;o;o-=o&(-o)) {
j=mp[o&(-o)];
for(q=mask-i;q;q-=q&(-q)) {
k=mp[q&(-q)];
f[i|(1<<(k-1))][k]=_min(f[i|(1<<(k-1))][k],f[i][j]+dis[j][k]);
}
}
}
int ans=1<<30;
for(i=1;i<=cnt;i++) ans=_min(ans,f[mask][i]);
printf("%d\n",ans);
}

BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS的更多相关文章

  1. BZOJ_1076_[SCOI2008]奖励关_状压DP

    BZOJ_1076_[SCOI2008]奖励关_状压DP 题意: 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛 ...

  2. BZOJ_2064_分裂_状压DP

    BZOJ_2064_分裂_状压DP Description 背景: 和久必分,分久必和... 题目描述: 中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力. 同时经常搞OI的 ...

  3. BZOJ_5369_[Pkusc2018]最大前缀和_状压DP

    BZOJ_5369_[Pkusc2018]最大前缀和_状压DP Description 小C是一个算法竞赛爱好者,有一天小C遇到了一个非常难的问题:求一个序列的最大子段和. 但是小C并不会做这个题,于 ...

  4. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  5. 【BZOJ2595_洛谷4294】[WC2008]游览计划(斯坦纳树_状压DP)

    上个月写的题qwq--突然想写篇博客 题目: 洛谷4294 分析: 斯坦纳树模板题. 简单来说,斯坦纳树问题就是给定一张有边权(或点权)的无向图,要求选若干条边使图中一些选定的点连通(可以经过其他点) ...

  6. [poj1185]炮兵阵地_状压dp

    炮兵阵地 poj-1185 题目大意:给出n列m行,在其中添加炮兵,问最多能加的炮兵数. 注释:n<=100,m<=10.然后只能在平原的地方建立炮兵. 想法:第2到状压dp,++.这题显 ...

  7. [bzoj4006][JLOI2015]管道连接_斯坦纳树_状压dp

    管道连接 bzoj-4006 JLOI-2015 题目大意:给定一张$n$个节点$m$条边的带边权无向图.并且给定$p$个重要节点,每个重要节点都有一个颜色.求一个边权和最小的边集使得颜色相同的重要节 ...

  8. [bzoj1879][Sdoi2009]Bill的挑战_动态规划_状压dp

    Bill的挑战 bzoj-1879 Sdoi-2009 题目大意: 注释:$1\le t \le 5$,$1\le m \le 15$,$1\le length \le 50$. 想法: 又是一个看数 ...

  9. [bzoj3717][PA2014]Pakowanie_动态规划_状压dp

    Pakowanie bzoj-3717 PA-2014 题目大意:给你n个物品m个包,物品有体积包有容量,问装下这些物品最少用几个包. 注释:$1\le n\le 24$,$1\le m\le 100 ...

随机推荐

  1. c++ 高效并发编程

    高效并发编程 并发编程的基本模型包括,通过消息机制来管理运行顺序的message passing, 通过互斥保护共享的shared memory. 线程同步的基本原则 最低限度共享变量,考虑使用imm ...

  2. BZOJ 1090 字符串折叠(Hash + DP)

    题目链接 字符串折叠 区间DP.$f[l][r]$为字符串在区间l到r的最小值 正常情况下 $f[l][r] = min(f[l][r], f[l][l+k-1]+f[l+k][r]);$ 当$l$到 ...

  3. [bzoj1110][POI2007]砝码Odw_贪心

    bzoj-1110 POI-2007 砝码Odw 参考博客:http://hzwer.com/4761.html 题目大意:在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件 ...

  4. Callable和Runnable和FutureTask

    http://www.cnblogs.com/dolphin0520/p/3949310.html 一.Callable与Runnable 二.Future 三.FutureTask 四.使用示例 一 ...

  5. iOS release版本去除NSLog打印信息

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...

  6. BroadcastReceiver详解(一)

    今天我们来讲一下Android中BroadcastReceiver的相关知识. BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在And ...

  7. 用df命令显示磁盘使用量和占用率。

    使用“df -k”命令,以k为单位显示磁盘使用量和占用率. root@gsg43:/tmp# df -kFilesystem     1K-blocks    Used Available Use% ...

  8. react 实现pure render的时候,bind(this)隐患

    react 实现pure render的时候,bind(this)隐患 export default class Parent extends Component { ... render() { c ...

  9. 多线程网页爬虫 python 实现

    采用了多线程和锁机制,实现了广度优先算法的网页爬虫. 对于一个网络爬虫,如果要按广度遍历的方式下载,它就是这样干活的:         1.从给定的入口网址把第一个网页下载下来         2.从 ...

  10. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...