题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656

题意:

  给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点。

  所有'X'为一个连通块。

  对于每一个点,你可以向周围八个方向走,均算作一步。

  让你找出一条路径,能够将所有'X'包围。

  问你路径最短为多少。

题解:

  bfs + 射线法。

  找出最上面(x坐标最小)的一个'X',并向上方作一条射线,标记为'#'。

  从起点开始bfs,并且不能穿过射线(即'#'不能到达)。

  最后枚举射线上的每一个点,令lef为左边能够一步到达当前点的最短路径,rig同理。

  所以ans = min (lef + rig + 2)

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define MAX_N 55
#define INF 1000000000 using namespace std; const int dx[]={-,,,,-,-,,};
const int dy[]={,,-,,-,,-,}; struct Coor
{
int x;
int y;
Coor(int _x,int _y)
{
x=_x;
y=_y;
}
Coor(){}
}; int n,m;
int ans=INF;
int dis[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
char c[MAX_N][MAX_N];
Coor start;
Coor tp;
queue<Coor> q; void read()
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>c[i][j];
if(c[i][j]=='*') start=Coor(i,j);
}
}
} void find_line()
{
tp=Coor(INF,INF);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(c[i][j]=='X' && i<tp.x) tp=Coor(i,j);
}
}
for(int i=tp.x-;i>;i--)
{
c[i][tp.y]='#';
}
} Coor get_front()
{
Coor now=q.front();
q.pop();
vis[now.x][now.y]=false;
return now;
} void insert(Coor now)
{
if(vis[now.x][now.y]) return;
q.push(now);
vis[now.x][now.y]=true;
} inline bool is_legal(int x,int y)
{
return x> && x<=n && y> && y<=m && c[x][y]!='X' && c[x][y]!='#';
} void bfs()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,false,sizeof(vis));
insert(start);
dis[start.x][start.y]=;
while(!q.empty())
{
Coor now=get_front();
int x=now.x;
int y=now.y;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(is_legal(nx,ny) && dis[nx][ny]>dis[x][y]+)
{
dis[nx][ny]=dis[x][y]+;
insert(Coor(nx,ny));
}
}
}
} void solve()
{
find_line();
bfs();
for(int i=tp.x-;i>;i--)
{
int x=i;
int y=tp.y;
int lef=min(dis[x][y-],min(dis[x-][y-],dis[x+][y-]));
int rig=min(dis[x][y+],min(dis[x-][y+],dis[x+][y+]));
ans=min(ans,lef+rig+);
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}

BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】的更多相关文章

  1. bzoj:1656 [Usaco2006 Jan] The Grove 树木

    Description The pasture contains a small, contiguous grove of trees that has no 'holes' in the middl ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)

      题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...

  4. 【BZOJ】1656:[Usaco2006 Jan]The Grove 树木(bfs+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...

  5. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  6. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan

    1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MB Description The N (2 & ...

  7. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径

    Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...

  8. bzoj:1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

    Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in ...

  9. BZOJ——1720: [Usaco2006 Jan]Corral the Cows 奶牛围栏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1720 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

随机推荐

  1. EJB是什么?(节选)

    近期的项目中使用了EJB.当时就仅仅知道怎么用,没有深入的去理解.当完毕这个项目之后.再回想项目中使用的技术.框架以及工具的时候,突然感觉对EJB这个概念非常是模糊,于是上网搜一些资料.可是,非常多的 ...

  2. Android Studio gradle 文件中 ${supportLibVersion} 用法

    一般我们在项目中的gradle会添加如下库文件 dependencies { compile 'com.android.support:appcompat-v7:23.1.0' compile 'co ...

  3. 【python系列】python画报表(Chartkick、Flask)(附中文乱码解决方式)

    chartkick 能够画 javascript 报表, 并且比較美观.可是网上搜了下.非常难找到 python 版本号的,于是查了些资料,摸索了下. 对 Flask 也不非常熟悉,这里就仅仅抛砖引玉 ...

  4. 自学宝典:10个学习Android开发的网站推荐

    1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...

  5. React antd嵌入百度编辑器(css加载不到等问题,'offsetWidth' of null)

    之前有看过一些类似的文章,以为嵌入不会遇到太多坑 结果...    其他不说,先来描述下跳坑的过程 先定义Ueditor.js类,这个和网上版本类似 import React, { Component ...

  6. 简单记事本&Java

    目标: 学习java的IO流和文件的打开保存 内容: 使用javaSwing包里面的一些东西,比如按钮.菜单来进行布局 代码: package myNotePad; import java.awt.F ...

  7. hive深入使用

    Hive表的创建和数据类型 https://cwiki.apache.org/confluence/display/Hive/Home 管理表和外部的区别 # 管理表 1. 内部表也称之为MANAGE ...

  8. MySQL时间函数-获取当前时间-时间差

    MySQL中获取当前时间为now(),不同于sqlserver getdate(). SQLServer转MySQL除变化top 1 -> limit 1之后报错: limit [Err] 15 ...

  9. Linux安装Nginx使用反向代理

    nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)1.实验环境准备准备2个服务器,都安装好nginx软件nginx1 192.168.13.79 作为web服务器 (理解为火车票售票点) ...

  10. linux c编程:popen

    我们在执行shell命令比如cat /etc/group | grep root的时候,通过管道的机制将cat /etc/group的结果传递给grep root,然后将结果显示出来 linux中提供 ...