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 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.
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
XX.S
.S..
SXSS
S.SX
..SX
INPUT DETAILS: There are three islands with shallow water paths connecting some of them.
Sample Output
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
#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); }