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—numpy.argsort()

    函数将数组的值从小到大排序后,并按照其相对应的索引值输出. 一维数组: >>> a = array([3,1,2]) >>> argsort(a) array([1 ...

  2. 十三 Django框架,CSRF跨站请求伪造

     全局CSRF 如果要启用防止CSRF跨站请求伪造,就需要在中间件开启CSRF #中间件 MIDDLEWARE = [ 'django.middleware.security.SecurityMidd ...

  3. scanf和cin的返回值

    需要连续从标准输入读取数据时,可以采用下面两种不同的方式判断文件结束: [cpp] view plaincopy   int i; while(scanf("%d",&i) ...

  4. codeforces 628B B. New Skateboard (数论)

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. 洛谷 P2777 [AHOI2016初中组]自行车比赛

    题目描述 小雪非常关注自行车比赛,尤其是环滨湖自行车赛.一年一度的环滨湖自行车赛,需要选手们连续比赛数日,最终按照累计得分决出冠军.今年一共有 N 位参赛选手.每一天的比赛总会决出当日的排名,第一名的 ...

  6. python实现redis三种cas事务操作

    cas全称是compare and set,是一种典型的事务操作. 简单的说,事务就是为了存取数据库中同一数据时不破坏操作的隔离性和原子性,从而保证数据的一致性. 一般数据库,比如MySql是如何保证 ...

  7. Ubuntu 14.04开发环境

    安装ssh服务:sudo apt-get install openssh-server 安装vim:sudo apt-get install vim-gtk 安装gparted:sudo apt-ge ...

  8. FPGA, Float 32bit, multiplyier by Verilog

    1, FPGA device, using three 18bit x 18 bit multiplier to implement 32bit float multiplier 2, compari ...

  9. Python:内置函数makestrans()、translate()

    转于:https://blog.csdn.net/u014351782/article/details/46740297 博主:夜-feng 一.makestrans() 格式: str.maketr ...

  10. 使用Sed抽取MySQL安装文档的目录及行号

    sed -nr  -e '/^2.|^shell/=' -e '/^2.|^shell/p' INSTALL-SOURCE |awk '{if (NR%2==1) x=$1; else printf ...