[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;
}
[Usaco2013 Jan]Island Travels的更多相关文章
- BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS
BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...
- bzoj 3048[Usaco2013 Jan]Cow Lineup 思想,乱搞 stl
3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 237 Solved: 168[Subm ...
- bzoj3048[Usaco2013 Jan]Cow Lineup 尺取法
3048: [Usaco2013 Jan]Cow Lineup Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 225 Solved: 159[Subm ...
- BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针
BZOJ_3048_[Usaco2013 Jan]Cow Lineup _双指针 Description Farmer John's N cows (1 <= N <= 100,000) ...
- 洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...
- [bzoj 3048] [Usaco2013 Jan]Cow Lineup
[bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...
- 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP
这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...
- [Luogu3070][USACO13JAN]岛游记Island Travels
题目描述 Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 < ...
- 【BZOJ 3050】【USACO2013 Jan】Seating 线段树
线段树维护4个标记, 昨天互测时题意理解错了,今天上午才发现. #include<cstdio> #include<cstring> #include<algorithm ...
随机推荐
- http://www.doframe.com/jetoolweb/index.html
http://www.doframe.com/jetoolweb/index.html http://www.doframe.com/jetoolweb/html/tasks/orders.html# ...
- tomcat配置访问项目时不需要加项目名称
原文:http://blog.csdn.net/coolcoffee168/article/details/52582770 java web部署后,访问项目的时候,需要在地址中添加项目名称,那么如何 ...
- 使用图像扫描控件ScanOnWeb实现在线图像扫描
今天上网查资料,看到一篇文章,描述的是一个开发OA软件的公司解决浏览器嵌入式扫描仪编程的文章,文章描述了改OA厂商的工程师如何辛苦的克服了各种技术难题,最终实现了在线图像扫描处理,然后又在无数个不眠的 ...
- 【网络】TCP的拥塞控制
一.拥塞控制的一般原理 拥塞:对网络中某一资源的需求超过了该资源所能提供的可用部分 拥塞控制是防止过多的数据注入到网络,这样可以使网络中的路由器或链路不致过载,拥塞控制是一个全局性的过程. 流量控制往 ...
- markdown 插入latex公式练习
markdown 插入latex公式 $$公式$$表示行间公式,本来Tex中使用\(公式\)表示行内公式,但因为Markdown中\是转义字符,所以在Markdown中输入行内公式使用\\(公式\\) ...
- History(历史)命令用法 15 例
如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFORMAT ...
- DWR(AJAX)+Highcharts绘制曲线图,饼图
基本需求: 1. 在前台会用DWR框架(或者AJAX)调用Java后台代码获取要在Hightcharts展示的数据 2. 了解JSON(JavaScript Object Notation)的格式 3 ...
- Solr 文章集成
Solr 文章集成 solr原理 solr wiki: http://wiki.apache.org/solr/ 分布式全文检索系统SolrCloud简单介绍 http://my.oschina.ne ...
- 2016/05/25 empty() 与 isset()的区别
对于初学php的人来说,empty()和和isset()用法的区别是很难搞清楚的,他们的用法的差别不仔细去琢磨的话确实很难弄清楚. 先说一下他们的共同点: 都可以判定一个变量是否为空: 都返回bool ...
- iOS开发——高级篇——iOS涂鸦画板效果实现
一个简单的绘图应用,模仿苹果自带软件备忘录里的涂鸦功能 核心代码 #import "DrawView.h" #import "DrawPath.h" @inte ...