洛谷P3070 [USACO13JAN]岛游记Island Travels
题目描述
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:你最少要经过几个浅水区?保证有解。
输入输出格式
输入格式:
-
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'.
输出格式:
- Line 1: A single integer representing the minimum distance Bessie has to swim to visit all islands.
输入输出样例
5 4 XX.S .S.. SXSS S.SX ..SX
3
说明
There are three islands with shallow water paths connecting some of them.
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.
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,m,ans=0x7fffffff,tot; int e[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; bool vis[60][60]; char map[60][60]; void dfs(int x,int y,int cnt,int sum){ if(sum>=ans)return; if(cnt==tot){ ans=min(ans,sum); return; } for(int i=0;i<4;i++){ int xx=x+e[i][0],yy=y+e[i][1]; if(xx<=n&&xx>=1&&yy<=m&&yy>=1&&map[xx][yy]!='.'&&!vis[xx][yy]){ vis[xx][yy]=1; dfs(xx,yy,cnt+(map[xx][yy]=='X'),sum+(map[xx][yy]=='S')); vis[xx][yy]=0; } } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%s",map[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(map[i][j]=='X')tot++; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(map[i][j]=='X'){ memset(vis,0,sizeof(vis)); vis[i][j]=1; dfs(i,j,1,0); } } } cout<<ans; }
/* 因为题目要求以连通块为单位,所以就先求出所有'X'所在的连通块,flag[i][j]就是i,j位置上的点所在的联通块,num[i]是编号为i的连通块的大小,然后以每个连通块为起点进行spfa,经过所有spfa后可以得出任意两连通块之间的最短路。就可以开始dp啦! dp[S][j]表示走过点集S到达j的最短路径,转移:dp[S/j][k] + dis[k,j] */ #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; #define INF 0x7fffffff #define MAXN 16 #define maxn 55 int e[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int n,m,flag[maxn][maxn],vis[maxn][maxn],num[maxn],d[maxn][maxn],cnt; int dis[maxn][maxn],dp[1<<MAXN][MAXN]; char g[maxn][maxn]; struct Node{ int x,y; }block[MAXN][MAXN]; queue<Node>q; void bfs(int sx,int sy){//记录第一个连通块的位置 q.push(Node{sx,sy}); vis[sx][sy]=1; flag[sx][sy]=cnt; ++num[cnt]; block[cnt][num[cnt]]=Node{sx,sy}; while(!q.empty()){ Node now=q.front();q.pop(); for(int i=0;i<4;i++){ int xx=now.x+e[i][0]; int yy=now.y+e[i][1]; if(xx<=n&&xx>=1&&yy<=m&&yy>=1&&!vis[xx][yy]&&g[xx][yy]=='X'){ q.push(Node{xx,yy}); vis[xx][yy]=1; flag[xx][yy]=cnt; ++num[cnt]; block[cnt][num[cnt]]=Node{xx,yy}; } } } } void spfa(int s){ memset(vis,0,sizeof(vis)); memset(dis,127/3,sizeof(dis)); for(int i=1;i<=num[s];i++){ vis[block[s][i].x][block[s][i].y]=1; dis[block[s][i].x][block[s][i].y]=0; q.push(block[s][i]); } Node now; while(!q.empty()){ now=q.front();q.pop(); vis[now.x][now.y]=0; for(int i=0;i<4;i++){ int xx=now.x+e[i][0]; int yy=now.y+e[i][1]; if(xx<=0||xx>n||yy<=0||yy>m||g[xx][yy]=='.')continue; if(g[xx][yy]=='X'){ if(dis[xx][yy]>dis[now.x][now.y]){ dis[xx][yy]=dis[now.x][now.y]; if(!vis[xx][yy]){ vis[xx][yy]=1; q.push(Node{xx,yy}); } } d[flag[xx][yy]][s]=d[s][flag[xx][yy]]=min(d[s][flag[xx][yy]],min(d[flag[xx][yy]][s],dis[xx][yy])); } else if(g[xx][yy]=='S'){ if(dis[xx][yy]>dis[now.x][now.y]+1){ dis[xx][yy]=dis[now.x][now.y]+1; if(!vis[xx][yy]){ vis[xx][yy]=1; q.push(Node{xx,yy}); } } } } } } void DP(){ memset(dp,127/3,sizeof(dp)); int tmp=(1<<cnt); for(int i=1;i<=cnt;i++)dp[1<<(i-1)][i]=0; for(int S=1;S<tmp;S++){ for(int i=1;i<=cnt;i++){ if(!(S&(1<<(i-1))))continue; for(int j=1;j<=cnt;j++){ if(j==i||!(S&(1<<(i-1))))continue; dp[S][i]=min(dp[S][i],dp[S^(1<<(i-1))][j]+d[j][i]); } } } } int main(){ freopen("Cola.txt","r",stdin); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%s",g[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(!vis[i][j]&&g[i][j]=='X'){ ++cnt; bfs(i,j); } memset(d,127/3,sizeof(d)); for(int i=1;i<=cnt;i++) d[i][i]=0,spfa(i); DP(); int S=(1<<cnt)-1; int ans=INF; for(int j=1;j<=cnt;j++) ans=min(ans,dp[S][j]); printf("%d",ans); }