3049: [Usaco2013 Jan]Island Travels
Time Limit: 10 Sec
Memory Limit: 128 MB
Submit: 73
Solved: 32
[ Submit][ Status][ Discuss]
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’
区到达右下角的岛屿。(最优路径不一定只有一条)
Source
题解:状压dp+spfa
这道题测试的时候,看到只有15个岛屿,想到了状压DP但是在预处理的时候卡住了,所有就只写了一个DFS的暴力,刚开始加了一些减枝优化,但是可能是处理不当,加上之后就卡死在里面了,最后没调出来就直接交了一个小暴力。
然后考完之后,看了看题解就顿悟了。
首先先由DFS搜索出所有的连通块(即岛屿),然后给每个岛屿中的点相同的边号,然后用spfa预处理求出每两个连通块之间最少需要经过的浅水区,进行状压dp。
dp 方程如下:dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dis[j+1][k+1]); dp[i][j]表示处在i这种状态,最后到达j 这个岛。
用二进制表示状态,0表示处在当前位置的岛屿没被访问过,1表示访问过。
上面之所以是dis[j+1][k+1] 因第一个岛屿处在第一位,需要左移0位。当然转移之前需要判断状态i 中是否已经访问过j,然后进行转移就可以。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 67
using namespace std;
int n,m,dis[N][N],len[N][N][N],p1[100000],p2[100000];
int map[N][N],c[N][N],can[N][N],dp[1<<15][15];
int x[10]={0,-1,0,1},y[10]={-1,0,1,0},tot;
const int inf=1e9;
struct data
{
int x,y;
};
void dfs(int x1,int y1,int k)
{
for (int i=0;i<4;i++)
{
int xx=x1+x[i]; int yy=y1+y[i];
if (xx>0&&yy>0&&xx<=n&&yy<=m&&!map[xx][yy]&&!can[xx][yy])
{
can[xx][yy]=1; c[xx][yy]=k;
dfs(xx,yy,k);
}
}
}
void spfa(int fx,int fy,int cnt)
{
memset(can,0,sizeof(can));
int k=c[fx][fy]; len[k][fx][fy]=0; can[fx][fy]=1;
memset(p1,0,sizeof(p1)); memset(p2,0,sizeof(p2));
int h=0; int t=0;
t++; p1[t]=fx; p2[t]=fy;
while (h<=t)
{
h++;
int nowx=p1[h]; int nowy=p2[h];
for (int i=0;i<4;i++)
{
int xx=nowx+x[i]; int yy=nowy+y[i];
if (xx>0&&yy>0&&xx<=n&&yy<=m&&map[xx][yy]!=-1)
{
if (len[k][xx][yy]>len[k][nowx][nowy]+map[xx][yy])
{
len[k][xx][yy]=len[k][nowx][nowy]+map[xx][yy];
if (!can[xx][yy])
{
can[xx][yy]=1;
t++; p1[t]=xx; p2[t]=yy;
}
}
}
}
can[nowx][nowy]=0;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (k!=c[i][j])
dis[k][c[i][j]]=dis[c[i][j]][k]=min(dis[k][c[i][j]],len[k][i][j]);
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
char s[100]; scanf("%s",s);
for (int j=1;j<=m;j++)
switch(s[j-1])
{
case '.':map[i][j]=-1; break;
case 'S':map[i][j]=1; break;
case 'X':map[i][j]=0; break;
}
}
memset(dis,127/3,sizeof(dis));
memset(len,127/3,sizeof(len));
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (!map[i][j]&&!can[i][j])
{
can[i][j]=1; tot++; c[i][j]=tot;
dfs(i,j,tot);
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (!map[i][j])
spfa(i,j,c[i][j]);
memset(dp,127/3,sizeof(dp));
for (int i=0;i<tot;i++) dp[1<<i][i]=0;
for (int i=0;i<(1<<tot);i++)
for (int j=0;j<tot;j++)
if ((i>>j)&1)
for (int k=0;k<tot;k++)
dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dis[j+1][k+1]);
int ans=inf;
for (int i=0;i<tot;i++)
ans=min(ans,dp[(1<<tot)-1][i]);
printf("%d",ans);
}