[USACO 2013 Jan]Island Travels

本文介绍了一种算法问题,即在一个由不同地形组成的地图上找到一种方案,使得能够通过行走和游泳的方式访问所有岛屿,并且游泳的距离最短。文章详细介绍了如何通过两遍搜索确定每块陆地所属岛屿及岛屿间最小距离的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 RxC 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.
给你一张 rc 的地图,有’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.

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

Source
Gold

思路
vijos1456(最小总代价)这道题很像。。。这道题我的题解请点击这里
这道题和最小总代价的区别就是:需要找出每块陆地属于哪个岛屿,还需要找出从一块陆地到另一块的最小代价。这个可以用两遍搜索来解决。剩下的部分就和那一道题一样了。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>

const int maxn=50;
const int maxr=15;
const int inf=1000000000;
const int dx[]= {1,0,-1,0};
const int dy[]= {0,1,0,-1};

struct data
{
    int x,y,d;
};

int n,m,tot,ans=inf;
char map[maxn+1][maxn+1];
int belong[maxn+1][maxn+1];//belong表示每一块陆地是属于哪一块岛屿
int dist[maxn+1][maxn+1];
data q[maxn*maxn+1];
int b[maxn+1][maxn+1],head,tail;
int f[1<<maxr][maxr+1];

int in_range(int x,int y)
{
    return (x>0)&&(x<=n)&&(y>0)&&(y<=m);
}

int search_island(int x,int y)//寻找每一块陆地属于哪一块岛屿
{
    belong[x][y]=tot;
    for(int i=0; i<4; i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if(in_range(nx,ny)&&(map[nx][ny]=='X')&&(!belong[nx][ny]))
        {
            search_island(nx,ny);
        }
    }
    return 0;
}

int search_dist(int id)//寻找岛屿和岛屿之间的距离
{
    memset(b,0,sizeof b);
    head=0;
    tail=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(belong[i][j]==id)
            {
                for(int k=0; k<4; k++)
                {
                    int nx=i+dx[k],ny=j+dy[k];
                    if(in_range(nx,ny)&&(!belong[nx][ny]))
                    {
                        tail++;
                        q[tail].x=nx;
                        q[tail].y=ny;
                        q[tail].d=1;
                    }
                }
            }
        }
    }
    while(head!=tail)
    {
        head++;
        int hx=q[head].x,hy=q[head].y,hd=q[head].d;
        for(int i=0; i<4; i++)
        {
            int nx=hx+dx[i],ny=hy+dy[i];
            if(in_range(nx,ny)&&(!b[nx][ny]))
            {
                if(belong[nx][ny]==0)
                {
                    tail++;
                    q[tail].x=nx;
                    q[tail].y=ny;
                    q[tail].d=hd+1;
                    b[nx][ny]=1;
                }
                if((belong[nx][ny]>0)&&(belong[nx][ny]!=id))
                {
                    dist[id][belong[nx][ny]]=std::min(dist[id][belong[nx][ny]],hd);
                }
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%s",map[i]+1);
        for(int j=1; j<=m; j++)
        {
            if(map[i][j]=='S')
            {
                belong[i][j]=0;
            }
            if(map[i][j]=='.')
            {
                belong[i][j]=-1;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(map[i][j]=='X')
            {
                if(!belong[i][j])
                {
                    tot++;
                    search_island(i,j);
                }
            }
        }
    }
    memset(dist,63,sizeof dist);
    for(int i=1; i<=tot; i++)
    {
        search_dist(i);
    }
    for(int k=1; k<=tot; k++)
    {
        for(int i=1; i<=tot; i++)
        {
            for(int j=1; j<=tot; j++)
            {
                dist[i][j]=std::min(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }
    memset(f,63,sizeof f);
    for(int i=1; i<=tot; i++)
    {
        f[1<<(i-1)][i]=0;
    }
    for(int s=1; s<1<<tot; s++)
    {
        for(int i=1; i<=tot; i++)
        {
            if(s&(1<<(i-1)))
            {
                for(int j=1; j<=tot; j++)
                {
                    if(!(s&(1<<(j-1))))
                    {
                        f[s|(1<<(j-1))][j]=std::min(f[s|1<<(j-1)][j],f[s][i]+dist[i][j]);
                    }
                }
            }
        }
    }
    for(int i=1; i<=tot; i++)
    {
        ans=std::min(ans,f[(1<<tot)-1][i]);
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值