bfs廣度優先搜尋

ThestrY發表於2020-11-09

第一道

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;
	}
}

相關文章