30分,测试点2第六个数据莫名其妙多加一,用的BFS,求助各位
查看原帖
30分,测试点2第六个数据莫名其妙多加一,用的BFS,求助各位
664620
Alpex楼主2022/8/9 10:36
#include<bits/stdc++.h>
using namespace std;
bool book[400005];
struct node{
    int x,step;
};
int main(){
    int n,S,T;
    cin>>n;
    for(int i=0;i<n;i++){
    	cin>>S>>T;
    	queue<node> q;
    	memset(book,0,sizeof book);
        node ss={S,0};
        q.push(ss);
        while(1){
			node hh=q.front();
        	q.pop();
        	if(hh.x==T){
        	    cout<<hh.step<<endl; 
        	    break;
        	} 
        	for(int i=1;i<=3;i++){
        	    int temp;
        	    if(i==1) temp=hh.x+1;
        	    if(i==2) temp=hh.x-1;
        	    if(i==3) temp=hh.x*2;
        	    if(book[temp]==1) continue;
        	    if(temp<0||temp>T)continue;
        	    book[temp]=1;
        	    int hhh=hh.step+1;
        	    node hhhh={temp,hhh};
        	    q.push(hhhh);
       		}
    	}
    }
    return 0;
}
2022/8/9 10:36
加载中...