P2864 [USACO06JAN]树林The Grove(bfs)

题面

题目描述

The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take.

Happily, Bessie lives on a simple world where the pasture is represented by a grid with \(R\) rows and \(C\) columns ( \(1 \leq R \leq 50, 1 \leq C \leq 50\) ). Here's a typical example where . is pasture (which Bessie may traverse), X is the grove of trees, * represents Bessie's start and end position, and + marks one shortest path she can walk to circumnavigate the grove (i.e., the answer):

...+...
..+X+..
.+XXX+.
..+XXX+
..+X..+
...+++*

The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path.

牧场里有一片树林,林子里没有坑.

贝茜很想知道,最少需要多少步能围绕树林走一圈,最后回到起点.她能上下左右走,也能走对角线格子.

牧场被分成 \(R\) 行 \(C\) 列( \(1 \leq R \leq 50,1 \leq C \leq 50\) ).下面是一张样例的地图,其中表示贝茜 可以走的空地,“X”表示树林,表示起点.而贝茜走的最近的路已经特别地用“ + ”表示 出来.

题目保证,最短的路径一定可以找到.

输入输出格式

输入格式:

Line \(1\) : Two space-separated integers: \(R\) and \(C\)

Lines \(2..R+1\) : Line \(i+1\) describes row \(i\) with \(C\) characters (with no spaces between them).

输出格式:

Line \(1\) : The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.

输入输出样例

输入样例:

6 7
.......
...X...
..XXX..
...XXX.
...X...
......*

输出样例:

13

思路

艹,这题的边界判断! \(LSK\) 你试试这道题。 --zbtrs

换了三种写法,终于把神犇推荐的这道题 \(AC\) 了...

我们直接拿样例做例子:

.......
...X...
..XXX..
...XXX.
...X...
......*

比如说最上面的那一颗树,我们在它上面做一条射线。

.......
----
...X...
..XXX..
...XXX.
...X...
......*

那么绕树林一周的路线一定要经过这条射线。

我们可以用 \(bfs\) 更新所有点到出发点的最短距离,并且特判那条射线,要求不能跨过它:

typedef pair<int,int> PII;//个人代码习惯
#define mp(a,b) make_pair(a,b)//同上
int a[8]={-1,-1,-1,+0,+0,+1,+1,+1};//x的变化
int b[8]={-1,+0,+1,-1,+1,+1,+0,-1};//y的变化 step[sx][sy]=1;
queue<PII>Q;//队列
Q.push(mp(sx,sy));//放入出发点
while(!Q.empty())
{
int x=Q.front().first,y=Q.front().second;Q.pop();//取出队首点
for(int i=0;i<8;i++)//枚举所有情况
{
int dx=x+a[i],dy=y+b[i];//能到达的点
if(step[dx][dy]||!G[dx][dy]) continue;//不能到达的点和已经到过的点
if(y<=ly&&x==lx&&dx==lx-1) continue;//不能从上往下穿过射线
if(y<=ly&&x==lx-1&&dx==lx) continue;//不能从下往上穿过射线
step[dx][dy]=step[x][y]+1;
Q.push(mp(dx,dy));
}
}

然后就顺利 \(AC\) 了。

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
#define mp(a,b) make_pair(a,b)
int n,m,sx,sy,lx,ly,ans=INT_MAX,G[55][55],step[55][55];
bool is_line_made;
int a[8]={-1,-1,-1,+0,+0,+1,+1,+1};
int b[8]={-1,+0,+1,-1,+1,+1,+0,-1};
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
char ch;
cin>>ch;
if(ch=='*') G[i][j]=1,sx=i,sy=j;
else if(ch=='.') G[i][j]=1;
else if(!lx) lx=i,ly=j;
}
step[sx][sy]=1;
queue<PII>Q;
Q.push(mp(sx,sy));
while(!Q.empty())
{
int x=Q.front().first,y=Q.front().second;Q.pop();
for(int i=0;i<8;i++)
{
int dx=x+a[i],dy=y+b[i];
if(step[dx][dy]||!G[dx][dy]) continue;
if(y<=ly&&x==lx&&dx==lx-1) continue;
if(y<=ly&&x==lx-1&&dx==lx) continue;
step[dx][dy]=step[x][y]+1;
Q.push(mp(dx,dy));
}
}
for(int i=1;i<=ly;i++)
{
if(step[lx][i]&&step[lx-1][i]&&ans>step[lx][i]+step[lx-1][i]) ans=step[lx][i]+step[lx-1][i];
if(step[lx][i]&&step[lx-1][i+1]&&ans>step[lx][i]+step[lx-1][i+1]) ans=step[lx][i]+step[lx-1][i+1];
if(step[lx][i]&&step[lx-1][i-1]&&ans>step[lx][i]+step[lx-1][i-1]) ans=step[lx][i]+step[lx-1][i-1];
}
printf("%d",ans-1);
return 0;
}

