The Grove
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 904   Accepted: 444

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.

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

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

Sample Output

13

Source

题意:

    一个n*m(n,m<=50)的矩阵有一片连着的树林,Bessie要从起始位置出发绕林子一圈再回来,每次只能向横着、竖着或斜着走一步。
问最少需多少步才能完成。

分析:

    1.如果搜出的路径能够包围其中的一个点,那么就能包围森林,这样问题就被简化了
2.我们任选一个点作为被包围点,在搜索时利用射线法判断某这个点是否在多边形中
3.判断点在多边形内外最常用的方法就是射线法,即以一条射线穿过多边形次数的奇偶性来判断。
   奇在偶不在。

//dp[x][y][0]表示从起点达到(x,y)的距离
//dp[x][y][1]表示从(x,y)到起点的距离+去的距离

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=;
const int dx[]={,,,-,,,-,-};
const int dy[]={,-,,,-,,-,};
int dp[N][N][];char mp[N][N];
int n,m,sx,sy,gx,gy,px,py,pk,nx,ny,nk;
struct node{
int x,y,k;
node(int x=,int y=,int k=):x(x),y(y),k(k){}
};
bool ok(){
if(nx==gx&&ny<gy){
if(px<nx) return ;
}
if(px==gx&&py<gy){
if(px>nx) return ;
}
return ;
}
void bfs(){
memset(dp,-,sizeof dp);
queue<node>q;
q.push(node(sx,sy,));
dp[sx][sy][]=;
while(!q.empty()){
node t=q.front();q.pop();
px=t.x;py=t.y;pk=t.k;
for(int i=;i<;i++){
nx=px+dx[i];ny=py+dy[i];nk=pk;
if(nx<||ny<||nx>n||ny>m||mp[nx][ny]=='X') continue;
if(ok()) nk^=;
if(!(~dp[nx][ny][nk])){
dp[nx][ny][nk]=dp[px][py][pk]+;
q.push(node(nx,ny,nk));
}
}
}
printf("%d\n",dp[sx][sy][]);
}
int main(){
scanf("%d%d",&n,&m);bool flag=;
for(int i=;i<=n;i++) scanf("%s",mp[i]+);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='*') sx=i,sy=j;
if(!flag&&mp[i][j]=='X') flag=,gx=i,gy=j;
}
}
bfs();
return ;
}

POJ3182 The Grove[射线法+分层图最短路]的更多相关文章

  1. poj3635Full Tank?[分层图最短路]

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7248   Accepted: 2338 Descri ...

  2. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  3. BZOJ 2763 分层图最短路

    突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...

  4. 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)

    [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...

  5. 【网络流24题】 No.14 孤岛营救问题 (分层图最短路)

    [题意] 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛, 营救被敌军俘虏的大兵瑞恩. 瑞恩被关押在一个迷宫里, 迷宫地形复杂, 但幸好麦克得到了迷宫的地形图. 迷宫的外形是 ...

  6. BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路

    BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路 Description “我要成为魔法少女!”     “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切, ...

  7. BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路

    BZOJ_1579_[Usaco2009 Feb]Revamping Trails 道路升级_分层图最短路 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M ...

  8. Nowcoder contest 370H Rinne Loves Dynamic Graph【分层图最短路】

    <题目链接> 题目大意:Rinne 学到了一个新的奇妙的东西叫做动态图,这里的动态图的定义是边权可以随着操作而变动的图.当我们在这个图上经过一条边的时候,这个图上所有边的边权都会发生变动. ...

  9. ACM-ICPC 2018 南京赛区网络预赛 L 【分层图最短路】

    <题目链接> 题目大意: 有N个城市,这些城市之间有M条有向边,每条边有权值,能够选择K条边 边权置为0,求1到N的最短距离. 解题分析: 分层图最短路模板题,将该图看成 K+1 层图,然 ...

随机推荐

  1. 关于webpack的cdn配置

    在webpack中活用cdn可以大幅度减少打包文件的体积,配置方法也非常简单. 首先现在入口html文件(index.html)里引入相应cdn连接,然后配置webpack: module.expor ...

  2. ActiveMQ spring (一)

    在5.8.0版本下 配置成功. 参考文档:http://yinbinhome.iteye.com/blog/1273228

  3. POJ 3249 Test for Job (dfs + dp)

    题目链接:http://poj.org/problem?id=3249 题意: 给你一个DAG图,问你入度为0的点到出度为0的点的最长路是多少 思路: 记忆化搜索,注意v[i]可以是负的,所以初始值要 ...

  4. Guru's Guide to SQL Server Architecture and Internals

    Guru's Guide to SQL Server Architecture and Internals

  5. Android与javaScript的交互

    WebView与js的交互包含两方面,一是在html中通过js调用java代码:二是在安卓java代码中调用js. 一.html中通过js调用java代码 js中调用java代码其实就记住一点,Web ...

  6. numpy基础语法

    np.zeros(5) [ 0.  0.  0.  0.  0.] 所得类型为数组, numpy.zeros_like(a, dtype=None, order='K', subok=True) 生成 ...

  7. 【音乐App】—— Vue-music 项目学习笔记:歌手页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 一.歌手页面布局与设计 需 ...

  8. bat文件转换为exe文件

    批处理文件转换为exe文件(简单的处理文件),点击下载 使用超简单的了,不多说.

  9. linux 文件查找实用技巧

    1.tail catalina.out -n 100000 | grep -niR com.uujimu.utils.ArticleContentReplace.replacNumToA 查找内容,并 ...

  10. 席位分配问题——惯例Q值法和d&#39;hondt法的MATLAB程序

      本篇博文为追忆以前写过的算法系列第四篇 温故知新 本篇于2009年发表于百度博客,当时还没接触CSDN.所以是文学和技术博客混淆,只是这个程序博文訪问量突破2000,有不少网友评论互动.应该 ...