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. [转载]delete指针之后应该赋值NULL

    首先,C++标准规定:delete空指针是合法的,没有副作用.但是,delete p后,只是释放了指针指向的内存空间.p并不会自动被置为NULL,而且指针还在,同时还指向了之前的地址. 问题来了,对一 ...

  2. Python 3中bytes和str的分别

    最近把一段py2的代码转换到py3的代码,结果运行到向socket中写数据的代码部分出现了'str' does not support the buffer interface这样一个错误. 一番搜索 ...

  3. ActionResult派生类

    类名 抽象类 父类 功能 ContentResult 根据内容的类型和编码,数据内容. EmptyResult 空方法. FileResult abstract 写入文件内容,具体的写入方式在派生类中 ...

  4. ThinkPHP 发送post请求

    function post($url, $param=array()){ if(!is_array($param)){ throw new Exception("参数必须为array&quo ...

  5. C++ 性能剖析 (一)

    C++ 性能剖析 (一) 性能问题也不是仅仅用“技术”可以解决的,它往往是架构,测试,假设等综合难题.不过,对于一个工程师来说,必须从小做起,把一些“明显”的小问题解决.否则的话积小成多,千里堤坝,溃 ...

  6. Day3 set集合、函数和装饰器

    set特性:无序,不重复,可嵌套 创建setset_example = set("123", "213", "234", "432 ...

  7. Rxjava+Retrofit2+Okhttp3多文件上传(服务器端代码+客户端代码)

    所有代码亲测可用,如有问题,欢迎指正. 首先在ApiService接口文件中新建文件上传接口 public interface ApiService { static final String BAS ...

  8. 创建局域网内远程git仓库,并将本地仓库push推到远程仓库中

    转载请注明出处 http://www.goteny.com/articles/2014/06/136.html http://www.cnblogs.com/zjjne/p/3778640.html ...

  9. 解决octave for windows安装包无法通过SourceForge下载的问题

    近期SourceForge访问不了,可以通过访问SourceForge的ftp镜像ftp://sourceforge.nchc.org.tw/进行下载: ftp下载工具可以使用FileZilla,可在 ...

  10. KEIL C编译器常见警告与错误信息的解决办法

    对于函数的自变量.局部变量和全局变量声明如果没有指定内存类型,则内存模式将成为内定的内存类型.如果指定了内存类型的变量,则不理会内存模式,完全有所指定的内存类型为主.    SMALL模式:小模式   ...