jzoj 2158: 【USACO】Island Travels

                                            2158: 【USACO】Island Travels

时间限制: 1.000 Sec  内存限制: 64 MB
提交: 22  解决: 19
[命题人:][下载数据: 70]

提交状态报告

题目描述

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.

输入

* 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

提示

INPUT DETAILS:
There are three islands with shallow water paths connecting some of them.

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.

来源/分类

USACO 2013 January Gold 

题解:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<queue>
#include<cstring>
using namespace std;
const int Max=50;
struct node{
    int x,y,cnt;
    node(){}
    node(int a,int b,int c){x=a,y=b,cnt=c;}
}P[20];
struct qu{
    int cnt;
    node pos[260];
}Pl[20];
int R,C,Ans=INT_MAX,all,flag;
int Dp[(1<<15)+5][20];
int dd[4][2]={{0,1},{-1,0},{1,0},{0,-1}};
int have[Max+5][Max+5],way[20][20];
char map[Max+5][Max+5];
bool vis[Max+5][Max+5],use[Max+5];
void getint(int &num){
    char c;int flag=1;num=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
    while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
    num*=flag;
}
bool haveway(int x,int y){
    if(map[x-1][y]=='S'||map[x][y-1]=='S'||map[x+1][y]=='S'||map[x][y+1]=='S')
        return 1;
    return 0;
}
queue<node>Q;
void together(int x,int y){
    ++flag;
    vis[x][y]=1;
    P[flag]=node(x,y,0);
    have[x][y]=flag;
    Q.push(P[flag]);
    int xx,yy;
    if(haveway(x,y))    Pl[flag].pos[++Pl[flag].cnt]=node(x,y,0);
    while(!Q.empty()){
        node tmp=Q.front();
        Q.pop();
        have[tmp.x][tmp.y]=flag;
        for(int i=0; i<4; ++i){
            xx=tmp.x+dd[i][0];
            yy=tmp.y+dd[i][1];
            if(!vis[xx][yy]&&map[xx][yy]=='X'){
                vis[xx][yy]=1;
                have[xx][yy]=flag;
                Q.push(node(xx,yy,0));
                if(haveway(xx,yy))
                    Pl[flag].pos[++Pl[flag].cnt]=node(xx,yy,0);
            }
        }
    }
}
int Bfs(int s){
    queue<node>q;
    int xx,yy;
    for(int i=1; i<=Pl[s].cnt; ++i)
        q.push(Pl[s].pos[i]),vis[Pl[s].pos[i].x][Pl[s].pos[i].y]=1;
    while(!q.empty()){
        node tmp=q.front();
        q.pop();
        for(int i=0; i<4; ++i){
            xx=tmp.x+dd[i][0];
            yy=tmp.y+dd[i][1];
            if(!vis[xx][yy]&&map[xx][yy]=='X'&&have[xx][yy]!=have[P[s].x][P[s].y]){
                vis[xx][yy]=1;
				way[s][have[xx][yy]]=min(tmp.cnt,way[s][have[xx][yy]]);
			}
            else if(!vis[xx][yy]&&map[xx][yy]=='S'){
                vis[xx][yy]=1;
                q.push(node(xx,yy,tmp.cnt+1));
            }
        }
    }
    return 0;
}
int main(){
    //freopen("island.in","r",stdin);
    //freopen("island.out","w",stdout);
    getint(R),getint(C);
    for(int i=1; i<=R; ++i)
        scanf("%s",map[i]+1);
    for(int i=1; i<=R; ++i)
        for(int j=1; j<=C; ++j)if(map[i][j]=='X'&&!have[i][j])
            together(i,j);
	memset(way,0x3f,sizeof(way));
    for(int i=1; i<=flag; ++i){
		way[i][i]=0;
		memset(vis,0,sizeof(vis));
        Bfs(i);
	}
	for(int k=1; k<=flag; ++k)
		for(int i=1; i<=flag; ++i)
			for(int j=1; j<=flag; ++j)if(way[i][k]+way[j][k]<way[i][j])
				way[i][j]=way[i][k]+way[j][k];
	memset(Dp,0x3f,sizeof(Dp));
    for(int i=0; i<flag; ++i)
		Dp[(1<<i)][i]=0;
	int N=(1<<flag);
	for(int i=0; i<N; ++i)
		for(int j=0; j<flag; ++j)
			if((i>>j)&1)
				for(int k=0; k<flag; ++k)
					Dp[i|(1<<k)][k]=min(Dp[i|(1<<k)][k],Dp[i][j]+way[j+1][k+1]);
	Ans=0x3f3f3f3f;
	for(int i=0; i<flag; ++i)
		Ans=min(Ans,Dp[(1<<flag)-1][i]);
    printf("%d\n",Ans);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值