洛谷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.

输入输出样例

输入样例#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[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool vis[60][60];
char map[60][60];
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=0;i<4;i++){
        int xx=x+e[i][0],yy=y+e[i][1];
        if(xx<=n&&xx>=1&&yy<=m&&yy>=1&&map[xx][yy]!=‘.‘&&!vis[xx][yy]){
            vis[xx][yy]=1;
            dfs(xx,yy,cnt+(map[xx][yy]==‘X‘),sum+(map[xx][yy]==‘S‘));
            vis[xx][yy]=0;
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%s",map[i]+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(map[i][j]==‘X‘)tot++;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(map[i][j]==‘X‘){
                memset(vis,0,sizeof(vis));
                vis[i][j]=1;
                dfs(i,j,1,0);
            }
        }
    }
    cout<<ans;
}

36分 暴力

时间: 2024-08-10 21:27:28

洛谷P3070 [USACO13JAN]岛游记Island Travels的相关文章

【题解】 P3070 [USACO13JAN]岛游记Island Travels

题面有点坑,翻译内容中没有指明n的范围,通过观察原题面得到$n \leq 15$并大致猜测这是一个状态压缩dp 最小生成树显然不可行,可以举例说明存在某种情况某边要经过两次或更多 对于任意一个岛屿$i$到任意一个岛屿$j$的最短距离显然是固定的,每个岛域之间的距离(不经过其他岛屿)可以用bfs预处理出来,初始状态只要将这个岛屿全部坐标位置全部入队即可 得到了任意两个岛屿之间的直接距离后(当然存在部分岛屿不可互达),在$ n \leq 15$时可以直接用$floyd$求解 现在得到任意两个节点之间

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

P2205 [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He simply attaches a paint brush to his favorite cow Bessie, and t

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

P2202 [USACO13JAN]方块重叠Square Overlap 题目描述 Farmer John is planning to build N (2 <= N <= 50,000) square fenced-in pastures on his farm, each of size exactly K x K (1 <= K <= 1,000,000). Pasture i is centered at point (x_i, y_i) with integer coo

【洛谷八连测R7】nzhtl1477-我回来了

[洛谷八连测R7]nzhtl1477-我回来了 题目描述 68号岛有n个商店,有的商店直接有小路连接,小路的长度都为1,格里克告诉了你哪些地方可能有做黄油蛋糕的原料. 但是那个人是个坑货,所以他会告诉你一些商店,然后告诉你距离这些商店距离 <= k的商店中都是可能有原料的. 然后你要把这些可能的商店每个都去一遍,你想知道你要去多少个商店. 由于你是勇者,所以有m次询问 简洁题意: 给你一个图,每次查询的时候给一堆特殊点以及一个数k,求图中有多少点距离至少一个特殊点距离不超过k,边是无向的. 输入

洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little ene

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的

洛谷 P2801 教主的魔法 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2801 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是不超过1000的正整数.教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W.(虽然L=R时并不