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. 【原创】设置EXCEL2010打开多个独立窗口

            最近发现一个奇怪的问题,发现office中的word和ppt在我使用笔记本分屏幕(双屏)的时候都可以将2份文档分别在2个窗口打开,但是在使用excel的时候却发现不行,最后研究发现原因 ...

  2. OnTouchListener

    1.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  3. 以root用户身份在jenkins中运行shell命令

    以下过程是CentOS 1.打开此脚本(使用VIM或其他编辑器): vim /etc/sysconfig/jenkins 2.找到$JENKINS_USER并更改为“root”: $JENKINS_U ...

  4. VS2010 MFC中制作Visual Studio风格的停靠侧栏窗口(CDockablePane里嵌套FormView表单视图)

    VS2010 MFC中制作Visual Studio风格的停靠侧栏窗口(CDockablePane里嵌套FormView表单视图) 1. 在资源窗口里新建一个FormView的Dialog,修改ID为 ...

  5. 用户空间和内核空间通讯之【Netlink 中】

    原文地址:用户空间和内核空间通讯之[Netlink 中] 作者:wjlkoorey258 今天我们来动手演练一下Netlink的用法,看看它到底是如何实现用户-内核空间的数据通信的.我们依旧是在2.6 ...

  6. ASP.NET Web API是如何根据请求选择Action的?[下篇] 【转】

    再<上篇>中我们简单介绍了用于实现Action选择机制的HttpActionSelector,接下来我们来讨论本章最为核心的内 容:ASP.NET Web API如何利用HttpActio ...

  7. Google SEO 学习网站记录

    在搜索结果中创建良好的标题和摘要: https://support.google.com/webmasters/answer/35624?hl=zh-Hans&ref_topic=600194 ...

  8. spring Multiple MongoTemplate

    <!-- 数据环境配置 --> <mongo:repositories base-package="com.my9yu.manager.module.server.repo ...

  9. masm学习

    . .model flat,stdcall option casemap:none ; case sensitive ;http://www.popbook.com/wbbs/topic.asp?l_ ...

  10. TensorFlow笔记六:基于cifar10数据库的AlexNet识别

    准确率只有70%,cpu版本的TF居然跑了两天才跑完,其他方法将继续尝试. 生成数据目录: import numpy as np import os train_label = {} for i in ...