BFS(廣度優先搜索)

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 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[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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393264.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393264.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393264.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

leetcode111. 二叉樹的最小深度(隊列)

給定一個二叉樹&#xff0c;找出其最小深度。最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。說明: 葉子節點是指沒有子節點的節點。示例:給定二叉樹 [3,9,20,null,null,15,7],3/ \9 20/ \15 7 返回它的最小深度 2.代碼 /*** Definition for a binary tree no…

企業網站6個常見的優化漏洞

導讀&#xff1a;企業做營銷網站目的&#xff0c;就是希望通過網絡營銷&#xff0c;挖掘目標客戶。目標客戶怎么來&#xff0c;那就需要通過網站優化&#xff0c;把網站關鍵詞優化排名到首頁&#xff0c;這樣才能更多的機會被潛在客戶點擊。很多企業網站上線之前&#xff0c;沒…

aspx 微型_最初的十億分鐘:正在向世界授課的微型非營利組織背后的數字

aspx 微型by Quincy Larson昆西拉爾森(Quincy Larson) 最初的十億分鐘&#xff1a;正在向世界授課的微型非營利組織背后的數字 (The First Billion Minutes: The Numbers Behind the Tiny Nonprofit That’s Teaching the World to Code) People have now spent more than 1 b…

[RN] React Native 自定義導航欄隨滾動漸變

React Native 自定義導航欄隨滾動漸變 實現效果預覽&#xff1a; 代碼實現&#xff1a; 1、定義導航欄 NavPage.js import React, {Component} from react; import {View, Text, Image, StyleSheet, TouchableOpacity, Platform, Dimensions} from react-native;/*** 自定義導航…

【CSS 技能提升】 :before和:after的使用

前幾天的晚上較全面的去看了下css的一些文檔和資料&#xff0c;大部分的樣式運用都沒什么大問題了&#xff0c;只是有些許較陌生&#xff0c;但是也知道他們的存在和實現的是什么樣式。今天主要想在這篇學習筆記中寫的也不多&#xff0c;主要是針對:before和:after寫一些內容&a…

c語言模擬java面向對象_純c語言實現面向對象分析與示例分享

#include #include //接口#ifndef Interface#define Interface struct#endif//類#ifndef Class#define Class struct#endif//抽象形狀類Class Shape;typedef Class Shape shape;//抽象形狀類的方法聲明shape* Shape(int edges);int shape_getEdges(shape *);int shape_getArea(…

leetcode152. 乘積最大子數組

給你一個整數數組 nums &#xff0c;請你找出數組中乘積最大的連續子數組&#xff08;該子數組中至少包含一個數字&#xff09;&#xff0c;并返回該子數組所對應的乘積。 示例 1: 輸入: [2,3,-2,4] 輸出: 6 解釋: 子數組 [2,3] 有最大乘積 6。 代碼 class Solution {publi…

成功試驗基于C#/.NET的Android開發

今天最開心事情莫過于摸索驗證了一個事情&#xff0c;C#也能進行Android和IOS開發&#xff0c;白天安裝了開發環境&#xff0c;晚上進行測試&#xff0c;直到此時此刻&#xff0c;已經成功的導出一款基于C#/.NET的安卓APK&#xff0c;并且能夠成功的導入到安卓手機運行&#xf…

使用機器學習預測天氣_如何使用機器學習根據文章標題預測喜歡和分享

使用機器學習預測天氣by Flavio H. FreitasFlavio H.Freitas著 如何使用機器學習根據文章標題預測喜歡和分享 (How to predict likes and shares based on your article’s title using Machine Learning) Choosing a good title for an article is an important step in the …

深入理解了MySQL,你才能說熟悉數據庫

先拋出幾個問題 1.為什么不建議使用訂單號作為主鍵?2.為什么要在需要排序的字段上加索引?3.for update 的記錄不存在會導致鎖住全表?4.redolog 和 binlog 有什么區別?5.MySQL 如何回滾一條 sql ?6.char(50) 和 varchar(50) 效果是一樣的么?索引知識回顧 對于 MySQL 數據庫…

ibatis mysql 自增_mybatis自增主鍵

簡單介紹&#xff1a;在使用mybats插入數據是&#xff0c;有很多需要和id關聯的其他數據&#xff0c;所以在插入一條信息時獲取其主鍵信息是很常見的操作。一 mysql數據庫的主鍵自增(int類型的主鍵)1 創建一個表&#xff0c;設置表的id(此id必須是int類型)&#xff0c;設置為au…

DataGridView控件用法二:常用屬性

通常會設置的DataGridView的屬性如下&#xff1a; AllowUserToAddRows - False指示是否向用戶顯示用于添加行的選項&#xff0c;列標題下面的一行空行將消失。一般讓其消失。AllowUserToDeleteRows - False指示是否允許用戶從DataGridView刪除行。一般不允許。AllowUserToOrder…

leetcode面試題 16.21. 交換和(二分查找)

給定兩個整數數組&#xff0c;請交換一對數值&#xff08;每個數組中取一個數值&#xff09;&#xff0c;使得兩個數組所有元素的和相等。 返回一個數組&#xff0c;第一個元素是第一個數組中要交換的元素&#xff0c;第二個元素是第二個數組中要交換的元素。若有多個答案&…

談談IP和MAC捆綁的破解之道

來源:[url]http://l-y.vicp.net[/url]我們學校最近將MAC和IP進行了捆綁&#xff0c;又在服務器&#xff08;2K&#xff09;上進行了上網時間的限制&#xff0c;真是煩死人了&#xff0c;我想我可是一個從不受限制的人啊&#xff0c;怎么可以就這樣束手就擒呢&#xff01;古話說…

如何在JavaScript中區分深層副本和淺層副本

by Lukas Gisder-Dub盧卡斯吉斯杜比(LukasGisder-Dub) 如何在JavaScript中區分深層副本和淺層副本 (How to differentiate between deep and shallow copies in JavaScript) New is always better!新總是更好&#xff01; You have most certainly dealt with copies in Java…

網站QQ全屏PHP代碼,QQ技術導航升級版 超級導航美化版帶后臺版 PHP源碼

QQ技術導航升級版 超級導航美化版帶后臺版改進F2樣式&#xff0c;主針對QQ教程網、卡盟、博客、提供更好收錄的位置。改進QQ技術導航背景&#xff0c;增加整體美觀效果。去掉死鏈頁面&#xff0c;站長操作使用更加有擴大空間。優化后臺登陸界面&#xff0c;去掉織夢后臺攜帶的廣…

MySQL基礎操作(一)

MySQL操作 一、創建數據庫 # utf-8 CREATE DATABASE 數據庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;# gbk CREATE DATABASE 數據庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; 二、用戶管理 創建用戶create user 用戶名IP地址 identified by 密碼; 刪…

集合框架05

一、HashSet集合 1 public class Demo01 {2 /*3 * Set接口&#xff0c;特點不重復元素&#xff0c;沒索引4 * Set接口的實現類&#xff0c;HashSet(哈希表)5 * 特點&#xff1a;無序集合&#xff0c;存儲和取出的順序不同&#xff0c;沒有索引&#xff0c;不…

leetcode1233. 刪除子文件夾

你是一位系統管理員&#xff0c;手里有一份文件夾列表 folder&#xff0c;你的任務是要刪除該列表中的所有 子文件夾&#xff0c;并以 任意順序 返回剩下的文件夾。 我們這樣定義「子文件夾」&#xff1a; 如果文件夾 folder[i] 位于另一個文件夾 folder[j] 下&#xff0c;那…

HIVE-分桶表的詳解和創建實例

我們學習一下分桶表&#xff0c;其實分區和分桶這兩個概念對于初學者來說是比較難理解的。但對于理解了的人來說&#xff0c;發現又是如此簡單。 我們先建立一個分桶表&#xff0c;并嘗試直接上傳一個數據 create table student4(sno int,sname string,sex string,sage int, sd…