洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels
题目描述
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:你最少要经过几个浅水区?保证有解。
输入输出格式
输入格式:
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
说明
There are three islands with shallow water paths connecting some of them.
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.
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,ans=0x7fffffff,tot;
int e[][]={{,},{,-},{,},{-,}};
bool vis[][];
char map[][];
void dfs(int x,int y,int cnt,int sum){
if(sum>=ans)return;
if(cnt==tot){
ans=min(ans,sum);
return;
}
for(int i=;i<;i++){
int xx=x+e[i][],yy=y+e[i][];
if(xx<=n&&xx>=&&yy<=m&&yy>=&&map[xx][yy]!='.'&&!vis[xx][yy]){
vis[xx][yy]=;
dfs(xx,yy,cnt+(map[xx][yy]=='X'),sum+(map[xx][yy]=='S'));
vis[xx][yy]=;
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%s",map[i]+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(map[i][j]=='X')tot++;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(map[i][j]=='X'){
memset(vis,,sizeof(vis));
vis[i][j]=;
dfs(i,j,,);
}
}
}
cout<<ans;
}
36分 暴力
/*
因为题目要求以连通块为单位,所以就先求出所有'X'所在的连通块,flag[i][j]就是i,j位置上的点所在的联通块,num[i]是编号为i的连通块的大小,然后以每个连通块为起点进行spfa,经过所有spfa后可以得出任意两连通块之间的最短路。就可以开始dp啦!
dp[S][j]表示走过点集S到达j的最短路径,转移:dp[S/j][k] + dis[k,j]
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define INF 0x7fffffff
#define MAXN 16
#define maxn 55
int e[][]={{,},{,-},{,},{-,}};
int n,m,flag[maxn][maxn],vis[maxn][maxn],num[maxn],d[maxn][maxn],cnt;
int dis[maxn][maxn],dp[<<MAXN][MAXN];
char g[maxn][maxn];
struct Node{
int x,y;
}block[MAXN][MAXN];
queue<Node>q;
void bfs(int sx,int sy){//记录第一个连通块的位置
q.push(Node{sx,sy});
vis[sx][sy]=;
flag[sx][sy]=cnt;
++num[cnt];
block[cnt][num[cnt]]=Node{sx,sy};
while(!q.empty()){
Node now=q.front();q.pop();
for(int i=;i<;i++){
int xx=now.x+e[i][];
int yy=now.y+e[i][];
if(xx<=n&&xx>=&&yy<=m&&yy>=&&!vis[xx][yy]&&g[xx][yy]=='X'){
q.push(Node{xx,yy});
vis[xx][yy]=;
flag[xx][yy]=cnt;
++num[cnt];
block[cnt][num[cnt]]=Node{xx,yy};
}
}
}
}
void spfa(int s){
memset(vis,,sizeof(vis));
memset(dis,/,sizeof(dis));
for(int i=;i<=num[s];i++){
vis[block[s][i].x][block[s][i].y]=;
dis[block[s][i].x][block[s][i].y]=;
q.push(block[s][i]);
}
Node now;
while(!q.empty()){
now=q.front();q.pop();
vis[now.x][now.y]=;
for(int i=;i<;i++){
int xx=now.x+e[i][];
int yy=now.y+e[i][];
if(xx<=||xx>n||yy<=||yy>m||g[xx][yy]=='.')continue;
if(g[xx][yy]=='X'){
if(dis[xx][yy]>dis[now.x][now.y]){
dis[xx][yy]=dis[now.x][now.y];
if(!vis[xx][yy]){
vis[xx][yy]=;
q.push(Node{xx,yy});
}
}
d[flag[xx][yy]][s]=d[s][flag[xx][yy]]=min(d[s][flag[xx][yy]],min(d[flag[xx][yy]][s],dis[xx][yy]));
}
else if(g[xx][yy]=='S'){
if(dis[xx][yy]>dis[now.x][now.y]+){
dis[xx][yy]=dis[now.x][now.y]+;
if(!vis[xx][yy]){
vis[xx][yy]=;
q.push(Node{xx,yy});
}
}
}
}
}
}
void DP(){
memset(dp,/,sizeof(dp));
int tmp=(<<cnt);
for(int i=;i<=cnt;i++)dp[<<(i-)][i]=;
for(int S=;S<tmp;S++){
for(int i=;i<=cnt;i++){
if(!(S&(<<(i-))))continue;
for(int j=;j<=cnt;j++){
if(j==i||!(S&(<<(i-))))continue;
dp[S][i]=min(dp[S][i],dp[S^(<<(i-))][j]+d[j][i]);
}
}
}
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%s",g[i]+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(!vis[i][j]&&g[i][j]=='X'){
++cnt;
bfs(i,j);
}
memset(d,/,sizeof(d));
for(int i=;i<=cnt;i++)
d[i][i]=,spfa(i);
DP();
int S=(<<cnt)-;
int ans=INF;
for(int j=;j<=cnt;j++)
ans=min(ans,dp[S][j]);
printf("%d",ans);
}
100分 状压dp
洛谷P3070 [USACO13JAN]岛游记Island Travels的更多相关文章
- [Luogu3070][USACO13JAN]岛游记Island Travels
题目描述 Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 < ...
- 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心
P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...
- 洛谷P2202 [USACO13JAN]方块重叠Square Overlap
P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...
- 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations
P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...
- 洛谷 P3068 [USACO13JAN]派对邀请函Party Invitations
P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...
- 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
- 洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence
传送门 题目大意: 开始站在原点,给出一系列操作 x L/R,表示向左或向右走几步. 最多会移动到离原点1,000,000,000单位远的地方. n次操作,n<=100000 问走过k次的地方有 ...
- 洛谷 P2205 [USACO13JAN]画栅栏
这题其实没什么,但用到的算法都十分有用.做一个不恰当的比喻,这是一只必须用牛刀杀的鸡,但因为我这个蒟蒻杀不死牛,所以只能找只鸡来练练手. 题目描述 Farmer John 想出了一个给牛棚旁的长围墙涂 ...
- 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence
题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...
随机推荐
- hibernate 框架搭建
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...
- Java_util_02_Java判断字符串是中文还是英文
做微信开发,使用百度翻译API时,需要指定译文的语种.这就需要我们判断待翻译内容是中文还是英文,若是中文,则翻译成英文,若是英文则翻译成中文. 方法一:字符与字节的长度 依据:一个中文占两个字节,一个 ...
- 201621123014《Java程序设计》第四周学习总结
1.本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:继承.多态.子类.父类.final.static.类型判断与类型转换.抽象类. 1.2 尝试使用思维导图将这些关键词组织起来. ...
- raspi-config Expand root partition to fill SD card 原理
/********************************************************************************** * raspi-config E ...
- JQuery基本知识、选择器、事件、DOM操作、动画--2017年2月10日
$(对象)可以将JS对象转换为JQuery对象 .get(0)可以将JQuery对象转换为JS对象 并无太大区别,灵活点出即可
- Python 爬虫闯关(第一关)
在学习爬虫时,遇到了一个有意思的网站,这个网站设置了几个关卡,需要经过爬虫进行闯关,随着关卡的网后,难度不断增加,在闯关的过程中需要学习不同的知识,你的爬虫水平也自然随之提高. 今天我们先来第一关,访 ...
- Linux编程之错误代码
头文件/usr/include/asm-generic/errno-base.h定义错误码: #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERI ...
- RTSP协议分析(二)
以下是某省IPTV的RTSP协商过程: DESCRIBE rtsp://118.122.89.27:554/live/ch10083121594790060557.sdp?playtype=1& ...
- 常用排序算法总结(C语言描述)
最近又把排序给复(yu)习(xi)了一遍,在此总结一下~具体理论思想持续补充完善中... 1.交换排序 (1)普通冒泡 时间复杂度:最差.平均都是O(n^2),最好是O(n) 空间复杂度:O(1) # ...
- 代码实现跟控制器跳转到storyBoard