题目链接: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. java多线程编码注意事项

    Sole purpose of using concurrency is to produce scalable and faster program. But always remember, sp ...

  2. window mysql安装步骤

    window安装mysql(本人系统win10 64位 安装mysql-5.7.10-winx64) 1. 官网下载mysql zip安装包,然后解压到你想安装的目录,假设解压的目录是P:\mysql ...

  3. 配置LANMP环境(8)-- 安装Samba与配置

    Samba套件,将linux下的文件夹共享给windows(本地开发会很方便) 一.安装Samba yum install –y samba 二.配置Samba 1.备份配置文件 cp /etc/sa ...

  4. JSP HTTP 状态码

    JSP HTTP 状态码 HTTP请求与HTTP响应的格式相近,都有着如下结构: 以状态行+CRLF(回车换行)开始 零行或多行头模块+CRLF 一个空行,比如CRLF 可选的消息体比如文件,查询数据 ...

  5. Python3 多线程 学习 threading

    #-*- coding:utf-8 --*- #多线程测试 import time import datetime import threading def worker(): print(" ...

  6. Yii2实用基础学习笔记(二):Html助手和Request组件 [ 2.0 版本 ]

    Html助手 1 .在@app\views\test的index.php中: <?php //引入命名空间 use yii\helpers\Html; ?> <?php //[一]表 ...

  7. java 经典范例

    使用for 循环输出空心菱形 package 开阳; import java.util.Scanner; public class image { public static void main(St ...

  8. zoj 3356 Football Gambling II【枚举+精度问题】

    题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...

  9. 九度OJ 1199:找位置 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2083 解决:1010 题目描述: 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1:a,4 ...

  10. 解决iOS11 UIScrollView下移问题

    iOS11 系统为UIScrollView增加一个contentInsetAdjustmentBehavior属性,默认为UIScrollViewContentInsetAdjustmentAutom ...