警示后人
查看原帖
警示后人
824080
Roy100328楼主2023/8/22 10:43

警示后人

我开始还没发现那错了:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int t,x,y;
int vis[100005];
struct Node {
	int a,b;
};

int check(int a) {
	if(a<=0||a>100000)
		return 0;
	if(vis[a]==1)
		return 0;
	return 1;
}
void bfs(int x,int sum) {
	queue<Node> q;
	q.push(Node {x,sum});
	vis[x]=1;
	while(q.size()) {
		int hx=q.front().a;	//从队头取出 坐标和步数
		int hs=q.front().b;
		q.pop();
		if(hx==y) {			//验证队头是否是目标
			cout<<hs;
			return ;
		}
		int nx;
		nx=hx-1;	//决策1后退一步
		if(check(nx)) {
			vis[nx]=1;
			q.push(Node {nx,hs+1});
		}
		nx=hx+1;	//决策2 前进一步
		if(check(nx)) {
			vis[nx]=1;
			q.push(Node {nx,hs+1});
		}
		nx=hx*2;	//步数翻倍
		if(check(nx)) {
			vis[nx]=1;
			q.push(Node {nx,hs+1});
		}
	}
}
int main() {
	cin>>t;
	for(int i=1; i<=t; i++) {
		memset(vis,0,sizeof(vis));
		cin>>x>>y;		//从x到y
		bfs(x,0);		//从x开始走0步
	}
	return 0;
}

直到后来才发现我的输出(帖子第30行),没换行……

2023/8/22 10:43
加载中...