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
題目大意是 輸入兩個數,n和k,你可以對n進行加減1,乘2這三種操作,求使n變成k的最小操作次數
第一道用queue bfs做出來的題
#include <stdio.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
const int Ma=100000+10;
ll n,k;
int vis[Ma];
int s[Ma];
int step[3]={1,-1,2};
void bfs(int x)
{
int dx;
ll m;
queue<ll>cun;
cun.push(x);
s[x]=0;
vis[x]=1;
while(cun.size()!=0)
{
m=cun.front();
cun.pop();
if(m==k)break;
for(int i=0;i<3;i++)
{
if(step[i]==2)
{
dx=m*2;
}
else
{
dx=m+step[i];
}
if(!vis[dx]&&(dx>=0&&dx<=Ma))
{
s[dx]=s[m]+1;
vis[dx]=1;
cun.push(dx);
if(dx==k)break;
}
}
}
}
int main()
{
while(cin>>n>>k)
{
memset(vis,0,sizeof(vis));
bfs(n);
cout<<s[k]<<"\n";
}
}
花了近一個小時完全吃透(我是fw)
第二道
Dungeon Master
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S…
.###.
.##…
###.#
##.##
##…
#.###
####E
1 3 3
S##
#E#
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
題目大意是 輸入m,n,k,然後輸入由# . S E構建的迷宮,.是可以行走的,#是牆壁,S是起點,E是終點,一個人從起點出發,每次能移動一格,或者上升/下降一層(到另一層的對應位置),求他到終點的最短時間,如果到不了終點,輸出Trapped
因為是連著做的,這道題做的就比較快而且還算是得心應手,方法幾乎一樣
#include <stdio.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
int n,m,k;
int fx,fy,fz;
int ex,ey,ez;
char a[33][33][33];
int vis[33][33][33];
int s[33][33][33];
int step[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
void bfs(int x,int y,int z)
{
int dx,dy,dz;
int mx,my,mz;
queue<ll>cun;
cun.push(x);
cun.push(y);
cun.push(z);
s[x][y][z]=0;
vis[x][y][z]=1;
while(!cun.empty())
{
mx=cun.front();
cun.pop();
my=cun.front();
cun.pop();
mz=cun.front();
cun.pop();
if(mx==ex&&my==ey&&mz==ez)break;
for(int i=0;i<6;i++)
{
dx=mx+step[i][0];
dy=my+step[i][1];
dz=mz+step[i][2];
if(!vis[dx][dy][dz]&&(dx>0&&dx<=k)&&(dy>0&&dy<=n)&&(dz>0&&dz<=m)&&a[dx][dy][dz]!='#')
{
s[dx][dy][dz]=s[mx][my][mz]+1;
vis[dx][dy][dz]=1;
cun.push(dx);
cun.push(dy);
cun.push(dz);
if(dx==ex&&dy==ey&&dz==ez)break;
}
}
}
}
int main()
{
while(cin>>k>>n>>m)
{
if(m==0&&n==0&&k==0)
break;
memset(a,0,sizeof(a));
memset (vis,0,sizeof(vis));
getchar();
for(int o=1;o<=k;o++)
{
for(int i=1;i<=n;i++)
{
scanf("%s",a[o][i]+1);
}
}
// for(int o=1;o<=k;o++)
// {
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=m;j++)
// {
// printf("%c",a[o][i][j]);
// }
// printf("\n");
// }
// printf("\n");
// }
for(int o=1;o<=k;o++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[o][i][j]=='S')
{
fx=o;
fy=i;
fz=j;
}
if(a[o][i][j]=='E')
{
ex=o;
ey=i;
ez=j;
}
}
}
}
bfs(fx,fy,fz);
if(s[ex][ey][ez])
cout<<"Escaped in "<<s[ex][ey][ez]<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
}
相關文章
- 【演算法】廣度/寬度優先搜尋(BFS)演算法
- 基本演算法——深度優先搜尋(DFS)和廣度優先搜尋(BFS)演算法
- 演算法競賽——BFS廣度優先搜尋演算法
- 廣度優先搜尋(BFS)思路及演算法分析演算法
- ybtoj:廣度優先搜尋
- 圖的遍歷:深度優先搜尋與廣度優先搜尋
- c++ 廣度優先搜尋(寬搜)C++
- 圖的廣度優先搜尋和深度優先搜尋Python實現Python
- python 二叉樹深度優先搜尋和廣度優先搜尋Python二叉樹
- js版本的(廣、深)度優先搜尋JS
- 0演算法基礎學演算法 搜尋篇第二講 BFS廣度優先搜尋的思想演算法
- 【程式碼隨想錄】廣度優先搜尋
- 演算法筆記(廣度優先搜尋)演算法筆記
- 深度和廣度優先搜尋演算法演算法
- 「Golang成長之路」迷宮的廣度優先搜尋Golang
- 啟發式搜尋的方式(深度優先,廣度優先)和 搜尋方法(Dijkstra‘s演算法,代價一致搜尋,貪心搜尋 ,A星搜尋)演算法
- golang學習筆記——迷宮的廣度優先搜尋Golang筆記
- 深度DFS 和 廣度BFS搜尋演算法學習演算法
- 演算法(三):圖解廣度優先搜尋演算法演算法圖解
- 深度優先搜尋
- POJ1915,雙向寬度優先搜尋
- ybtoj:深度優先搜尋
- DFS(深度優先搜尋)
- 十、深度優先 && 廣度優先
- 深度優先與廣度優先
- 單詞接龍---快速建圖----雙向BFS(廣度優先遍歷)
- 廣度優先搜尋相關面試演算法總結(非圖論方面)面試演算法圖論
- Android程式設計師面試會遇到的演算法(part 2 廣度優先搜尋)Android程式設計師面試演算法
- python實現圖(基於圖的不同儲存方式)的深度優先(DFS)和廣度(BFS)優先遍歷Python
- leetcode 刷題之深度優先搜尋LeetCode
- 【演算法】深度優先搜尋(DFS)演算法
- (BFS廣度優先演算法) 油田問題演算法
- 廣度優先與深度優先類似嗎? - Mario
- 廣度優先遍歷圖解圖解
- Python 圖_系列之基於鄰接炬陣實現廣度、深度優先路徑搜尋演算法Python演算法
- 深度優先搜尋演算法(DFS)講解演算法
- 深度優先搜尋演算法-dfs講解演算法
- 【知識點】深度優先搜尋 Depth First Search