【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 ...
随机推荐
- Lind.DDD.SSO单点登陆组件的使用(原创)
回到目录 一般sso的说明 在Lind.DDD框架里,有对单点登陆的集成,原理就是各个网站去sso网站统一登陆授权,之后在sso网站将登陆的token进行存储,存储方式随你(cache,redis,m ...
- linux(八)__yum工具
自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载.安装. 什么是yum? yum工具实例: 因为yum指定的软件仓库源是国外的网站,在国内访问较慢.我们更改它的仓库源. 首先先备 ...
- jQuery 中bind(),live(),delegate(),on() 区别(转)
当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...
- 【初探IONIC】不会Native可不可以开发APP?
前言 Hybrid技术流行已经有一段日子了,楼主的关注点也一直围绕着移动端围绕着Hybrid相关展开,Hybrid已经是大大提升开发效率的开发方式了,但是仍然需要至少一个IOS与Andriod,那么可 ...
- 使用python处理子域名爆破工具subdomainsbrute结果txt
近期学习了一段时间python,结合自己的安全从业经验,越来越感觉到安全测试是一个体力活.如果没有良好的coding能力去自动化的话,无疑会把安全测试效率变得很低. 作为安全测试而言,第一步往往要通过 ...
- js与native交互
js与native交互 UIWebView Native调用JS,使用stringByEvaluatingJavaScriptFromString来解释执行js脚本. //script即为要执行的js ...
- 「视频直播技术详解」系列之七:直播云 SDK 性能测试模型
关于直播的技术文章不少,成体系的不多.我们将用七篇文章,更系统化地介绍当下大热的视频直播各环节的关键技术,帮助视频直播创业者们更全面.深入地了解视频直播技术,更好地技术选型. 本系列文章大纲如下: ...
- 小tips合集
No. 1 同一个文本文件里的行结束符如果不一致,比如有些行结束符是0D0A-Windows风格的,而有些行又是UNIX风格的0A,在这种混杂情况下,VIM将非UNIX风格的显示为^M,但如果都是0D ...
- 烂泥:zabbix3.0安装与配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 这个月又快过完了,最近也比较忙,没时间写文章,今天挤点时间把zabbix3.0安装与配置 ...
- 浏览器控制台js代码与后台不同步
原因:浏览器会缓存js 如果是将js代码直接通过<script>标签插入jsp页面中则不存在这个问题 在加载页面的时候会重新加载js代码 如果直接将js代码以文件的形式引入,那么每次在修改 ...