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[];
queue<node>ans;
void Push(int x,int step)//避免代码冗长
{
q.x=x;
vis[x]=;
q.step=step+;
ans.push(q);
}
int bfs(int a,int b)
{
int x;
pos.x=a;pos.step=;
vis[a]=;
ans.push(pos);
while(!ans.empty())
{
pos=ans.front();
ans.pop();
if(pos.x==b) return pos.step;
x=pos.x-;
if(x>= && x< && vis[x]==)
{
Push(x,pos.step);
}
x=pos.x+;
if(x>= && x< && vis[x]==)
{
Push(x,pos.step);
}
x=pos.x+pos.x;
if(x>= && x< && vis[x]==)
{
Push(x,pos.step);
}
}
return -;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<bfs(a,b)<<endl;;
return ;
}

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[],b[];
int dir[][]={{-,},{-,},{,},{,},{,-},{,-},{-,-},{-,-}};
struct node
{
int x,y,step;
}pos,ans;
void bfs(int x1,int y1,int x2,int y2)
{
queue<node>q;
int vis[][];
int xx,yy;
pos.x=x1;pos.y=y1;pos.step=;
memset(vis,,sizeof(vis));
vis[pos.x][pos.y]=;
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=;i<;i++)
{
xx=pos.x+dir[i][];
yy=pos.y+dir[i][];
if((xx> && xx<) && (yy> && yy<) && vis[xx][yy]==)
{
ans.x=xx;
ans.y=yy;
ans.step=pos.step+;
q.push(ans);
}
} }
}
int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
int x1,y1,x2,y2;
x1=a[]-'a'+;y1=a[]-'';
x2=b[]-'a'+;y2=b[]-'';
bfs(x1,y1,x2,y2);
}
return ;
}

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 6
X...XX
...XX.
.X..X.
......
1 6
2 2
Output
YES
Input
5 4
.X..
...X
X.X.
....
.XX.
5 3
1 1
Output
NO
Input
4 7
..X.XX.
.XX..X.
X...X..
X......
2 2
1 6
Output
YES

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

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

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[][]={{,},{,-},{,-},{,},{,},{-,},{-,-},{-,}};
struct node{
int x,y;
}pos,q;
queue<node>ans;
int vis[][];
char a[][];
void bfs(int i,int j,int n,int m)
{
int xx,yy;
pos.x=i;pos.y=j;
memset(vis,,sizeof(vis));
vis[i][j]=;
ans.push(pos);
while(!ans.empty())
{
pos=ans.front();
ans.pop();
a[pos.x][pos.y]='*';
for(int k=;k<;k++)
{
xx=pos.x+dir[k][];
yy=pos.y+dir[k][];
if((xx>= && xx<n) && (yy>= && yy<m) && a[xx][yy]=='@' && vis[xx][yy]==)
{
q.x=xx;
q.y=yy;
vis[xx][yy]=;
ans.push(q);
}
}
}
while(!ans.empty())
{
ans.pop();
}
}
int main()
{
int n,m;
while(cin>>n>>m && (n && m))
{
int sum=;
for(int i=;i<n;i++)
cin>>a[i];
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='@')
{
sum++;
bfs(i,j,n,m);
}
}
}
cout<<sum<<endl;
}
return ;
}

BFS(广度优先搜索)的更多相关文章

  1. BFS广度优先搜索 poj1915

    Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...

  2. 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想

    dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...

  3. 图的遍历BFS广度优先搜索

    图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...

  4. 算法竞赛——BFS广度优先搜索

    BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...

  5. GraphMatrix::BFS广度优先搜索

    查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...

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

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

  7. 关于宽搜BFS广度优先搜索的那点事

    以前一直知道深搜是一个递归栈,广搜是队列,FIFO先进先出LILO后进后出啥的.DFS是以深度作为第一关键词,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口 ...

  8. [MIT6.006] 13. Breadth-First Search (BFS) 广度优先搜索

    一.图 在正式进入广度优先搜索的学习前,先了解下图: 图分为有向图和无向图,由点vertices和边edges构成.图有很多应用,例如:网页爬取,社交网络,网络传播,垃圾回收,模型检查,数学推断检查和 ...

  9. DFS(深度优先搜索)和BFS(广度优先搜索)

    深度优先搜索算法(Depth-First-Search) 深度优先搜索算法(Depth-First-Search),是搜索算法的一种. 它沿着树的深度遍历树的节点,尽可能深的搜索树的分支. 当节点v的 ...

  10. DFS+BFS(广度优先搜索弥补深度优先搜索遍历漏洞求合格条件总数)--09--DFS+BFS--蓝桥杯剪邮票

    题目描述 如下图, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)  比如,下面两张图中,粉红色所示部分就是合格的剪取.  请你计算,一共有多少 ...

随机推荐

  1. [luogu] P4514 上帝造题的七分钟 (树状数组,二维差分)

    P4514 上帝造题的七分钟 题目背景 裸体就意味着身体. 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a ...

  2. 在Windows Server 2008 R2中删除网桥

    How to remove a network bridge in Windows Server 2008 R2 症状: 删除网桥的时候,按理说应该在“网络连接”中选择要被删除的网桥,右键点击,然后选 ...

  3. HDU——T 2444 The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  4. uva live 2326 - Moving Tables

    把房间号映射在一条坐标上,然后排序,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划,最后找从左到右找一次可行的计划, ............ 次数*1 ...

  5. TODOList 多线程交互、RCP、事物控制、数据倾斜、HBase数据同步性

    TODOList 多线程交互.RCP.事物控制.数据倾斜.HBase数据同步性 TODO List thread.join()如何互相之间通知? 线程池何时最后运行完成? MemCache性能要优于R ...

  6. 面向对象(OOP)五大基本原则

    书单 <Object-Oriented Analysis & Design with Application>:Grady Booch, 下载地址:object-oriented- ...

  7. [COI2007] [luogu P1823] Patrik 音乐会的等待 解题报告 (单调栈)

    题目链接:https://www.luogu.org/problemnew/show/P1823 题目: N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人. ...

  8. 系统丢失的DLL文件问题根源解决(纯净官网下载放心)(图文详解)(博主推荐)

    导言 最近,身边的朋友们,问我,他电脑的win10系统里 mfc110.dll 丢失. 其他的系统文件丢失修复,是一样的步骤. 现象 大家也许,都会有这么一个习惯,动不动则就去百度上搜索. 其实啊,这 ...

  9. VS自定义开发向导中的vsdir文件的简单说明

    作者:朱金灿 来源:http://blog.csdn.net/clever101 VS自定义开发向导中有一个vsdir文件.这个文件指定了在VS中项目的标题.默认工程名等内容.下面对vsdir文件做一 ...

  10. 显示gif动画(帧动画的播放)

    在android上显示gif不太方便,虽然有控件可以实现,但是效果不是很好,保险点儿的作法还是使用帧动画来处理.①在XML中定义animation-list:<?xml version=&quo ...