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. Python基础之字符串操作

    字符串的常用操作包括但不限于以下操作: 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 这里将对字符串的内置操作方法进行总结归纳,重点是以示例的方式进行展示. 使用type获取创建对象的类 ...

  2. (转)#ifndef的用法

    原文链接:http://wenku.baidu.com/link?url=c4doqVo3U429RkwTN5eaJIfD2rEu-1bLKKQXuqO8drmL359PhUjVmzC7P94wBY9 ...

  3. HIVE SQL JOIN

    最近总结了一下hive表关联的用法,与Postgres表关联还是有细微差别,总结在这里方便以后查看. join语法 join_table: table_reference [INNER] JOIN t ...

  4. linux命令学习笔记(6):rmdir 命令

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录 被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.) ...

  5. linux命令学习笔记(56):netstat命令

    netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况. netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UD ...

  6. bzoj 4817: [Sdoi2017]树点涂色 LCT+树链剖分+线段树

    题目: Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. Bob可能会进 ...

  7. 系列文章--突袭HTML5之Javascript

    突袭HTML5之Javascript API扩展5 - 其他扩展 突袭HTML5之Javascript API扩展4 - 拖拽 突袭HTML5之Javascript API扩展3 - 本地存储 突袭H ...

  8. 洛谷【P3908】异或之和

    二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P39 ...

  9. binlog配置和使用

    binlog启用和禁用在/etc/my.cnf文件中添加log-bin=mysql-bin来启用binlog,mysql-bin为日志文件名前缀.如果用户有super权限,可通过set sql_log ...

  10. HDU1698(线段树入门题)

    Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...