思路:如果要围绕一圈,必须经过一条竖线上的一点,把竖线左端封住,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. 两台Linux机之间传送文件

    最近实验室里接管了一台服务器,经常需要用到服务器与自己主机之间进行文件传输,因此,在此介绍一下两台Linux主机之间的一些操作,方便后来者. 1. Linux.Windows主机远程访问Linux服务 ...

  2. ASP.NET-FineUI开发实践-9

    用了FineUI有一段时间了,还是分享下我咋改的吧,没想的那么难,我也是从小白来的. 基础是要懂JQ和EXTJS,主要是要懂JQ和EXTJS能干啥,这里有两个网站 http://www.w3schoo ...

  3. FastReport 动态修改连接字符串

    代码如下: Report rp = new Report(); rp.Load(@"Print\aa.frx"); rp.Dictionary.Connections[0].Con ...

  4. HTML5触摸屏touch事件使用实例1

    1.源码: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...

  5. 反序列化 DateTime对象问题

      今天在Android的Json反序列化过程中,Date类型无法转化成自己想要的格式,鉴于之前在C#的反序列话中也遇到过这个问题,解决的同时,顺手做个总结,供自己及需要的人日后查阅.       将 ...

  6. mobx react

    目录结构: Model/index.js 'use strict'; import { action, autorun, observable, computed } from "mobx& ...

  7. (四)Angularjs - 小实例(2)

    自定义指令编写时钟 模板 <!-- 模板文件 --><html> <!-- 内置的ng-app指令通知编译器启动AngularJS框架--> <body ng ...

  8. WAJUEJI which home strong!

    算法:搜索+优先队列 描述 在一个山沟里,姐弟俩同时考上了大学.但由于家里拮据,所以这并不是什么好消息.父亲对孩子说:我就是砸锅卖铁也要把你们姐俩供出来. 当时的姐姐已经决定放弃上学的机会. 没想到第 ...

  9. Android开发之InstanceState详解(转)

    本文来自:http://www.cnblogs.com/hanyonglu/archive/2012/03/28/2420515.html 本文介绍Android中关于Activity的两个神秘方法: ...

  10. ios nslog 打印字典为中文

    #import <Foundation/Foundation.h> @implementation NSDictionary (Log) - (NSString *)description ...