poj 3182 The Grove bfs
思路:如果要围绕一圈,必须经过一条竖线上的一点,把竖线左端封住,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的更多相关文章
- poj 3182 The Grove
The Grove Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 641 Accepted: 297 Descripti ...
- POJ 3182 The Grove [DP(spfa) 射线法]
题意: 给一个地图,给定起点和一块连续图形,走一圈围住这个图形求最小步数 本来是要做课件上一道$CF$题,先做一个简化版 只要保证图形有一个点在走出的多边形内就可以了 $hzc:$动态化静态的思想,假 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- Luogu P2864 [USACO06JAN]树林The Grove(bfs)
P2864 [USACO06JAN]树林The Grove(bfs) 题面 题目描述 The pasture contains a small, contiguous grove of trees t ...
- The Grove(poj 3182)
题意:一个n*m(n,m<=50)的矩阵有一片连着的树林,Bessie要从起始位置出发绕林子一圈再回来,每次只能向横着.竖着或斜着走一步.问最少需多少步才能完成. /* 如果我们用搜索来写的话, ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- POJ 1979 dfs和bfs两种解法
fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- poj 3126 Prime Path( bfs + 素数)
题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
随机推荐
- android Bluetooth(官方翻译)
Bluetooth Using the Bluetooth APIs, an Android application can perform the following: 使用蓝牙APIs,一个And ...
- Vim的多窗口模式管理
Vim中的多窗口打开 vim中,默认的多窗口打开,是横向分割窗口. 进入vim编辑器以后,可以通过new命令,新建一个子窗口 :new “新建一个未命名窗口 :new name "新建一个 ...
- Shell基本的命令
ubuntu 中文乱码 如果使用的是 PuTTY,可以通过修改 font, character set 设置来解决. Window -> Appearance -> Font settin ...
- 分析器错误消息: 未能加载类型“WebApplication._Default”
1.新建一个空白解决方案2.新闻一个Web Application项目 默认就有Default.aspx 直接调试的时候出现-------------------------------------- ...
- 使用Canvas实现下雪功能
示例代码: <html> <head> <meta http-equiv="Content-Type" content="text/html ...
- discuznt学习笔记
DBWR=DbHelper(client) Discuz.Data部分 DbHelper相当与抽象工厂中的Client,其中定义了需要与数据库进行操作的通用方法(如ExecuteScalar,Fi ...
- DHCP租约时间工作原理
问题: 很多用户在使用路由器的DHCP服务器过程中都有一个疑问,DHCP有个设置项目是设置DHCP地址的租约时间,如果设置的比较短,是否会出现租约时间到了以后会重新去获取ip地址,造成用户断网? ...
- (四)Angularjs - 小实例(2)
自定义指令编写时钟 模板 <!-- 模板文件 --><html> <!-- 内置的ng-app指令通知编译器启动AngularJS框架--> <body ng ...
- 武汉科技大学ACM:1007: 陶陶摘苹果
Problem Description 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试. 个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹 ...
- Centos 中 vim 的配置
工欲善其事,必先利其器,我们要用好 vim 就先来把它配置的顺手一点,这样可以大大提高我们的工作学习效率 1.跳转指令 Ctags1 从下面地址下载ctags,将其中的ctags.exe复制到vim目 ...