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
Output
题意已经很清楚了,,首先就是建图问题,很显然的广搜,,但是我的广搜打了几十行,,一道题打了将近两个小时。。
广搜时顺便统计出各陆地间的距离,广搜建好图之后,,就是装压dp(数据范围15,,标算太明显了。。)f[i][j]表示当前位置为第j个陆地,i是已经经过的陆地的集合,,然后转移即可。显然当前状态可以转移到下一个可以抵达的陆地,同时将当前岛屿加到集合里即 f[i| 2^j ][to]=min(f[i| 2^j ][to],f[i][j]+dis[j][to]);
虽然代码很长,,但跑的飞快 233
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int f[32780][18],to[18][18],tot,r,c,dis[2700];
char a[52][52]; bool v[2700];
queue<int> q;
int hash(int i,int j) { return (i-1)*c+j; }
void re(int &i,int &j,int s) { i=(s-1)/c+1;j=(s-1)%c+1; }
void mark(int i,int j,int now) {
if(i<1||i>r||j<1||j>c) return;
a[i][j]=now;
if(a[i-1][j]=='X') mark(i-1,j,now);
if(a[i+1][j]=='X') mark(i+1,j,now);
if(a[i][j-1]=='X') mark(i,j-1,now);
if(a[i][j+1]=='X') mark(i,j+1,now);
}
void dfs(int i,int j,int now) {
if(i<1||i>r||j<1||j>c) return;
a[i][j]=0;dis[hash(i,j)]=0;
if(a[i-1][j]==now) dfs(i-1,j,now);
if(a[i+1][j]==now) dfs(i+1,j,now);
if(a[i][j-1]==now) dfs(i,j-1,now);
if(a[i][j+1]==now) dfs(i,j+1,now);
if(a[i-1][j]=='S') q.push(hash(i,j));
else if(a[i+1][j]=='S') q.push(hash(i,j));
else if(a[i][j-1]=='S') q.push(hash(i,j));
else if(a[i][j+1]=='S') q.push(hash(i,j));
}
void bfs(int fr) {
memset(v,1,sizeof(v));
while(!q.empty()) {
int i,j,s=q.front();re(i,j,s);q.pop();v[s]=1;
if(a[i-1][j]=='S') {
int x=hash(i-1,j);
if(dis[x]>dis[s]+1) {
dis[x]=dis[s]+1;
if(v[x]) q.push(x),v[x]=0;
}
}
if(a[i+1][j]=='S') {
int x=hash(i+1,j);
if(dis[x]>dis[s]+1) {
dis[x]=dis[s]+1;
if(v[x]) q.push(x),v[x]=0;
}
}
if(a[i][j-1]=='S') {
int x=hash(i,j-1);
if(dis[x]>dis[s]+1) {
dis[x]=dis[s]+1;
if(v[x]) q.push(x),v[x]=0;
}
}
if(a[i][j+1]=='S') {
int x=hash(i,j+1);
if(dis[x]>dis[s]+1) {
dis[x]=dis[s]+1;
if(v[x]) q.push(x),v[x]=0;
}
}
if(a[i-1][j]!=0&&a[i-1][j]!=fr&&a[i-1][j]<=16) {
to[a[i-1][j]-1][fr-1]=to[fr-1][a[i-1][j]-1]=min(to[fr-1][a[i-1][j]-1],dis[s]);
}
if(a[i+1][j]!=0&&a[i-1][j]!=fr&&a[i+1][j]<=16) {
to[a[i+1][j]-1][fr-1]=to[fr-1][a[i+1][j]-1]=min(to[fr-1][a[i+1][j]-1],dis[s]);
}
if(a[i][j-1]!=fr&&a[i][j-1]!=0&&a[i][j-1]<=16) {
to[a[i][j-1]-1][fr-1]=to[fr-1][a[i][j-1]-1]=min(to[fr-1][a[i][j-1]-1],dis[s]);
}
if(a[i][j+1]!=fr&&a[i][j+1]!=0&&a[i][j+1]<=16) {
to[a[i][j+1]-1][fr-1]=to[fr-1][a[i][j+1]-1]=min(to[fr-1][a[i][j+1]-1],dis[s]);
}
}
}
void work() {
memset(to,127/3,sizeof(to));
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
if(a[i][j]=='X') mark(i,j,++tot);
}
}
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++)
if(a[i][j]>0&&a[i][j]<=16) {
memset(dis,127/3,sizeof(dis));
int x=a[i][j];dfs(i,j,x);
bfs(x);
}
}
}
int main() {
scanf("%d%d",&r,&c);
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
char z;while((z=getchar())!='.'&&z!='S'&&z!='X');
a[i][j]=z;
}
} work();
memset(f,127/3,sizeof(f));
for(int i=0;i<=tot;i++) f[0][i]=0,to[i][16]=1;
int o=1<<tot;
for(int i=0;i<o;i++) {
for(int j=0;j<tot;j++) {
for(int t=0;t<=16;t++) {
if(to[j][t])
f[i|(1<<j)][t]=min(f[i|(1<<j)][t],f[i][j]+to[j][t]);
}
}
} printf("%d",f[o-1][16]-1);
}