搜索(队列)

农夫约翰得知一头逃犯奶牛的位置,他从点N出发,目标是点K,两者都在数轴上。约翰有两种移动方式:步行和瞬移。步行可以向左或向右移动1单位,瞬移可以将位置变为原来的2倍。如果奶牛不动,约翰如何最快抓住它?输入包含两个整数N和K,输出约翰抓到奶牛的最短时间。例如,当N=5, K=17时,最短路径5-10-9-18-17需要4分钟。" 132932429,20036702,SDIO总线在音视频设备中的应用解析,"['音视频开发', '接口标准', '数据传输', '嵌入式硬件']

深搜广搜的队列写法模板

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 <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int book[1000010];
int n,k;
struct node
{
    int x,step;
};
int cmp(int x)
{
    if(x<0 || x>=1000000 || book[x])
        return 0;
    return 1;
}
int bfs(int x)
{
    int i;
    queue<node> Q;
    node a,next;
    a.x = x;
    a.step = 0;
    book[x] = 1;
    Q.push(a);
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(a.x == k)
            return a.step;
        next = a;
        next.x = a.x+1;
        if(cmp(next.x))
        {
            next.step = a.step+1;
            book[next.x] = 1;
            Q.push(next);
        }
        next.x = a.x-1;
        if(cmp(next.x))
        {
            next.step = a.step+1;
            book[next.x] = 1;
            Q.push(next);
        }
        next.x = a.x*2;
        if(cmp(next.x))
        {
            next.step = a.step+1;
            book[next.x] = 1;
            Q.push(next);
        }
    }
    return -1;
}
 
int main()
{
    int ans;
    while(~scanf("%d%d",&n,&k))
    {
        memset(book,0,sizeof(book));
        ans = bfs(n);
        printf("%d\n",ans);
    }
    return 0;
}
### 如何使用指针实现广度优先搜索(BFS)中的队列 在广度优先搜索(BFS)中,队列是一种常用的数据结构,用于存储待访问的节点。通常情况下,队列可以通过数组、链表或循环队列实现。如果需要使用指针来实现队列,则可以通过动态内存分配和指针操作完成。以下是基于指针实现队列的具体方法[^1]。 #### 队列的基本操作 队列是一种遵循“先进先出”(FIFO)原则的数据结构。以下是队列的核心操作: - **入队(Enqueue)**:将元素添加到队列的末尾。 - **出队(Dequeue)**:从队列的前端移除元素。 - **获取队首元素(Front)**:返回队列的第一个元素而不移除它。 - **检查队列是否为空(Empty)**:判断队列中是否有元素。 #### 使用指针实现队列的步骤 为了实现队列,可以定义一个节点结构体,其中包含数据部分和指向下一个节点的指针。通过维护两个指针(`front` 和 `rear`),分别指向队列的头和尾,可以高效地进行入队和出队操作。 #### 示例代码 以下是一个基于指针实现的队列代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 定义队列结构体 typedef struct Queue { Node* front; Node* rear; } Queue; // 初始化队列 Queue* createQueue() { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->front = queue->rear = NULL; return queue; } // 入队操作 void enqueue(Queue* queue, int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("无法分配内存\n"); return; } newNode->data = value; newNode->next = NULL; if (queue->rear == NULL) { // 如果队列为空 queue->front = queue->rear = newNode; } else { queue->rear->next = newNode; // 将新节点连接到队尾 queue->rear = newNode; // 更新队尾指针 } } // 出队操作 int dequeue(Queue* queue) { if (queue->front == NULL) { printf("队列为空\n"); return -1; // 返回错误值 } Node* temp = queue->front; int value = temp->data; queue->front = queue->front->next; // 移动队首指针 if (queue->front == NULL) { // 如果队列变空 queue->rear = NULL; } free(temp); // 释放节点内存 return value; } // 检查队列是否为空 int isEmpty(Queue* queue) { return queue->front == NULL; } // 获取队首元素 int getFront(Queue* queue) { if (isEmpty(queue)) { printf("队列为空\n"); return -1; } return queue->front->data; } // 测试队列功能 int main() { Queue* queue = createQueue(); enqueue(queue, 10); enqueue(queue, 20); enqueue(queue, 30); printf("队首元素: %d\n", getFront(queue)); printf("出队元素: %d\n", dequeue(queue)); printf("出队元素: %d\n", dequeue(queue)); printf("队首元素: %d\n", getFront(queue)); return 0; } ``` #### 代码说明 1. **节点结构体**:每个节点包含一个整型数据和一个指向下一个节点的指针。 2. **队列结构体**:包含两个指针,分别指向队列的头(`front`)和尾(`rear`)。 3. **入队操作**:创建新节点并将其连接到队列的尾部,同时更新`rear`指针。 4. **出队操作**:移除队列头部的节点,并更新`front`指针。如果队列变为空,则将`rear`也置为`NULL`。 5. **检查队列是否为空**:通过判断`front`是否为`NULL`来确定队列是否为空。 #### 广度优先搜索(BFS)中的应用 在广度优先搜索中,可以使用上述队列来存储待访问的节点。每次从队列中取出一个节点进行处理,并将其相邻节点依次加入队列。这种实现方式能够保证按照层次顺序访问图中的节点[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值