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. AC日记——【模板】KMP字符串匹配 洛谷 3375

    题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...

  2. Educational Codeforces Round 35 A. Nearest Minimums【预处理】

    [题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...

  3. win7下用PyInstaller把Python代码打包成exe文件

    2013-11-05 22:02:14|   1.安装 使用PyInstaller需要安装PyWin32. 下载与Python对应的PyInstaller版本,解压后就算安装好了. 例如,安装了PyI ...

  4. 【强连通分量】 Kosaraju和Tarjan算法 (标准模板+详细注释)

    codevs 题意:求最大强连通分量的大小以及所包含的顶点有哪些 Tarjan算法 #include<iostream> #include<queue> #include< ...

  5. GCJ——Crazy Rows (2009 Round 2 A)

    题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: ...

  6. Interactive Extensions简介

    自.net 3.5起,MS在System.Linq命名空间下的Enumerable对象中提供了一组IEnumerable的扩展方法,从而极大的方便了我们的查询操作.尽管如此,由于IEnumerable ...

  7. 查看linux 系统 当前使用的网卡

    使用ifconfig命令查看到linux 系统有三个网卡 ,其实我只要其中一个启用就可以了,用什么命令查看或者切换网卡,或者停用掉其他两个网卡? watch cat /proc/net/dev 看下哪 ...

  8. html使用示例

    select标签 <select name="Area" id="Area" class="sel"> <option v ...

  9. EasyMvc入门教程-基本控件说明(3)时间线

    我们有时候经常看到如下的页面: 或者快递物流信息图标,那么利用EasyMvc如何实现呢?很简单,看下面的例子: @{ var data=new List<TimeLineItem>() { ...

  10. linux中read用法

    read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...