0pds求助,bfs,单组样例全过
查看原帖
0pds求助,bfs,单组样例全过
1057081
Evelyn_wsx_star楼主2025/8/4 08:49
#include<bits/stdc++.h>
using namespace std;
queue<int> q;
int h[4]= {0,1,-1,0};
int b[12000000];
int bs[12000000];
int main() {
	int T;
	cin>>T;
	while(T--) {
		int n,m;
		cin>>n>>m;
		q.push(n);
		if(n==m) {
			cout<<0;
			return 0;
		}
		while(!q.empty()) {
			int x=q.front();
			q.pop();
			h[3]=x;
			/*for(int i=1;i<=4;i++){
			    cout<<h[i]<<" ";
			}
			cout<<endl;*/
			for(int i=1; i<=3; i++) {

				//cout<<x+h[i]<<endl;
				if(b[x+h[i]]==0&&x+h[i]>=0&&x+h[i]<=200002) {
					b[x+h[i]]=1;
					bs[x+h[i]]=bs[x]+1;
					q.push(x+h[i]);
					if(x+h[i]==m) {
						cout<<bs[x+h[i]]<<endl;
						//continue;
					}
				}
			}
		}
	}

	return 0;
}
2025/8/4 08:49
加载中...