洛谷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 ...
随机推荐
- Saiku_00_资源帖
一.精选 1.李秋 随笔分类 - pentaho 二.概述 1.Saiku + Kylin 多维分析平台探索 三.Saiku+Kylin 1.使用Saiku+Kylin构建多维分析OLAP平台 2.使 ...
- Prototype Chain
参考资料: <javascript权威指南 第六版> <javascript高级程序设计 第二版> 写在前面的话 所谓的原型链就是一个一个的对象通过其__proto__属性连接 ...
- hdu Digital Square(广搜)
题目:给出n,求出最小的m,满足m^2 % 10^k = n,其中k=0,1,2 http://acm.hdu.edu.cn/showproblem.php?pid=4394 只要有一个x满足条件便 ...
- uimsbf和 bslbf的含义
bslbf代表位串,即“Bit string, left bit first ”, uimsbf代表无符号整数,即”unsinged integer, most significant bit fir ...
- 【LeetCode】060. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- JS undefined
undefined表示"缺少值",就是此处应该有一个值,但是还没有定义.典型用法是: (1)变量被声明了,但没有赋值时,就等于undefined. (2) 调用函数时,应该提供的参 ...
- Oracle12c多租户如何连接到CDB或PDB、CDB与PDB容器切换
Oracle 数据库 12 c 多租户选项允许单个容器数据库 (CDB) 来承载多个单独的可插拔数据库 (PDB).那么我们如何连接到容器数据库 (CDB) 和可插拔数据库 (PDB). 1. V$S ...
- IAR常用快捷键及技巧
1.复制和粘贴几行的部分代码 需求:有时候我们需要复制几行代码的后半部分,不需要复制前半部分.方法:按住Alt键,再用鼠标拖动就可以复制和粘贴后半部分 [END/2015-09-23] 2.复制一行 ...
- SQL 常用语句收集
1.UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 2.SELECT * FROM TableA INNER JOIN TableB ON TableA.name = T ...
- 【转】 Pro Android学习笔记(三三):Menu(4):Alternative菜单
目录(?)[-] 什么是Alternative menu替代菜单 小例子说明 Alternative menu代码 关于Category和规范代码写法 关于flags 多个匹配的itemId等参数 什 ...