题目链接: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. scala 遇到过的问题

    1:在我安装完scala的插件后,在打开方法的实现类(open implementactions)的时候,抛出这个异常,后来发现这个异常是因为我的scala的插件跟我eclipse版本不兼容导致的. ...

  2. cocos2dx 3.0rc怎样创建项目

    转自官网的文档. How to Run cpp-tests on win32 In this article, I will show you how to run cpp-tests on your ...

  3. python的list求和与求积

    python中,无论是对的list求和还是求积,我都给出了两种方法. 1.对list求和 1.1 s=0 for i in range(10): s+=i 1.2 s=sum(range(10)) 2 ...

  4. Java 异常介绍

    Java标准库内建了一些通用的异常,这些类以 Throwable 为顶层父类.Throwable又派生出 Error 类和 Exception 类. 错误:Error类以及他的子类的实例,代表了JVM ...

  5. web应用的负载均衡、集群、高可用(HA)解决方案

    看看别人的文章: 1.熟悉几个组件 1.1.apache     —— 它是Apache软件基金会的一个开放源代码的跨平台的网页服务器,属于老牌的web服务器了,支持基于Ip或者域名的虚拟主机,支持代 ...

  6. 关于函数的return

    function add(x, y) { var total = x + y; return total; } add(5,10); 关于函数的return 我一开始是认为没有什么用的  后来在项目中 ...

  7. Laravel开发:多用户登录验证(1)

    之前实现了一次,后来代码忘记放哪了,所以有跳了一次坑. 先贴上Laravel自带的验证代码: 路由:routes/web.php // Authentication Routes... $this-& ...

  8. [转]为 windows cmd 设置代理

    为 windows cmd 设置代理 转自:http://blog.csdn.net/lovelyelfpop/article/details/69586366 通过cmd命令行执行某些命令,如果这些 ...

  9. 九度OJ 1185:特殊排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15588 解决:3592 题目描述: 输入一系列整数,将其中最大的数挑出,并将剩下的数进行排序. 输入: 输入第一行包括1个整数N,1< ...

  10. They're much closer in spirit to how our brains work than feedforward networks.

    http://neuralnetworksanddeeplearning.com/chap1.html Up to now, we've been discussing neural networks ...