进行搜索时候记得 条件,,是否可以进入搜索 避免 超时 等等
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int dist[400];
int a,b,c,d;
void bfs()
{
memset(dist,0x3f,sizeof(dist));
queue<pair<int,int>>q;
q.push({10.,0});
dist[10]=0;
dist[300]=1;
q.push({300,1});
while(q.size())
{
auto k=q.front();
q.pop();
int id=k.first;
int tx=k.second;
dist[id]=min(dist[id],tx);
// if(id<10||id>300) continue;
if(id+1>=0&&id+1<=300&&dist[id+1]>tx+1)
{ dist[id+1]=tx+1;
q.push({id+1,tx+1});
}
if(id-1>=0&&id-1<=300&&dist[id-1]>tx+1)
{ dist[id-1]=tx+1;
q.push({id-1,tx+1});
}
if(id+10>=0&&id+10<=300&&dist[id+10]>tx+1)
{ dist[id+10]=tx+1;
q.push({id+10,tx+1});
}
if(id-10>=0&&id-10<=300&&dist[id-10]>tx+1)
{ dist[id-10]=tx+1;
q.push({id-10,tx+1});
}
if(id-100>=0&&id-100<=300&&dist[id-100]>tx+1)
{ dist[id-100]=tx+1;
q.push({id-100,tx+1});
}
if(id+100>=0&&id+100<=300&&dist[id+100]>tx+1)
{ dist[id+100]=tx+1;
q.push({id+100,tx+1});
}
}
}
void solve()
{ cin>>a>>b>>c>>d;
cout<<dist[a]+dist[b]+dist[c]+dist[d]<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t; cin>>t;
bfs();
while(t--)
{
solve();
}
return 0;
}