跳到正文

个人记录

7.9-晚上河西教室1 -- 广搜4--一维广搜

zqrmon约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年7月10日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 9 分钟

版本

  1. v1当前

T579124 影分身过河

C++
#include <bits/stdc++.h>
using namespace std;

// 定义状态结构体:pos 表示当前所在的木桩下标,step 表示已使用的体力
struct node {
    int pos;
    int step;
};

queue<node> q;             // BFS 使用的队列
int visited[20005];        // 记录每个位置是否已经被访问过,防止重复入队

int main() {
    int n;
    cin >> n;               // 输入木桩数量
    int a[20005];           // 存储每个木桩的魔法值

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    // 初始状态:站在第 0 号木桩(对应编号为 1),已经用了 0 点体力
    q.push({0, 0});
    visited[0] = 1;         // 标记起点已访问

    while (!q.empty()) {
        node f = q.front(); // 取出当前状态
        q.pop();

        int c = f.pos;      // 当前所在木桩的下标
        int step = f.step;  // 当前已用的体力值

        // 如果当前位置 >= n,表示已经成功过河
        if (c >= n) {
            cout << step << endl;
            return 0;
        }

        // 情况一:当前木桩魔法值 > 0
        if (a[c] > 0) {
            // 可以跳跃到 [c+1, c+a[c]] 范围内的任意木桩
            for (int k = 1; k <= a[c]; k++) {
                int next = c + k;

                // 如果跳到了编号 >= n 的位置,则成功过河
                // 或者如果该位置还没访问过,就加入队列继续探索
                if (next >= n || (next < n && !visited[next])) {
                    visited[next] = 1;
                    q.push({next, step + 1});  // 步数+1,即体力+1
                }
            }
        } 
        // 情况二:当前木桩魔法值 ≤ 0
        else {
            // 可以跳到 [0, c + a[c]] 范围内的任意木桩(注意是数组下标)
            for (int k = 0; k <= c + a[c]; k++) {
                if (!visited[k]) {
                    visited[k] = 1;
                    q.push({k, step + 1});
                }
            }
        }
    }

    // 队列空了说明无法到达终点
    cout << -1 << endl;
    return 0;
}

T582141 星际跃迁追踪协议

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b; // 输入起始坐标 a 和目标坐标 b

    // 特殊情况处理:如果起点在终点或更远的位置,只能一步一步往回走
    if (a >= b) {
        cout << a - b << endl;
        return 0;
    }

    // 定义最大可能坐标范围(根据题目中a,b<=1e5,并考虑翻倍操作)
    const int MAX = 200000;

    // visited 数组记录每个位置是否已经访问过,防止重复入队
    bool visited[MAX + 1] = {false};

    // 使用队列进行 BFS 搜索,pair<当前坐标, 当前步数>
    queue<pair<int, int>> q;

    // 起点入队
    q.push({a, 0});
    visited[a] = true;

    while (!q.empty()) {
        int current = q.front().first;     // 当前所在位置
        int steps = q.front().second;      // 到达该位置所需的步数
        q.pop();

        // 如果当前位置等于目标位置,输出结果并结束
        if (current == b) {
            cout << steps << endl;
            return 0;
        }

        int next;

        // 操作一:量子滑移向左(y -> y-1)
        next = current - 1;
        if (next >= 0 && !visited[next]) {
            visited[next] = true;
            q.push({next, steps + 1});
        }

        // 操作二:量子滑移向右(y -> y+1)
        next = current + 1;
        if (next <= MAX && !visited[next]) {
            visited[next] = true;
            q.push({next, steps + 1});
        }

        // 操作三:超膜折叠(y -> 2*y)
        next = current * 2;
        if (next <= MAX && !visited[next]) {
            visited[next] = true;
            q.push({next, steps + 1});
        }
    }

    // 理论上不会执行到这里,因为总能通过减法回到原点或接近目标
    return 0;
}

P1588 [USACO07OPEN] Catch That Cow S

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10 + 2e5;
int n, k,d,q[N],dist[N];
int bfs() {
	memset(dist, -1, sizeof(dist));
	dist[n] = 0;
	q[0] = n;
	int hh = 0, tt = 0;
	while (hh <= tt) {
		int t = q[hh++];
		if (t == k) return dist[t];
		if (t + 1 <= k && dist[t + 1] == -1) {
			dist[t + 1] = dist[t] + 1;
			q[++tt] = t + 1;
		}
		if (t - 1 >= 0 && dist[t - 1] == -1) {
			dist[t - 1] = dist[t] + 1;
			q[++tt] = t - 1;
		}
		if (t * 2 < N && dist[t * 2] == -1) {
			dist[t * 2] = dist[t] + 1;
			q[++tt] = t * 2;
		}
	}
	return -1;
}
int main() {
	cin >> d;
	while (d--) {
		cin >> n >> k;
		cout << bfs() << endl;
	}
	return 0;
}

B3626 跳跃机器人

C++
#include<bits/stdc++.h>
using namespace std;
int vis[1000010],n;
queue<int> q;

void bfs()
{
	memset(vis,-1,sizeof vis);
	q.push(1);
	vis[1]=0;
	while(!q.empty()){
		int t=q.front();
		q.pop();
		if(t==n){
			cout<<vis[n];
			return;
		}
		if(t-1>=1&&t-1<=n&&vis[t-1]==-1){
			q.push(t-1);
			vis[t-1]=vis[t]+1;
		}
		if(t+1>=1&&t+1<=n&&vis[t+1]==-1){
			q.push(t+1);
			vis[t+1]=vis[t]+1;
		}
		if(t*2>=1&&t*2<=n&&vis[t*2]==-1){
			q.push(t*2);
			vis[t*2]=vis[t]+1;
		}
	}
	return;
}

int main()
{
	cin>>n;
	bfs();
	return 0;
}
原文