抓住那头牛 —一维BFS—坐标移动

  •  

  • 输入格式

    共一行,包含两个整数N和K。

    输出格式

    输出一个整数,表示抓到牛所花费的最少时间。

    数据范围

    0≤N,K≤1e5

    输入样例:

    5 17
    

    输出样例:

    4
    

分析: 注意适当剪枝!不然会超内存

法一:使用pair——40 ms

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e7 + 10;
typedef pair<int, int>pii;
int st, ed;
bool vis[N]; 
void bfs() {
	queue<pii>q;
	q.push(make_pair(st, 0));//起始位置和step;
	vis[st] = true;
	while (q.size()) {
		int now = q.front().first;
		int step = q.front().second;
		q.pop();
		if (now == ed) {
			cout << step << endl;
			return;
		}
		if (now + 1 <= ed && !vis[now+1]) {
			vis[now+1] = true;
			q.push({ now+1,step + 1 });
		}
		if (now-1>=0&& !vis[now-1]) {
			vis[now-1] = true;
			q.push({ now-1,step + 1 });
		}
		if (now <= ed &&!vis[now*2]) {
			vis[now*2] = true;
			q.push({ now*2,step + 1 });
		}
	}
	cout << "no" << endl;
}
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> st >> ed;
	bfs();
	return 0;
}

 法二:开dis数组存放步数——105ms,比pair慢

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int N=2e5+10;

int n,k,dist[N];


int bfs()
{
    memset(dist,-1,sizeof dist);
    
    queue<int> q;
    q.push(n);
    dist[n]=0;
    
    while(q.size())
    {
        int t=q.front();
        q.pop();
        
        if(t==k) return dist[k];
        
        if(t+1<N&&dist[t+1]==-1)
        {
            dist[t+1]=dist[t]+1;
            q.push(t+1);
        }
        
        if(t-1>=0&&dist[t-1]==-1)
        {

            dist[t-1]=dist[t]+1;
            q.push(t-1);
        }
        
        if(2*t<N&&dist[2*t]==-1)
        {
            dist[2*t]=dist[t]+1;
            q.push(2*t);
        }
    }
    
    return -1;
}

int main()
{
    cin>>n>>k;
    
    cout<<bfs()<<endl;
    
    return 0;
}

这种写法很方便

	while (q.size()) {
		int now = q.front().first;
		int step = q.front().second;
		q.pop();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果您想使用 BFS(广度优先搜索)算法来实现小车在迷宫中的路径规划,可以考虑在程序中使用坐标表示小车当前的位置。以下是一个示例: ```c //定义迷宫大小 #define MAZE_WIDTH 10 #define MAZE_HEIGHT 10 //定义迷宫地图 int maze[MAZE_HEIGHT][MAZE_WIDTH] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 0, 1, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1, 0, 1}, {1, 0, 1, 1, 1, 1, 0, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; //定义坐标结构体 struct Coordinate { int x; int y; }; //定义队列结构体 struct Queue { Coordinate data[MAZE_WIDTH * MAZE_HEIGHT]; int front; int rear; }; //初始化队列 void initQueue(Queue *q) { q->front = 0; q->rear = 0; } //判断队列是否为空 int isQueueEmpty(Queue *q) { return (q->front == q->rear); } //入队 void enqueue(Queue *q, Coordinate c) { q->data[q->rear] = c; q->rear++; } //出队 Coordinate dequeue(Queue *q) { Coordinate c = q->data[q->front]; q->front++; return c; } //判断坐标是否合法 int isValidCoordinate(Coordinate c) { return (c.x >= 0 && c.x < MAZE_WIDTH && c.y >= 0 && c.y < MAZE_HEIGHT && maze[c.y][c.x] == 0); } //BFS算法 void bfs(Coordinate start, Coordinate end) { Queue q; initQueue(&q); int visited[MAZE_HEIGHT][MAZE_WIDTH] = {0}; //记录是否访问过 int distance[MAZE_HEIGHT][MAZE_WIDTH] = {0}; //记录距离 Coordinate prev[MAZE_HEIGHT][MAZE_WIDTH]; //记录路径 enqueue(&q, start); visited[start.y][start.x] = 1; while (!isQueueEmpty(&q)) { Coordinate current = dequeue(&q); if (current.x == end.x && current.y == end.y) { break; } Coordinate next; next.x = current.x + 1; next.y = current.y; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } next.x = current.x - 1; next.y = current.y; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } next.x = current.x; next.y = current.y + 1; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } next.x = current.x; next.y = current.y - 1; if (isValidCoordinate(next) && !visited[next.y][next.x]) { enqueue(&q, next); visited[next.y][next.x] = 1; distance[next.y][next.x] = distance[current.y][current.x] + 1; prev[next.y][next.x] = current; } } //输出路径 Coordinate c = end; while (c.x != start.x || c.y != start.y) { Serial.print("("); Serial.print(c.x); Serial.print(", "); Serial.print(c.y); Serial.println(")"); c = prev[c.y][c.x]; } Serial.print("("); Serial.print(start.x); Serial.print(", "); Serial.print(start.y); Serial.println(")"); } ``` 此示例中,使用 `Coordinate` 结构体表示小车在迷宫中的坐标,使用 `Queue` 结构体实现 BFS 算法的队列。`isValidCoordinate` 函数判断坐标是否合法,`bfs` 函数实现广度优先搜索,并输出路径。在实际使用时,您需要根据具体情况修改和完善代码,例如根据传感器数据更新小车坐标等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou_fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值