QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#545759#6621. Luggage LockwdwTL 1ms4552kbC++202.2kb2024-09-03 16:58:292024-09-03 16:58:29

Judging History

你现在查看的是最新测评结果

  • [2024-09-03 16:58:29]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:4552kb
  • [2024-09-03 16:58:29]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define int long long
#define endl '\n'
const int N = 1e5 + 10;
const int mod = 998244353;
using i64 = int64_t;
int w1[5]={1,10,100,1000,10000};
int vis[N],dis[N];
void solve() {
    string a,b;
    cin>>a>>b;
    queue<int>q;
    int a1=0,b1=0;
    for(int i=1;i<=4;i++){
        a1=a1*10+(a[i-1]-'0');
        b1=b1*10+(b[i-1]-'0');
    }
    vector<int>px;
    dis[a1]=0;
    q.push(a1);
    px.push_back(a1);
    while(!q.empty()){
        int y=q.front();
        q.pop();
        if(vis[y])continue;
        vis[y]=1;
        if(y==b1){
            cout<<dis[y]<<'\n';
            break;
        }
        px.push_back(y);
        vector<int>q1(5),q2(5);
        int w2=y;
        for(int i=4;i>=1;i--){
            int p=w2%10;
            w2/=10;
            if(p!=9){
                q1[i]+=w1[(4-i)];
            }else{
                q1[i]-=9*w1[(4-i)];
            }
            if(p!=0){
                q2[i]-=w1[(4-i)];
            }else{
                q2[i]+=(9*w1[(4-i)]);
            }
        }
        //cout<<q2[1]<<" "<<q2[0]<<'\n';
        for(int i=1;i<=4;i++){
            q1[i]+=q1[i-1];
            q2[i]+=q2[i-1];
        }
        for(int i=1;i<=4;i++){
            for(int j=i;j<=4;j++){
                int sum1=y+q1[j]-q1[i-1];
                int sum2=y+q2[j]-q2[i-1];
                //cout<<sum1<<" "<<sum2<<"\n";
                if(dis[sum1]>dis[y]+1){
                    dis[sum1]=dis[y]+1;
                    q.push(sum1);
                }
                if(dis[sum2]>dis[y]+1){
                    dis[sum2]=dis[y]+1;
                    q.push(sum2);
                }
            }
        }
    }
    while(!q.empty()){
        int y=q.front();
        q.pop();
        if(vis[y])continue;
        vis[y]=1;
        px.push_back(y);
    }
    for(int j=0;j<px.size();j++){
        vis[px[j]]=0;
        dis[px[j]]=1e9;
    }
    px.clear();
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    //cout<<fixed<<setprecision(3);
    int T = 1;
     cin>>T;
    for(int j=0;j<1e5;j++){
        dis[j]=1e9;
    }
    while (T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4552kb

input:

6
1234 2345
1234 0123
1234 2267
1234 3401
1234 1344
1234 2468

output:

1
1
4
5
1
4

result:

ok 6 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

100000
9138 9251
0887 4577
2745 6294
6883 1197
8654 2502
7867 7941
7505 2717
1497 7726
3337 8042
2767 8782
5311 4368
9711 6229
7105 8101
3553 6673
6544 7940
8848 8920
1458 6315
4605 2931
5505 7310
5963 4765
7625 0418
4049 0136
2078 6899
3339 1080
2276 6814
4249 4182
1861 0824
8975 8926
6447 6965
226...

output:


result: