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.

输入输出样例

输入样例#1:

5 4
XX.S
.S..
SXSS
S.SX
..SX
输出样例#1:

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的更多相关文章

  1. [Luogu3070][USACO13JAN]岛游记Island Travels

    题目描述 Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 < ...

  2. 洛谷 P3071 [USACO13JAN]座位Seating-线段树区间合并(判断找,只需要最大前缀和最大后缀)+分治+贪心

    P3071 [USACO13JAN]座位Seating 题目描述 To earn some extra money, the cows have opened a restaurant in thei ...

  3. 洛谷P2202 [USACO13JAN]方块重叠Square Overlap

    P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000 ...

  4. 洛谷P3068 [USACO13JAN]派对邀请函Party Invitations

    P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...

  5. 洛谷 P3068 [USACO13JAN]派对邀请函Party Invitations

    P3068 [USACO13JAN]派对邀请函Party Invitations 题目描述 Farmer John is throwing a party and wants to invite so ...

  6. 洛谷 P3071 [USACO13JAN]座位Seating(线段树)

    P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...

  7. 洛谷 P2205 [USACO13JAN]画栅栏Painting the Fence

    传送门 题目大意: 开始站在原点,给出一系列操作 x L/R,表示向左或向右走几步. 最多会移动到离原点1,000,000,000单位远的地方. n次操作,n<=100000 问走过k次的地方有 ...

  8. 洛谷 P2205 [USACO13JAN]画栅栏

    这题其实没什么,但用到的算法都十分有用.做一个不恰当的比喻,这是一只必须用牛刀杀的鸡,但因为我这个蒟蒻杀不死牛,所以只能找只鸡来练练手. 题目描述 Farmer John 想出了一个给牛棚旁的长围墙涂 ...

  9. 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

    题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...

随机推荐

  1. vue-mixins使用注意事项和高级用法

    因为在项目中 mixins(混合)特性使用频率是很高的 有必要熟练掌握官方文档: mixins 实际项目中 一般都存在 列表(list) 这种很常见的使用场景 话再多都不如上demo file: mi ...

  2. Python with MYSQL - sytax problem

    Con= MySQLdb.connect(host=',db='test') cur=Con.cursor() cur.execute('insert into staff_daily(Date,Na ...

  3. Redis安装以及基本操作命令

    Redis安装 cd redis-2.6.14make PREFIX=/usr/local/redis install 可能会出现的错误提示>>提示1:make[3]: gcc:命令未找到 ...

  4. CSS让图标变成白色或黑色实例页面 和css变灰色

    css代码 .black { filter: brightness(0); } .white { filter: brightness(100); } html代码 <h4>原图</ ...

  5. C++ template 声明与定义

    今天编码的时候,发现了一个错误,就是模板代码在链接的时候找不到方法. 情况大概如下: 在 "Manager.h" 中 class Manager { public: templat ...

  6. 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...

  7. p2p视频点播系统开发案例――Myseelite

    项目地址:http://sourceforge.net/projects/myseelite/ 1. MySee公司倡导的开源项目,目前国内功能最强大的一个开源系统. 2. 一个直播(也支持轮播.点播 ...

  8. bzoj 4631: 踩气球 线段树

    题目: Description 六一儿童节到了, SHUXK 被迫陪着M个熊孩子玩一个无聊的游戏:有N个盒子从左到右排成一排,第i个盒子里装着Ai个气球. SHUXK 要进行Q次操作,每次从某一个盒子 ...

  9. Maven(2)-坐标和依赖

    本文简要介绍Maven里面的坐标(coodinate)以及maven依赖管理(Dependency) 一.坐标 先来个截图: 在上图peoject栏目有groupId,artifactId,versi ...

  10. BZOJ2120:数颜色

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...