Luogu P2864 [USACO06JAN]树林The Grove(bfs)的更多相关文章

  1. P2864 [USACO06JAN]树林The Grove

    P2864 [USACO06JAN]树林The Grove 神奇的射线法+bfs 裸的bfs很难写....... 那么我们找一个最外围障碍点,向图的外边引一条虚拟射线. 蓝后bfs时经过这条射线奇数次 ...

  2. [USACO06JAN]树林The Grove

    树木(grove)Time Limit: 1Sec Memory Limit: 64 MB[Description]牧场里有一片树林,林子里没有坑.贝茜很想知道,最少需要多少步能围绕树林走一圈,最后回 ...

  3. luogu题解P1032字串变换--BFS+STL:string骚操作

    题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...

  4. 【POJ3182】The Grove BFS 最短路径周围

    意甲冠军:给定一个N*M图.,间'X'代表树木(树木必须汇集到森林,非分离),然后,'.'它代表的空间.'*'它代表的起点.现在它需要从起点.一圈,最后回到起点,所经过最少点数. 题目中给的'+'就是 ...

  5. poj 3182 The Grove bfs

    思路:如果要围绕一圈,必须经过一条竖线上的一点,把竖线左端封住,bfs一次,枚举点,再把竖线右端封住,再bfs回起点. #include <iostream> #include <c ...

  6. 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...

  7. 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...

  8. LuoGu P2863 [USACO06JAN]牛的舞会The Cow Prom

    题目传送门 这个题还是个缩点的板子题...... 答案就是size大于1的强连通分量的个数 加一个size来统计就好了 #include <iostream> #include <c ...

  9. luogu P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...

随机推荐

  1. python pywin32学习笔记

    参考博客链接 https://blog.csdn.net/polyhedronx/article/details/81988948 参考博客链接 https://www.cnblogs.com/zha ...

  2. 【学术篇】oj.jzxx.net2701 无根树

    这是一道来自OIerBBS的题目.. 原帖地址:http://www.oierbbs.com/forum.php?mod=viewthread&tid=512?fromuid=71 (似乎是个 ...

  3. JavaScript中对象的3种定义方式

    对象是有特性(属性)和功能(方法)的集合体. 定义对象有以下3种方式: 1.使用系统的new Object()方式定义对象 2.使用对象字面量定义对象( 即使用{}语法糖结构定义对象 ) 3.使用自定 ...

  4. Codeigniter 获取当前的控制器名称和方法名称

    在Codeigniter 可以通过下面两个方法获取当前的控制器名称和方法名称 $this->router->fetch_class(); $this->router->fetc ...

  5. js 面向对象几种数据模式

    一.单例模式: 把描述同一事物的属性和方法放在同一内存空间下,实现了分组的作用,防止同一属性或者方法冲突.我们把这种分组编写代码的模式叫做单例模式即普通的对象. 单例模式是项目开发中最常用的一种开发模 ...

  6. R语言:表格的线图转化

    R语言:表格的线图转化 最先选取的是北京各区普通住宅成交十年(2016年及2006年)涨幅对比.这张图比较plain,主要拿来练习: 1.数据表格的基本整理及计算 2. 数据的初步分析 3.线图的基本 ...

  7. Lucene 全文搜索解析

    一.创建查询对象的方式 对要搜索的信息创建 Query 查询对象,Lucene 会根据 Query 查询对象生成最终的查询语法.类似关系数据库 Sql 语法一样,Lucene 也有自己的查询语法,比如 ...

  8. Docker系列(十一):Kubernetes集群集群部署实践

    Kubernetes分布式集群架构 服务注册和服务发现问题怎么解决的? 分布式通讯的核心就是ip加端口 每个服务分配一个不变的虚拟IP+端口 系统env环境变量里有每个服务的服务名称到IP的映射 如下 ...

  9. jdom xpath定位带xmlns命名空间的节点(转)

    jdom xpath定位带xmlns命名空间的节点 2013-06-29      0个评论       作者:baozhengw 收藏    我要投稿 关键词:jdom xpath xmlns 命名 ...

  10. 2014-VGG-《Very deep convolutional networks for large-scale image recognition》翻译

    2014-VGG-<Very deep convolutional networks for large-scale image recognition>翻译 原文:http://xues ...