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):

  1. ...+...

  2. ..+X+..

  3. .+XXX+.

  4. ..+XXX+

  5. ..+X..+

  6. ...+++*

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

  1. 6 7
  2. .......
  3. ...X...
  4. ..XXX..
  5. ...XXX.
  6. ...X...
  7. ......*

Sample Output

  1. 13

Source

题意:

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

分析:

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

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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. const int N=;
  6. const int dx[]={,,,-,,,-,-};
  7. const int dy[]={,-,,,-,,-,};
  8. int dp[N][N][];char mp[N][N];
  9. int n,m,sx,sy,gx,gy,px,py,pk,nx,ny,nk;
  10. struct node{
  11. int x,y,k;
  12. node(int x=,int y=,int k=):x(x),y(y),k(k){}
  13. };
  14. bool ok(){
  15. if(nx==gx&&ny<gy){
  16. if(px<nx) return ;
  17. }
  18. if(px==gx&&py<gy){
  19. if(px>nx) return ;
  20. }
  21. return ;
  22. }
  23. void bfs(){
  24. memset(dp,-,sizeof dp);
  25. queue<node>q;
  26. q.push(node(sx,sy,));
  27. dp[sx][sy][]=;
  28. while(!q.empty()){
  29. node t=q.front();q.pop();
  30. px=t.x;py=t.y;pk=t.k;
  31. for(int i=;i<;i++){
  32. nx=px+dx[i];ny=py+dy[i];nk=pk;
  33. if(nx<||ny<||nx>n||ny>m||mp[nx][ny]=='X') continue;
  34. if(ok()) nk^=;
  35. if(!(~dp[nx][ny][nk])){
  36. dp[nx][ny][nk]=dp[px][py][pk]+;
  37. q.push(node(nx,ny,nk));
  38. }
  39. }
  40. }
  41. printf("%d\n",dp[sx][sy][]);
  42. }
  43. int main(){
  44. scanf("%d%d",&n,&m);bool flag=;
  45. for(int i=;i<=n;i++) scanf("%s",mp[i]+);
  46. for(int i=;i<=n;i++){
  47. for(int j=;j<=m;j++){
  48. if(mp[i][j]=='*') sx=i,sy=j;
  49. if(!flag&&mp[i][j]=='X') flag=,gx=i,gy=j;
  50. }
  51. }
  52. bfs();
  53. return ;
  54. }

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. .NET下XML文件的读写

    一.前言: XML是微软.Net战略的一个重要组成部分,而且它可谓是XML Web服务的基石,所以掌握.Net框架下的XML技术自然显得非常重要了.本文将指导大家如何运用C#语言完成.Net框架下的X ...

  2. Codeforces Gym100735 D.Triangle Formation (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    日常训练题解 D.Triangle Formation You are given N wooden sticks. Your task is to determine how many triang ...

  3. Ueditor 在线编辑器使用

    ueditor在线编辑器插件 地址:http://ueditor.baidu.com/website/ 试用体验: 帮助文档:http://fex.baidu.com/ueditor/   实操 引入 ...

  4. OceanBase 2.1 的ORACLE兼容性能力探秘

    概述 OceanBase是一款通用的分布式关系型数据库,目前内部业务使用比较多有两个版本:1.4和2.1.OceanBase每个版本变化总能带给人很多惊喜,其中2.1版本实现了ORACLE很多特性的兼 ...

  5. luogu P2949 [USACO09OPEN]工作调度Work Scheduling

    题目描述 Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make mon ...

  6. codevs 1450 xth 的旅行

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 毕业了,Xth很高兴,因为他要和他的 ra ...

  7. fs寄存器相关,PEB,TEB

    ---恢复内容开始--- FS寄存器指向:偏移 说明000 指向SEH链指针004 线程堆栈顶部008 线程堆栈底部00C SubSystemTib010 FiberData014 Arbitrary ...

  8. C++ 11中几个我比较喜欢的语法(二)

    之前在文章C++ 11中几个我比较喜欢的语法中介绍了几个我比较喜欢的C++语法,其中有些语法由于VC 11还不支持,无法跨平台,所以没有介绍.前几天VS 2013 Preview发布后,对C++ 11 ...

  9. Code signing is required for product type Unit Test Bundle in SDK iOS 8.0

    I fixed the issue (temporarily) by going to Edit Scheme, then in the Build section, removing my unit ...

  10. iOS升级经验分享

    作者认为,及时关注.快速反应.覆盖测试是面对iOS系统升级时最重要的三大原则,文中还详细分析了iCloud Storage和Automatic Reference Counting这两大iOS 5新特 ...