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

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e1+9,inf=1e9;
char a[maxn][maxn],now[maxn][maxn];
int dist[maxn][maxn],d[maxn][maxn],quex[1111111],quey[1111111];
int n,m;
void init()
{
memset(a,0,sizeof(a));
memset(now,0,sizeof(now));
} void bfs(int x,int y,int dist[maxn][maxn])
{
bool visit[maxn][maxn];
memset(dist,50,sizeof(dist));
memset(visit,0,sizeof(visit));
dist[x][y]=0;
int front=1,end=0;
quex[++end]=x;
quey[end]=y;
visit[x][y]=1;
while(front<=end)
{
int nowx=quex[front],nowy=quey[front++];
int tox,toy;
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
{
tox=nowx+i;
toy=nowy+j;
if(tox>n||tox<1) continue;
if(toy>m||toy<1) continue;
if(now[tox][toy]=='X') continue;
if(!visit[tox][toy])
{
visit[tox][toy]=1;
dist[tox][toy]=dist[nowx][nowy]+1;
quex[++end]=tox;
quey[end]=toy;
}
}
}
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
init();
int sx,sy,lowx,lowy;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
char tmp;
tmp=getchar();
if(tmp==' '||tmp=='\n') j--;
else a[i][j]=tmp;
if(tmp=='*') sx=i,sy=j;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='X')
{
lowx=i;
lowy=j;
i=n+1;
break;
}
for(int i=1;i<=n;i++) strcpy(now[i]+1,a[i]+1);
for(int i=lowx;i>=1;i--)
now[i][lowy-1]='X';
bfs(sx,sy,dist); int upx=lowx,upy;
for(int j=m;j>=1;j--)
if(a[upx][j]=='X')
{
upy=j;
break;
} for(int i=1;i<=n;i++) strcpy(now[i]+1,a[i]+1);
for(int i=upx;i>=1;i--)
now[i][upy+1]='X'; int ans=inf;
for(int i=lowx-1;i>=1;i--)
{
bfs(i,lowy,d);
ans=min(ans,dist[i][lowy]+d[sx][sy]);
}
cout<<ans<<endl;
}
return 0;
}

poj 3182 The Grove bfs的更多相关文章

  1. poj 3182 The Grove

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

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

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

  3. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  4. Luogu P2864 [USACO06JAN]树林The Grove(bfs)

    P2864 [USACO06JAN]树林The Grove(bfs) 题面 题目描述 The pasture contains a small, contiguous grove of trees t ...

  5. The Grove(poj 3182)

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

  6. poj 3026 Borg Maze (BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS     Memory Limit:65536KB     64bit IO For ...

  7. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  8. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  9. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

随机推荐

  1. Javascript 第一阶段 学习使用总结

    JavaScript 是一种轻量级的编程语言.JavaScript 是可插入 HTML 页面的编程代码.脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中 ...

  2. Genymotion 模拟器 VirtualBox

    准备 介绍: 1.Genymotion安卓模拟器其实不是普通的模拟器,严格来说,genymotion是虚拟机,被网传定义为模拟器,加载APP的速度比较快,操作起来也很流畅.2.Genymotion依赖 ...

  3. codevs 1269 匈牙利游戏

    /*暴力+乱搞 55分(似乎只有暴力得分了)*/ #include<iostream> #include<cstdio> #include<cstring> #in ...

  4. CAGradientLayer实现色差动画

    效果图: 代码部分: RPGradientAnimationView.h #import <UIKit/UIKit.h> typedef enum : NSUInteger { RPGra ...

  5. jQuery自学笔记(一):初识jQuery

    jQuery 是一个 JavaScript 函数库, jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数,引用jQuery应该注意: <script&g ...

  6. 用javascript预加载图片、css、js的方法研究

    预加载的好处可以让网页更快的呈现给用户,缺点就是可能会增加无用的请求(但图片.css.js这些静态文件可以被缓存),如果用户访问的页面里面的css.js.图片被预加载了,用户打开页面的速度会快很多,提 ...

  7. js 获取节点

    var chils= s.childNodes; //得到s的全部子节点 var par=s.parentNode; //得到s的父节点 var ns=s.nextSbiling; //获得s的下一个 ...

  8. php 防止SQL注入函数

    function inject_check($sql_str) { return eregi('select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/ ...

  9. python: list[-1] 与 list[-1:] 的区别

    >>> l '3.542485\t1.977398\t-1\r\n' >>> l.split() ['3.542485', '1.977398', '-1'] &g ...

  10. JS--图片轮播效果

    搞了很长时间才弄清楚图片轮播效果的原理,理解各个事件发生的原因,浪费了这么长的时间,只怪自己的知识太过于薄弱.现将代码写下,供大家参看,如有不妥之处还望指出,大家一起学习. 功能: 1.点击左右两边的 ...