一维坐标的移动(bfs)

3 篇文章 0 订阅
文章介绍了如何使用深度优先搜索(DFS)和广度优先搜索(BFS)算法解决小S在长度为n的坐标轴上从A点移动到B点的最少步数问题,两种方法都实现了遍历所有可能路径并找到最优解的过程。
摘要由CSDN通过智能技术生成

在一个长度为n的坐标轴上,小S想从A点移动B点。

他的移动规则如下:

向前一步,坐标增加1。

向后一步,坐标减少1。

跳跃一步,使得坐标乘2。

小S不能移动到坐标小于0或大于n的位置。

小S想知道从A点移动到B点的最少步数是多少,你能帮他计算出来么?

输入格式

第一行输入三个整数n,A,B,分别代表坐标轴长度,起始点坐标,终点坐标。(0<=A,B<=n<=50000)

输出格式

输出一个整数占一行,代表小S要走的最少步数。

样例输入

10 2 7

样例输出

3

dfs深度优先(时间复杂度会比较高,这里仅供理解题目)

#include<iostream>
using namespace std;
const int N=50005;
bool st[N];
int n,A,B;
int ans;
void dfs(int x,int step){
	if(x==B){//结束条件 
		ans=step;
		return;
	}
	int y;

	//向前走,坐标+1
	y=x+1;
	if(!st[y]&&y>=1&&y<=n){
		st[y]=true;
		dfs(y,step+1);
		st[y]=false;
	}
	
	//向后走,坐标-1
	y=x-1;
	if(!st[y]&&y>=1&&y<=n){
		st[y]=true;
		dfs(y,step+1);
		st[y]=false;
	}
	//跳跃,坐标*2 
	y=x*2;
	if(!st[y]&&y>=1&&y<=n){
		st[y]=true;
		dfs(y,step+1);
		st[y]=false;
	}
}
int main(){
	cin>>n>>A>>B;
	dfs(A,0);
	cout<<ans<<endl;
	return 0;
}

bfs广度优先

 

#include<iostream>
#include<queue>
using namespace std;
const int N=50005;
bool st[N];
typedef struct point{
	int x;
	int step;
}point;
queue<point> q;
int n,A,B;
 
void bfs(){
	while(q.size()){
		point p=q.front();
		if(p.x==B){
			break;
		}
		point next;
		int a;
		//向前走,坐标+1 
		a=p.x+1;
		if(!st[a]&&a>=1&&a<=n){
			next.x=a;
			next.step=p.step+1;
			q.push(next);//入队
			st[a]=true; 
		}
		//向后走,坐标-1 
		a=p.x-1;
		if(!st[a]&&a>=1&&a<=n){
			next.x=a;
			next.step=p.step+1;
			q.push(next);//入队
			st[a]=true; 
		}
		//跳跃,坐标*2 
		a=p.x*2;
		if(!st[a]&&a>=1&&a<=n){
			next.x=a;
			next.step=p.step+1;
			q.push(next);//入队
			st[a]=true; 
		}
		q.pop(); 
	}
}
int main(){
	cin>>n>>A>>B;
	point start;
	start.x=A;
	start.step=0;
	q.push(start);
	bfs();
	cout<<q.front().step<<endl;
	return 0;
} 

 

以下是Python实现代码: ```python from collections import deque # 定义坐标类 class Coordinate: def __init__(self, x, y): self.x = x self.y = y # 定义BFS函数 def bfs(start, end, maze): queue = deque() # 记录起点到每个点的最短距离 distance = [[-1] * len(maze[0]) for _ in range(len(maze))] distance[start.x][start.y] = 0 queue.append(start) while queue: cur = queue.popleft() # 找到终点,返回最短路径长度 if cur.x == end.x and cur.y == end.y: return distance[cur.x][cur.y] # 四个方向进行搜索 for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: new_x, new_y = cur.x + dx, cur.y + dy # 如果新坐标越界或者是墙壁或者已经被搜索过,直接跳过 if not (0 <= new_x < len(maze) and 0 <= new_y < len(maze[0]) and maze[new_x][new_y] == '.' and distance[new_x][new_y] == -1): continue # 更新最短距离并加入队列 distance[new_x][new_y] = distance[cur.x][cur.y] + 1 queue.append(Coordinate(new_x, new_y)) # 没有找到终点,返回-1 return -1 # 读入输入 start_x, start_y, end_x, end_y = map(int, input().split()) maze = [] for i in range(10): maze.append(input()) # 计算乌龟到终点的距离 turtle_distance = abs(end_x - start_x) + abs(end_y - start_y) # 计算兔子到终点的最短路径长度 start = Coordinate(start_x, start_y) end = Coordinate(end_x, end_y) rabbit_distance = bfs(start, end, maze) # 计算兔子比乌龟多走的距离 extra_distance = rabbit_distance - turtle_distance # 输出结果 print('%.2f' % extra_distance) ``` 注意,以上代码中用到了坐标类`Coordinate`和队列`deque`。`Coordinate`类用于表示二维坐标,`deque`则是Python标准库中的双端队列,可以高效地实现队列的操作。在BFS中,我们需要使用队列来保存待搜索的坐标
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值