提交时间:2022-10-04 11:33:08

运行 ID: 57517

#include <iostream> #include <map> #include <queue> using namespace std; struct Node { long long x,stp; }; map<long long,long long>p; int d,l,s; inline long long abs(long long x) { if(x < 0) return -x; return x; } void BFS(int x) { queue<Node>q; p.clear(); q.push((Node){1,0}); while(!q.empty()) { Node ak = q.front(); q.pop(); if(ak.x == x) { s = ak.stp; break; } if(!p[ak.x<<1]) { p[ak.x<<1] = ak.x; q.push((Node){ak.x<<1,ak.stp + 1}); } if(ak.x % 3 == 1) { if(!p[ak.x / 3]) { p[ak.x / 3] = ak.x; q.push((Node){ak.x / 3,ak.stp + 1}); } } if(min(abs(ak.x),abs(ak.x - d)) <= l&&!p[ak.x - d]) { p[ak.x - d] = ak.x; q.push((Node){ak.x - d,ak.stp + 1}); } } } void Path(int x) { if(x == 1) { cout<<1<<' '; return; } Path(p[x]); cout<<x<<' '; return; } int main() { int T; cin>>T>>d>>l; while(T--) { int x; cin>>x; BFS(x); cout<<s<<' '; Path(x); cout<<endl; } return 0; }