[Usaco2013 Jan]Island Travels

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

Sample Output

3

HINT

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

首先预处理出每个岛屿与岛屿之间的距离,然后考虑到只有N(N<=14)个岛屿,就可以用状压来求

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+'0');
}
const int N=50,M=15;
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
char map[N+10][N+10];
int Land[N+10][N+10],tot[M+10],dis[N+10][N+10],dist[M+10][M+10];
bool vis[N+10][N+10];
int f[(1<<M)+10][M+10];
int n,m;
struct AC{
    int x,y;
    void join(int a,int b){x=a,y=b;}
}NewLand[M+10][N*N+10],h[N*N];
void ID_get(int x,int y,int num){
    if (x<1||x>n||y<1||y>m||Land[x][y]||map[x][y]!='X') return;
    Land[x][y]=num;
    NewLand[num][++tot[num]].join(x,y);
    for (int i=0;i<4;i++)   ID_get(x+dx[i],y+dy[i],num);
}
void spfa(int T){
    memset(vis,0,sizeof(vis));
    memset(dis,63,sizeof(dis));
    int head=1,tail=1;
    for (;tail<=tot[T];tail++){
        vis[NewLand[T][tail].x][NewLand[T][tail].y]=1;
        dis[NewLand[T][tail].x][NewLand[T][tail].y]=0;
        h[tail]=NewLand[T][tail];
    }
    for (;head<=tail;head++){
        AC Now=h[head];
        for (int k=0;k<4;k++){
            int tx=Now.x+dx[k],ty=Now.y+dy[k];
            if (tx<1||tx>n||ty<1||ty>m||map[tx][ty]=='.')   continue;
            if (map[tx][ty]=='S')
                if (dis[tx][ty]>dis[Now.x][Now.y]+1){
                    dis[tx][ty]=dis[Now.x][Now.y]+1;
                    if (!vis[tx][ty])   h[++tail].join(tx,ty),vis[tx][ty]=1;
                }
            if (map[tx][ty]=='X'){
                if (dis[tx][ty]>dis[Now.x][Now.y]){
                    dis[tx][ty]=dis[Now.x][Now.y];
                    if (!vis[tx][ty])   h[++tail].join(tx,ty),vis[tx][ty]=1;
                }
                dist[T][Land[tx][ty]]=min(dist[T][Land[tx][ty]],dis[tx][ty]);
            }
        }
        vis[Now.x][Now.y]=0;
    }
}               
int main(){
    n=read(),m=read();
    int num=0,ans=inf;
    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'&&!Land[i][j])
                ID_get(i,j,++num);
    memset(dist,63,sizeof(dist));
    for (int i=1;i<=num;i++)    spfa(i);
    memset(f,63,sizeof(f));
    for (int i=1;i<=num;i++)    f[1<<(i-1)][i]=0;
    for (int sta=0;sta<1<<num;sta++)
        for (int i=1;i<=num;i++)
            if (sta&(1<<(i-1)))
                for (int j=1;j<=num;j++)
                    if (i!=j&&(sta&(1<<(j-1))))
                        f[sta][i]=min(f[sta][i],f[sta^(1<<(i-1))][j]+dist[j][i]);
    for (int i=1;i<=num;i++)    ans=min(ans,f[(1<<num)-1][i]);
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一道经典的单调栈问题。题目描述如下: 有 $n$ 个湖,第 $i$ 个湖有一个高度 $h_i$。现在要在这些湖之间挖一些沟渠,使得相邻的湖之间的高度差不超过 $d$。请问最少需要挖多少个沟渠。 这是一道单调栈的典型应用题。我们可以从左到右遍历湖的高度,同时使用一个单调栈来维护之前所有湖的高度。具体来说,我们维护一个单调递增的栈,栈中存储的是湖的下标。假设当前遍历到第 $i$ 个湖,我们需要在之前的湖中找到一个高度最接近 $h_i$ 且高度不超过 $h_i-d$ 的湖,然后从这个湖到第 $i$ 个湖之间挖一条沟渠。具体的实现可以参考下面的代码: ```c++ #include <cstdio> #include <stack> using namespace std; const int N = 100010; int n, d; int h[N]; stack<int> stk; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); int ans = 0; for (int i = 1; i <= n; i++) { while (!stk.empty() && h[stk.top()] <= h[i] - d) stk.pop(); if (!stk.empty()) ans++; stk.push(i); } printf("%d\n", ans); return 0; } ``` 这里的关键在于,当我们遍历到第 $i$ 个湖时,所有比 $h_i-d$ 小的湖都可以被舍弃,因为它们不可能成为第 $i$ 个湖的前驱。因此,我们可以不断地从栈顶弹出比 $h_i-d$ 小的湖,直到栈顶的湖高度大于 $h_i-d$,然后将 $i$ 入栈。这样,栈中存储的就是当前 $h_i$ 左边所有高度不超过 $h_i-d$ 的湖,栈顶元素就是最靠近 $h_i$ 且高度不超过 $h_i-d$ 的湖。如果栈不为空,说明找到了一个前驱湖,答案加一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值