BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】
题目链接: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【射线法】的更多相关文章
- bzoj:1656 [Usaco2006 Jan] The Grove 树木
Description The pasture contains a small, contiguous grove of trees that has no 'holes' in the middl ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...
- 【BZOJ】1656:[Usaco2006 Jan]The Grove 树木(bfs+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1656 神bfs! 我们知道,我们要绕这个联通的树林一圈. 那么,我们想,怎么才能让我们的bfs绕一个 ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- 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 & ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径
Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...
- 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 ...
- 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 ...
随机推荐
- JLink defective
下载了最新的JLink V622g,打开JLink命令行后,提示以下信息 The connected J-Link is defective,Proper operation cannot be gu ...
- 初始化map和list的两种写法
第一种方法(常用方法): //初始化List List list = new ArrayList(); list.add("string1"); list.add("st ...
- centos6.9使用NTFS-3G挂载ntfs文件系统
centos6.9使用NTFS-3G挂载ntfs文件系统 工作中,难免需要到linux 系统上拷贝文件,但linux 自己不支持ntfs,下面就是解决问题的办法. NTFS-3G是一个开源软件,支持在 ...
- 使用Highcharts实现柱状图展示
第一步 新建页面line.html,引入HighCharts核心js文件 <script type="text/javascript" src="../../js/ ...
- 由浅到深理解ROS(2)
ROS文件系统 用户可以直接参看官网:http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念:Packa ...
- SVM支持向量机
支持向量机(Support Vector Machine,SVM)是效果最好的分类算法之中的一个. 一.线性分类器: 一个线性分类器就是要在n维的数据空间中找到一个超平面,通过这个超平面能够把两类数据 ...
- Bootstrap的js插件之轮播(carousel)
轮播请查看下面演示样例.基本已经涵盖最经常使用的一个轮播 <!DOCTYPE html> <html lang="en"> <head> < ...
- 7.FactoryBean 和BeanFactory去区别
FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...
- 浅谈公平组合游戏IGC
浅谈公平组合游戏IGC IGC简介 一个游戏满足以下条件时被叫做IGC游戏 (前面三个字是自己YY的,不必在意) 竞争性:两名玩家交替行动. 公平性:游戏进程的任意时刻,可以执行的操作和操作者本人无关 ...
- IOS - 执行时 (多态)
一 多态概述 多态指同一操作作用于不同的对象.能够有不同的解释.产生不同的执行结果.它是面向对象程序设计(OOP)的一个重要特征,动态类型能使程序直到执行时才确定对象的所属类.其详细 ...