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. repeater控件 + marquee标签 实现文字滚动显示

    各种信息网站.BBS等网站上的公告信息模块的实现 拖出一个repeater控件绑定数据库中要显示的信息 在repeater的 <ItemTemplate> ... </ItemTem ...

  2. Tempdb对SQL Server性能的影响

    转载文章,原文地址:http://www.cnblogs.com/laodao1/archive/2010/04/15/1712395.html1.SQL Server系统数据库介绍 SQL Serv ...

  3. JavaScript 字符串

    字符串属性 属性 描述 constructor 返回创建字符串属性属性的函数 length 返回字符串的长度 prototype 允许您向对象添加属性和方法 字符串方法 Method 描述 charA ...

  4. Smarty环境配置

    Smaty优点:1.代码分离 2.缓存技术 使用步骤: 1.下载Smaty模板 2.将模板中那个lib文件夹复制到项目中(一般为根目录,并且重命名在此命名为Smarty), 3.配置PHP 1.新建一 ...

  5. 设置windows窗口ICON 【windows 编程】【API】【原创】

    1. ICON介绍 最近开始接触windows 编程,因此将自己所接触的一些零散的知识进行整理并记录.本文主要介绍了如何更改windows对话框窗口的ICON图标.这里首先介绍一下windows IC ...

  6. Nginx配置文件nginx.conf详细说明

    Nginx配置文件nginx.conf详细说明 #worker_processes 8; #worker_cpu_affinity 00000001 00000010 00000100 0000100 ...

  7. jquery mobile 栅格化

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. UVA 1594 Ducci Sequence(两极问题)

           Ducci Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   D ...

  9. python 深拷贝和浅拷贝浅析

    简单点说 1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象.id会变化2. copy.deepcopy 深拷贝 拷贝对象及其子对象.id会变化 >>> im ...

  10. FFT快速傅立叶

    Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n.第二行描述一个位数为n的正整数x.第三行描述一个位数为n的正整数y. Output 输出一行 ...