【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 186 Solved: 118
[Submit][Status][Discuss]
Description
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 <= R <= 50, 1 <= C <= 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.
.jpg)
Input
* 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).
Output
* Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.
Sample Input
.......
...X...
..XXX..
...XXX.
...X...
......*
Sample Output
HINT
Source
Solution
显然是bfs,但是要求绕障碍一周,所以不能裸上bfs。
考虑从障碍向边界引出一个阻拦边,规定,这个阻拦边右边的点无法穿过该边,但是左边的点可以穿过,这样就可以用这条边来限制,使得可以得到绕圈一周的答案。
具体的方法就是,对于每个点记录dis[0/1][x][y]表示从右/左来的距离,这样在bfs的时候分类讨论一下,就可以了。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int N,M,sx,sy,lx,ly,dis[][][];
struct Pa{int x,y,d;};
int dx[]={,,,,,,-,-,-},dy[]={,,-,,,-,,,-};
bool visit[][],mp[][],line[][];
inline bool OK(int x,int y) {return x>= && x<=N && y>= && y<=M;}
void BFS()
{
queue<Pa>q; q.push((Pa){sx,sy,}); dis[][sx][sy]=;
while (!q.empty())
{
Pa now=q.front(); q.pop();
for (int d=,x,y; d<=; d++)
{
x=now.x+dx[d],y=now.y+dy[d];
if (!OK(x,y) || mp[x][y]) continue;
if (line[now.x][now.y] && y<=now.y) continue;
if (line[x][y] && y>now.y)
if (!dis[][x][y])
dis[][x][y]=dis[][now.x][now.y]+,q.push((Pa){x,y,});
else;
else
if (!dis[now.d][x][y])
dis[now.d][x][y]=dis[now.d][now.x][now.y]+,q.push((Pa){x,y,now.d});
else;
}
}
}
int main()
{
scanf("%d%d",&N,&M); char c[][];
for (int i=; i<=N; i++)
{
scanf("%s",c[i]+);
for (int j=; j<=M; j++)
mp[i][j]=c[i][j]=='X',lx=c[i][j]=='X'? i:lx,ly=c[i][j]=='X'? j:ly,
sx=c[i][j]=='*'? i:sx,sy=c[i][j]=='*'? j:sy;
}
for (int i=; i<=N; i++) if (i+lx<=N) line[i+lx][ly]=;
BFS();
printf("%d\n",dis[][sx][sy]-);
return ;
}
说好了shabi题不发博客...
然而长这么大没写过这样的bfs...
自己明明想到了,却实现出了问题,在讨论的时候naive了....所以留一下长记性
【BZOJ-1656】The Grove 树木 BFS + 射线法的更多相关文章
- bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...
- POJ 3182 The Grove [DP(spfa) 射线法]
题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...
- BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656 题意: 给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点. 所有 ...
- POJ3182 The Grove[射线法+分层图最短路]
The Grove Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 904 Accepted: 444 Descripti ...
- matlab练习程序(射线法判断点与多边形关系)
依然是计算几何. 射线法判断点与多边形关系原理如下: 从待判断点引出一条射线,射线与多边形相交,如果交点为偶数,则点不在多边形内,如果交点为奇数,则点在多边形内. 原理虽是这样,有些细节还是要注意一下 ...
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...
- 射线法(1190 - Sleepwalking )
题目:http://lightoj.com/volume_showproblem.php?problem=1190 参考链接:https://blog.csdn.net/gkingzheng/arti ...
- Codeforces 375C Circling Round Treasures - 最短路 - 射线法 - 位运算
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure wit ...
- WebGL模型拾取——射线法二
这篇文章是对射线法raycaster的补充,上一篇文章主要讲的是raycaster射线法拾取模型的原理,而这篇文章着重讲使用射线法要注意的地方.首先我们来看下图. 我来解释一下上图中的originTr ...
随机推荐
- java web学习总结(二十九) -------------------JavaBean的两种开发模式
SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. 一.JSP+JavaBean开发模式 1 ...
- HttpSession与Hibernate中Session的区别
一.javax.servlet.http.HttpSession是一个抽象接口 它的产生:J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个HttpSession,这个Session是用户 ...
- CSS3写折纸
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 【容器云】十分钟快速构建 Influxdb+cadvisor+grafana 监控
本文作者:七牛云布道师@陈爱珍,DBAPlus社群联合发起人.前新炬技术专家.多年企业级系统的应用运维及分布式系统实战经验.现专注于容器.微服务及DevOps落地的研究与实践. 安装过程 三个都直接下 ...
- 因为没用过,所以没想过的--goto
今天读了读 Rui Maciel 大神写的 mjson parser,mjson 解析器是一个使用 ISO C 实现的小型 JSON 解析器.嵌入式项目中使用到了该解析器,随即拿出来看看. 看到如下代 ...
- linux memcached安装
准备安装包: libevent-2.0.21-stable.tar.gz //Memcached服务端的依赖包 memcached-1.4.29.tar.gz //Memcached服务端 li ...
- Lambda表达式的诞生过程
这是一篇很经典的文章,解决了工作中一些使用过但是又不太明白的知识点,今天终于弄明白了.花了一晚上重新整的,坚决要分享出来!!! 那得从很久很久以前说起了,记得那个时候... 懵懂的记得从前有个叫委托的 ...
- [Erlang 0111] Erlang Abstract Format , Part 2
上回书,我们说到飞天玉虎蒋伯芳来到蜈蚣岭,不是,重来,上回咱们说到可以在Erlang Shell里面手工构造,加载并调用一个模块.在那个demo里面,我把多个Form单独生成出来,最后放在一起做 ...
- mysql
这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter pas ...
- redis数据结构存储Dict设计细节(redis的设计与实现笔记)
说到redis的Dict(字典),虽说算法上跟市面上一般的Dict实现没有什么区别,但是redis的Dict有2个特殊的地方那就是它的rehash(重新散列)和它的字典节点单向链表. 以下是dict用 ...