Catch That Cow

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
struct node
{int x,step;
}pos,q;
int vis[100006];
queue<node>ans;
void Push(int x,int step)//避免代码冗长
{q.x=x;vis[x]=1;q.step=step+1;ans.push(q);
}
int bfs(int a,int b)
{int x;pos.x=a;pos.step=0;vis[a]=1;ans.push(pos);while(!ans.empty()){pos=ans.front();ans.pop();if(pos.x==b) return pos.step;x=pos.x-1;if(x>=0 && x<100005 && vis[x]==0){Push(x,pos.step);}x=pos.x+1;if(x>=0 && x<100005 && vis[x]==0){Push(x,pos.step);}x=pos.x+pos.x;if(x>=0 && x<100005 && vis[x]==0){Push(x,pos.step);}}return -1;
}
int main()
{int a,b;cin>>a>>b;cout<<bfs(a,b)<<endl;;return 0;
}

Knight Moves 

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char a[5],b[5];
int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
struct node
{int x,y,step;
}pos,ans;
void bfs(int x1,int y1,int x2,int y2)
{queue<node>q;int vis[11][11];int xx,yy;pos.x=x1;pos.y=y1;pos.step=0;memset(vis,0,sizeof(vis));vis[pos.x][pos.y]=1;q.push(pos);while(!q.empty()){pos=q.front();q.pop();if(pos.x==x2 && pos.y==y2){printf("To get from %s to %s takes %d knight moves.\n",a,b,pos.step);return ;}for(int i=0;i<8;i++){xx=pos.x+dir[i][0];yy=pos.y+dir[i][1];if((xx>0 && xx<9) && (yy>0 && yy<9) && vis[xx][yy]==0){ans.x=xx;ans.y=yy;ans.step=pos.step+1;q.push(ans);}}}
}
int main()
{while(scanf("%s%s",a,b)!=EOF){int x1,y1,x2,y2;x1=a[0]-'a'+1;y1=a[1]-'0';x2=b[0]-'a'+1;y2=b[1]-'0';bfs(x1,y1,x2,y2);}return 0;
}

Ice Cave       

You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.

The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.

Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).

You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?

Input

The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.

Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).

The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.

The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.

Output

If you can reach the destination, print 'YES', otherwise print 'NO'.

Example

Input
4 6X...XX...XX..X..X.......1 62 2

Output
YES

Input
5 4.X.....XX.X......XX.5 31 1

Output
NO

Input
4 7..X.XX..XX..X.X...X..X......2 21 6

Output
YES

题目大意,从X(缝隙中出来,最后必须回到裂缝中)但中途不能掉入裂缝中,所以只能走点上,且每个点走一次过后会开裂,因此不能走第二次,第二次一旦走上就会掉下去。

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
char a[502][502];
int vis[502][502];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node
{int x,y;
}pos,q;
queue<node>ans;
int bfs(int x1,int y1,int x2,int y2,int n,int m)
{int xx,yy;memset(vis,0,sizeof(vis));pos.x=x1;pos.y=y1;vis[x1][y1]=1;ans.push(pos);while(!ans.empty()){pos=ans.front();ans.pop();for(int i=0;i<4;i++){xx=dir[i][0]+pos.x;yy=dir[i][1]+pos.y;if((xx>=1 && xx<=n) && (yy>=1 && yy<=m)){if(xx==x2 && yy==y2 && a[xx][yy]=='X')return 1;if(a[xx][yy]=='.' && vis[xx][yy]==0){q.x=xx;q.y=yy;vis[xx][yy]=1;ans.push(q);a[xx][yy]='X';}}}}return 0;
}
int main()
{int n,m,x1,y1,x2,y2;cin>>n>>m;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>a[i][j];}}cin>>x1>>y1>>x2>>y2;int c=bfs(x1,y1,x2,y2,n,m);if(c==1) cout<<"YES"<<endl;else cout<<"NO"<<endl;return 0;
}

Oil Deposits 

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input  The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output  For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 

Sample Output

0
1
2
2
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int dir[8][2]={{0,1},{0,-1},{1,-1},{1,1},{1,0},{-1,0},{-1,-1},{-1,1}};
struct node{int x,y;
}pos,q;
queue<node>ans;
int vis[110][110];
char a[110][110];
void bfs(int i,int j,int n,int m)
{int xx,yy;pos.x=i;pos.y=j;memset(vis,0,sizeof(vis));vis[i][j]=1;ans.push(pos);while(!ans.empty()){pos=ans.front();ans.pop();a[pos.x][pos.y]='*';for(int k=0;k<8;k++){xx=pos.x+dir[k][0];yy=pos.y+dir[k][1];if((xx>=0 && xx<n) && (yy>=0 && yy<m) && a[xx][yy]=='@' && vis[xx][yy]==0){q.x=xx;q.y=yy;vis[xx][yy]=1;ans.push(q);}}}while(!ans.empty()){ans.pop();}
}
int main()
{int n,m;while(cin>>n>>m && (n && m)){int sum=0;for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(a[i][j]=='@'){sum++;bfs(i,j,n,m);}}}cout<<sum<<endl;}return 0;
}

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/6682631.html

