>
  • 板块灌水区
  • 楼主Nostopathy
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/22 19:35
  • 上次更新2024/6/22 22:26:35
查看原帖
>
1286053
Nostopathy楼主2024/6/22 19:35

计蒜客 一维坐标的移动 50pts50\text{pts}

#include <iostream>
#include <queue>
using namespace std;
struct node {int first, second;} ;
queue <node> q;
int v[5002], n, a, b, cr, st;
void BFS(){
	q.push({a, 0});
	v[a]=1;
	while(q.size()){
		cr=q.front().first, st=q.front().second;
		q.pop();
		if(cr==b){cout << st; exit(0);}
		if(cr<n && v[cr+1]==0){q.push({cr+1, st+1}); v[cr+1]=1;}
		if(cr>0 && v[cr-1]==0){q.push({cr-1, st+1}); v[cr-1]=1;}
		if(cr*2<1+n && v[cr*2]==0){q.push({cr*2, st+1}); v[cr*2]=1;}
	}
}
int main(){
	cin >> n >> a >> b;
	BFS();
}

验证码 y98k

2024/6/22 19:35
加载中...