BFS(宽度优先搜索) -例题
原题地址
https://vjudge.net/contest/313171 密码:algorithm
A - Rescue
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
解题思路:
当r遇到护卫时将杀死他并浪费一分钟时间,所以需要用优先队列。
代码:
#include <bits/stdc++.h>
using namespace std;
char arr[+][+];//存储地牢地图
int arr1[+][+];//标记走过的路
int dx[]={,,,-};
int dy[]={,-,,}; //代表方向
struct aa{//将数据放入一个结构优先队列中,并按w的大小排序
int x,y,w;
friend bool operator<(const aa &x,const aa &y){
return x.w>y.w;//按照w的升序排列
}
}aaa;
int bfs(int n,int m,int x1,int y1){
memset(arr1,,sizeof(arr1));//多组输入初始化数组
priority_queue<aa>que;//构建队列
aaa={x1,y1,};
que.push({x1,y1,});
int xx,yy,xxx,yyy,w,s;
while(que.size()){
xx=que.top().x;
yy=que.top().y;
w=que.top().w;//取出队列的top;
arr1[yy][xx]=;//将初始位置标记
que.pop();//删除top,要现取现删
for(int i=;i<;i++){
xxx=xx+dx[i];
yyy=yy+dy[i];//下一个位置的坐标
if(xxx>=&&xxx<m&&yyy>=&&yyy<n&&arr[yyy][xxx]!='#'&&arr1[yyy][xxx]==){//进行判断
if(arr[yyy][xxx]=='.'){
que.push({xxx,yyy,w+});//如果是路w+1,并存入队列
arr1[yyy][xxx]=; //标记
}
else if(arr[yyy][xxx]=='x'){
que.push({xxx,yyy,w+});//如果是护卫w+2
arr1[yyy][xxx]=;//标记
}
else if(arr[yyy][xxx]=='a'){
return w+;//等于a说明找到公主,此时所用时间最短,输出
}
}
}
}
return ;//没找到输出0;
}
int main(){
int n,m;
while(cin>>n>>m){
int x,y,ans;
for(int i=;i<n;i++){//构建地牢地图
cin>>arr[i];
for(int j=;j<m;j++){
if(arr[i][j]=='r'){
x=j;
y=i;
}
}
}
ans=bfs(n,m,x,y);
if(ans){//判断输出
cout<<ans<<endl;
}
else{
puts("Poor ANGEL has to stay in the prison all his life.");
}
}
return ; }
B - Red and Black
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
解题思路:
与上一题大致相同,且可以不用优先队列。
C - Battle City
解题思路与第一题相同
D - Catch That Cow
农夫知道一头牛的位置,想要抓住它。农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) 。农夫有两种移动方式: 1、从 X移动到 X-1或X+1 ,每次移动花费一分钟 2、从 X移动到 2*X ,每次移动花费一分钟 假设牛没有意识到农夫的行动,站在原地不。最少要花多少时间才能抓住牛?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
E - Dungeon Master
Description - 题目描述
[NWUACM]
你被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成
你每次向上下前后左右移动一个单位需要一分钟
你不能对角线移动并且四周封闭
是否存在逃出生天的可能性?如果存在,则需要多少时间?
Input - 输入
输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度。
R和C分别表示每层空间的行与列的大小。
随后L层地牢,每层R行,每行C个字符。
每个字符表示空间的一个单元。'#'表示不可通过单元,'.'表示空白单元。你的起始位置在'S',出口为'E'。
每层空间后都有一个空行。L,R和C均为0时输入结束。
Output - 输出
每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下 :Trapped!
解题思路:
三维与二维相似,将方向改为 int dx[6]={0,0,1,-1,0,0}; int dy[6]={1,-1,0,0,0,0}; int dz[6]={0,0,0,0,1,-1};即可.
F - Robot Motion

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
Output
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s) 解题思路:
使用递归遍历走的路,看是否是循环或能否走出去。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<algorithm>
#include <vector>
using namespace std;
int n,m;
char arr[][];
int arr1[][];
int flag=,l,r,ans;
void bfs(int x,int y,int num){
if(x<||y<||x>=m||y>=n){
ans=num;
return ;
}
if(arr1[y][x]){
flag=;
l=arr1[y][x];
r=num-arr1[y][x];
return ;
}
arr1[y][x]=num;
if(arr[y][x] == 'N')
bfs(x,y-,num+);
else if(arr[y][x] == 'S')
bfs(x,y+,num+);
else if(arr[y][x] == 'E')
bfs(x+,y,num+);
else if(arr[y][x] == 'W')
bfs(x-,y,num+);
}
int main(){
int x,y=;
while(cin>>n>>m>>x){
if(n==&m==&x==){
break;
}
flag=;
y=;
x=x-;
memset(arr1,,sizeof(arr1));
for(int i=;i<n;i++){
cin>>arr[i];
}
bfs(x,y,);
if(flag){
printf("%d step(s) before a loop of %d step(s)\n",l-,r);
}
else {
printf("%d step(s) to exit\n",ans-);
}
}
return ;
}
G - Number Transformation
In this problem, you are given an integer number s. You can transform any integer number A to another integer number B by adding x to A. This x is an integer number which is a prime factor of A (please note that 1 and A are not being considered as a factor of A). Now, your task is to find the minimum number of transformations required to transform s to another integer number t.
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case contains two integers: s (1 ≤ s ≤ 100) and t (1 ≤ t ≤ 1000).
Output
For each case, print the case number and the minimum number of transformations needed. If it's impossible, then print -1.
Sample Input
2
6 12
6 13
Sample Output
Case 1: 2
Case 2: -1
解题思路:
a的prime factor(素因子)是指能整除a且不是素数的数字i,i!=1&&i!=a.本题的意思是循环一个数,这个数加上他的素因子,直到与给出的另一个数相等位置(数在改变,所以素因子也在改变)。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<algorithm>
#include <vector>
using namespace std;
typedef pair<int,int> P;
vector<int>arr;
int arr1[];
int is_primer(int a){
if(a==)return ;
for(int i=;i<a;i++){
if(a%i==){
return ;
}
}
return ;
}
int bfs(int a,int b){
memset(arr1,,sizeof(arr1));
queue<P>que;
int d,ss;
que.push({a,});
arr1[a]=;
while(que.size()){
arr.clear();
int x,y;
x=que.front().first;
y=que.front().second;
for(int j=;j<x;j++){
if(x%j==&&is_primer(j)){
arr.push_back(j);
}
}
que.pop();
for(int i=;i<arr.size();i++){
d=x+arr[i];
if(d>&&d<b+&&!arr1[d]){
if(d==b){
return y+;
}
else{
que.push({d,y+});
arr1[d]=;
}
}
}
}
return ;
}
int main(){
int n,s,k=,t;
cin>>n;
for(int i=;i<=n;i++){
k=;
cin>>s>>t;
if(s==t){
printf("Case %d: 0\n",i);
}
else{
int ans=bfs(s,t);
printf("Case %d: ",i);
if(ans){
cout<<ans<<endl;
}
else {
cout<<-<<endl;
}
}
}
return ;
}
H - Knight Moves
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
解题思路:
题意大致是从一个点到另一个点以走'日'的形式需要的最短时间,一共有八个方向。
代码略。
BFS(宽度优先搜索) -例题的更多相关文章
- 【BFS宽度优先搜索】
一.求所有顶点到s顶点的最小步数 //BFS宽度优先搜索 #include<iostream> using namespace std; #include<queue> # ...
- 搜索与图论②--宽度优先搜索(BFS)
宽度优先搜索 例题一(献给阿尔吉侬的花束) 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫. 今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔 ...
- BFS算法的优化 双向宽度优先搜索
双向宽度优先搜索 (Bidirectional BFS) 算法适用于如下的场景: 无向图 所有边的长度都为 1 或者长度都一样 同时给出了起点和终点 以上 3 个条件都满足的时候,可以使用双向宽度优先 ...
- 【算法入门】广度/宽度优先搜索(BFS)
广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
- 算法基础⑦搜索与图论--BFS(宽度优先搜索)
宽度优先搜索(BFS) #include<cstdio> #include<cstring> #include<iostream> #include<algo ...
- 挑战程序2.1.5 穷竭搜索>>宽度优先搜索
先对比一下DFS和BFS 深度优先搜索DFS 宽度优先搜索BFS 明显可以看出搜索顺序不同. DFS是搜索单条路径到 ...
- [宽度优先搜索] FZU-2150 Fire Game
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...
- 宽度优先搜索--------迷宫的最短路径问题(dfs)
宽度优先搜索运用了队列(queue)在unility头文件中 源代码 #include<iostream>#include<cstdio>#include<queue&g ...
随机推荐
- Electron构建一个文件浏览器应用(二)
在前一篇文章我们已经学习到了使用Electron来构建我们的文件浏览器了基础东西了,我们之前已经完成了界面功能和显示文件或文件夹的功能了,想看之前文章,请点击这个链接 .现在我们需要在之前的基础上来 ...
- 用.NET Core实现一个类似于饿了吗的简易拆红包功能
需求说明 以前很讨厌点外卖的我,最近中午经常点外卖,因为确实很方便,提前点好餐,算准时间,就可以在下班的时候吃上饭,然后省下的那些时间就可以在中午的时候多休息一下了. 点餐结束后,会有一个好友分享 ...
- 透视BlueStore存储结构:如何根据文件名从裸盘提取文件内容
在FileStore下,用户文件经过切分对象块后最终存放在了单机文件系统(xfs .ext4等)中,我们可以较容易地找到这些对象块对应的文件,然后提取这些对象块文件后组装成用户文件.然而,BlueSt ...
- ZooKeeper入门(四) Zookeeper监视(Watches)
1 简介 Zookeeper 所有的读操作——getData(), getChildren(), 和 exists() 都 可以设置监视(watch),并且这些watch都由写操作来触发:create ...
- 不使用 ASR 将虚机还原到另一个数据中心
背景 在 Azure 上可能会遇到一个场景是将一台虚机搬到另一台数据中心,在不借助 ASR 的情况下我们该如何做? 因为 ASR 在云上更多的场景是用于灾备到异地.对于虚机的相关信息主要的是磁盘和网络 ...
- Java线程池源码及原理
目录 1 说明 1.1类继承图 2 线程池的状态 3 源码分析 3.1完整的线程池构造方法 3.2 ctl 3.3 任务的执行 3.3.1 execute(Runnable command) 3.3. ...
- Noip 2016 愤怒的小鸟 题解
[NOIP2016]愤怒的小鸟 时间限制:1 s 内存限制:256 MB [题目描述] Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0, ...
- 【转载】DOMContentLoaded与load的区别
DOMContentLoaded与load的区别 (1)在chrome浏览器的开发过程中,我们会看到network面板中有这两个数值,分别对应网 络请求上的标志线,这两个时间数值分别代表什么? ( ...
- dockerfile 制作镜像
# Set the base image to UbuntuFROM ubuntu # File Author chenghanMAINTAINER chenghan ################ ...
- [原创]Greenplum数据库集群实践
GreenPlum实践 ============================================== 目录: 一.安装环境准备 二.GP数据库安装 三.集群添加standby节点 四. ...