The Grove
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 641   Accepted: 297

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

思路:

突破口肯定是必须要围绕果园走一圈了,那么起点到一个点分顺时针和逆时针经过果园用bfs求两次最短路就够了,比赛时想了很久都没想到处理顺时针和逆时针的方法,就只能想到这步了,思维还是不能突破呀。

怎样处理顺时针和逆时针呢?从果园中引一条射线出去与地图边界相交,从起点出发两次bfs,一次控制只能向上经过射线,一次控制只能向下进过射线就够了。因为绕果园必须要经过射线上一点,所以最后用射线上的点来更新答案就OK了。

注意:

那个射线不能随便作的,要保证射线不与果园再次相交。想一想,为什么?

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std; typedef long long ll;
int n,m,ans;
int sx,sy;
int dx[]={-1,1,0,0,-1,-1,1,1};
int dy[]={0,0,-1,1,-1,1,-1,1};
int dist[maxn][maxn][2];
bool vis[maxn][maxn][2];
char mp[maxn][maxn];
char s[maxn];
struct Node
{
int x,y;
}cur,now;
queue<Node>q; void presolve()
{
int i,j,k,flag=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(mp[i][j]=='X')
{
flag=1;
for(k=j-1;k>=1;k--) // 将射线标记
{
if(mp[i][k]=='X') continue ;
mp[i][k]='Y';
}
break ;
}
}
if(flag) break ;
}
}
void bfs(int k)
{
int i,j,nx,ny,tx,ty;
while(!q.empty()) q.pop();
cur.x=sx;
cur.y=sy;
dist[sx][sy][k]=0;
vis[sx][sy][k]=1;
q.push(cur);
while(!q.empty())
{
now=q.front();
q.pop();
nx=now.x;
ny=now.y;
for(i=0;i<8;i++)
{
tx=nx+dx[i];
ty=ny+dy[i];
if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='X'||vis[tx][ty][k]) continue ;
if(k&&mp[tx][ty]=='Y') // 到Y时控制过去的方向就好了
{
if(i==1||i==6||i==7) continue ;
}
else if(!k&&mp[tx][ty]=='Y') // 到Y时控制过去的方向就好了
{
if(i==0||i==4||i==5) continue ;
}
cur.x=tx;
cur.y=ty;
dist[tx][ty][k]=dist[nx][ny][k]+1;
vis[tx][ty][k]=1;
q.push(cur);
}
}
}
void solve()
{
int i,j;
ans=INF;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(mp[i][j]=='Y')
{
ans=min(ans,dist[i][j][0]+dist[i][j][1]);
}
}
}
}
int main()
{
int i,j,t;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;i++)
{
scanf("%s",s);
for(j=1;j<=m;j++)
{
mp[i][j]=s[j-1];
if(mp[i][j]=='*') sx=i,sy=j;
}
}
presolve();
memset(vis,0,sizeof(vis));
bfs(0);
bfs(1);
solve();
printf("%d\n",ans);
}
return 0;
}

poj 3182 The Grove的更多相关文章

  1. poj 3182 The Grove bfs

    思路:如果要围绕一圈,必须经过一条竖线上的一点,把竖线左端封住,bfs一次,枚举点,再把竖线右端封住,再bfs回起点. #include <iostream> #include <c ...

  2. POJ 3182 The Grove [DP(spfa) 射线法]

    题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...

  3. The Grove(poj 3182)

    题意:一个n*m(n,m<=50)的矩阵有一片连着的树林,Bessie要从起始位置出发绕林子一圈再回来,每次只能向横着.竖着或斜着走一步.问最少需多少步才能完成. /* 如果我们用搜索来写的话, ...

  4. [USACO2006][poj3182]The Grove(巧妙的BFS)

    题目:http://poj.org/problem?id=3182 题意:一个棋盘中间有一个联通块,给你一个起点让你从起点开始绕联通块外围一圈并回到起点,求最小步数. 分析: 首先根据数据的范围比较小 ...

  5. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  6. Poj 3982 序列

    1.Link: http://poj.org/problem?id=3982 2.Content: 序列 Time Limit: 1000MS   Memory Limit: 65536K Total ...

  7. POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  8. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  9. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

随机推荐

  1. Linux基本权限

    首先需要我们了解的是,权限(rwx)对于文件和目录的作用是不一样的 . 权限对文件的作用 r : 读取文件内容(cat , more , head , tail) w: 编辑.新增.修改文件内容(vi ...

  2. HTML5和CSS3:游戏的变革Flexbox

    HTML5和CSS3给网络开发者提供了新的语法标签,本地动画工具,服务器端字体等等新增功能,这些并不是结束.开发者正苦于为网页设计挖出一条战壕 - 真正的页面排版工具,事实上,即便是最有前途的CSS3 ...

  3. css实现鼠标移入table时出现滚动条且table内容不移位

    一般是这样: 表格的标题和内容分别由一个table组成,其中表格内容的table由一个class="table-body"的div包裹.css如下 .tContainer .tab ...

  4. jQuery常用事件

    1.$(document).ready() $(document).ready()是jQuery中响应JavaScript内置的onload事件并执行任务的一种典型方式.它和onload具有类似的效果 ...

  5. Sql 格式化工具

    SQL Pretty Printer:目前提供4种使用方式,桌面版本,SSMS(SQL Server Management Studio)插件,VS插件,和提供API接口. SQL Pretty Pr ...

  6. Gnuradio 实验二

    今天根据教程做了实验二 要到了两个新的模块 一个是 FFT SINK, 其作用是按频谱输出信号. 另外一个就是 GUI Notebook ,起作用就是可以将SCOPE SINK 和 FFT SINK ...

  7. python执行shell获取硬件参数写入mysql

    最近要获取服务器各种参数,包括cpu.内存.磁盘.型号等信息.试用了Hyperic HQ.Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy. 于是乎想到用python ...

  8. access数据库管理软件收集下载

    access百科 Microsoft Office Access是由微软发布的关系数据库管理系统.它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点,是 Mi ...

  9. Solr4.8.0源码分析(8)之Lucene的索引文件(1)

    Solr4.8.0源码分析(8)之Lucene的索引文件(1) 题记:最近有幸看到觉先大神的Lucene的博客,感觉自己之前学习的以及工作的太为肤浅,所以决定先跟随觉先大神的博客学习下Lucene的原 ...

  10. 在vs中连接sql的几种连接方式

     sql身份验证:Data Source=.;Initial Catalog=DBName;UID=sa;Pwd=pwd windows身份验证:Data Source=.;Initial Catal ...