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 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
随机推荐
- JBoss 系列九十九:Rest WebService jBPM 6 集成演示样例
概述 jBPM 6 提供 Rest API 供第三方应用整合使用 jBPM 6,本文演示假设通过 Rest API: 启动流程 获取流程实例信息 启动 User Task 完毕 User Task j ...
- msxml 操作xml
1.简介 在.NET平台,微软为C#或托管C++程序员提供了丰富的类库,用以支持各种需求,其中就有对XML文件操作的丰富的类.例如XMLDocument, XmlElement等.但是C++标准库中并 ...
- 跨平台传输中使用base64来保证非ascii码字符串的完整性
首先,我们来看一个例子: byte[] b=new byte[]{2,9,43}; String ss=new String(b,"utf-8"); byte[] b1=ss.ge ...
- 开源消息中间件DotNetMQ
由于这个开源项目对我这种中间件菜鸟很有帮助,因此,我将官方的说明文档翻译如下: Introduction In this article, I will introduce a new and ind ...
- Linux network setting.
Lubuntu network setting. //1. Vi /etc/network/interfaces Add:auto eth0iface eth0 inet dhcp //2. Vi / ...
- mongodb权威指南读书笔记
一个服务器能不能运行多个不同端口的mongo实例? 如果两个对象不相等,hashcode一定不相等:如果两个对象相等,hashcode相等或者不相等? 修改器速度42页 update({},{&quo ...
- Android Studio中常用设置与快捷键
常用设置: 1.Tab不用4个空格Code Style->Java->Tabs and Indents->Use tab characterCode Style->Genera ...
- 解决CENTOS7虚拟机更改静态IP无法启动
在linuxman的编辑中,未出现问题.反复的查看原因未果,后查明是虚拟机所致.1.在开启网络时,有错误提示:Restarting network (via systemctl): Job for ...
- MySQL忘记root密码的解决方案
在实际操作中忘记MySQL的root密码是一件令人很头痛的事情,不要急以下的文章就是介绍MySQL的root密码忘记的时候解决方案,我们可以对其进行如下的步骤重新设置,以下就是文章的详细内容描述. ...
- PHP include 和 require
PHP include 和 require 语句 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容. include 和 require 语句用于在执行流中插入写在其他 ...