BFS(广度优先搜索)相关推荐

  1. LeetCode-笔记-199. 二叉树的右视图——BFS广度优先搜索

    LeetCode-笔记-199. 二叉树的右视图 199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,n ...

  2. PTA:7-102 喊山 (30分)---解析(bfs广度优先搜索,vector)

    7-102 喊山 (30分) 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出"喂-喂喂-喂喂喂--"的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的& ...

  3. 利用BFS广度优先搜索还原二阶魔方

    利用BFS广度优先搜索还原二阶魔方 采用BFS深度优先搜索算法进行了对于魔方求解问题的建模,并且利用C++代码进行了算法实现,能够实现输入魔方状态,自动输出解法的效果. BFS是图论中一种基本的搜索算 ...

  4. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

  5. [ACM_NYOJ_21]三个水杯(BFS广度优先搜索)

    三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只 ...

  6. 哈理工OJ 1490 咒语(BFS广度优先搜索)

    咒语 Time Limit: 1000 MS Memory Limit: 65535 K Total Submit: 162(37 users) Total Accepted: 53(35 users ...

  7. 农夫过河游戏的几种处理方法(DFS深度优先搜索,BFS广度优先搜索)

    农夫过河游戏规则:在左岸有农夫.狼.羊.菜,农夫需要想办法将狼.羊.菜最终都带到右岸,条件就是农夫不在的时候,狼会吃羊,羊会吃菜,而且每次只能带一样,或者不带. 这里会用到堆栈.队列.列表这样的数据结 ...

  8. 【BFS 广度优先搜索】详解感染橘子最短时间问题

    一.题目描述 在给定的 m x n 网格 grid 中,每个单元格可以有以下三个值之一: 值 0 代表空单元格: 值 1 代表新鲜橘子: 值 2 代表腐烂的橘子. 每分钟,腐烂的橘子 周围 4 个方向 ...

  9. 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

    背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...

  10. HDU 2102 题解(BFS 广度优先搜索 练习题)

    原题链接,但是HDU现在校外提交需要审核 欢迎来 SCPC OJ提交 知识点 : BFS(广搜/宽搜) 原题: 描述: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生 ...

最新文章

  1. -bash: /bin/rm: Argument list too long的解决办法
  2. VS2017 按ctrl+f5执行程序窗口依然一闪而过的问题(图文)
  3. Android-源码解析HandlerLooper
  4. matlab损失函数出现nan,[译]在训练过程中loss出现NaN的原因以及可以采取的方法。...
  5. java程序的调试过程
  6. 服务器和前台采用JSON通讯
  7. php 类名访问静态属性,请问可以用类名直接调用静态属性吗?
  8. 第四篇:稳定性之提升团队潜意识【及时止损、监控报警】
  9. java statemachine_StateMachine 状态机机制深入解析
  10. jmeter简单使用
  11. 如何制作一款HTML5 RPG游戏引擎——第二篇,烟雨+飞雪效果
  12. Vissim4.3之API/SDK编程;Vissim编程;
  13. 普中28335开发攻略_DSP28335汇编教程
  14. 用python自动制作ppt第二讲——插入文字的两种方法和追加文字
  15. SAP CO TCode
  16. 计算机基础教学质量分析报告,张丹菲信息技术质量分析报告1
  17. 云帆文档管理系统版本更新说明:v4.6.0
  18. C++ abort() has been called错误
  19. Linux下批处理文件编写
  20. web期末作业设计网页 HTML+CSS+JavaScript仿王者荣耀游戏新闻咨询(网页设计期末课程设计)...

热门文章

  1. mysql中字典值怎么添加_插入Python字典中的值,包括MySQL的键
  2. 假设mysql数据表t1有字段_使用ROMA Connect集成数据
  3. vs使用了未初始化的局部变量怎么解决_C程序为什么要初始化?
  4. mysql的连接名是哪个文件_mysql连接名是什么
  5. qregexp括号匹配_转:Qt的正则表达式和QRegExp
  6. 算法转换c语言程序,(转)C语言实现卡尔曼滤波算法程序
  7. NewCode----求数列的和
  8. Ubuntu如何安装setuptools
  9. java 检查目录是否存在_如何检查Java目录是否存在?
  10. 线性插值算法实现图像_C程序实现插值搜索算法