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 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...
随机推荐
- python 下的数据结构与算法---6:6大排序算法
顶先最后推荐:哈哈,意思是放到顶部强调其重要性,但是应该我总结的六种算法看完了后再看的一篇醍醐灌顶的文章 一:冒泡排序(Bubble Sort) 原理:假设有n个数,第一轮时:从第一个元素开始,与相邻 ...
- linux的comm命令
http://blog.csdn.net/apache6/article/details/5789669
- WordPress程序流程分析
index.php 统一入口文件 包含wp-blog-heaer.php 包含wp-load.php 包含wp-config.php 数据库.语言包配置等 包含wp-setting.php 对各种运行 ...
- PHP Calendar 函数
PHP 5 Calendar 函数 函数 描述 cal_days_in_month() 针对指定的年份和历法,返回一个月中的天数. cal_from_jd() 把儒略日计数转换为指定历法的日期. ca ...
- inline-block元素的空白间距解决方法
方法1 <ul><li>item1</li><li>item2</li><li>item3</li><li&g ...
- 你好,C++(29)脚踏两只船的函数不是好函数 5.4 函数设计的基本规则
5.4 函数设计的基本规则 函数是C++程序的基本功能单元,就像一块块砖头可以有规则地垒成一座房子,而一个个函数也可以有规则地组织成一个程序.我们在大量使用他人设计好的函数的同时,也在设计大量的函数 ...
- void *memmove( void* dest, const void* src, size_t count );数据拷贝,不需要CPU帮助
分享到 腾讯微博 QQ空间 新浪微博 人人网 朋友网 memmove 编辑词条 编辑词条 --> memmove用于从src拷贝count个字符到dest,如果目标区域和源区域有重叠的话,m ...
- xdebug使用说明
常用配置 xdebug.var_display_max_children整数类型,默认值128.用于控制通过xdebug_var_dump(),var_dump()方法时显示数组中子数组的个数或对象中 ...
- java学习笔记(12) —— Struts2 通过 xml /json 实现简单的业务处理
XML 1.引入dom4j-2.0.0.jar 2.引入jquery-1.8.2.js 3.新建common.js getInfo = function(){ $.post("getXmlA ...
- JQUERY1.9学习笔记 之基本过滤器(一) 动态选择器
动态选择器:animated Selector 描述:当选择器运行时,选择动态进程中的所有元素.(对动态进程起作用) jQuery( ":animated" ) 注释::anima ...