2026.1.27_401_总结

· · 个人记录

总分

\color{green} 90pts$+$\color{green} 100pts$+$\color{green} 100pts$+$\color{red} 25pts$+$\color{red} 0pts$=$\color{yellow} 315pts

P1588 [USACO07OPEN] Catch That Cow S

\color{green} 90pts

死因

边界条件f.x <= m*2了,没有考虑到开始时就n > m*2的情况

思路

一维bfs模板,多测记得换行、清空

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(2e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
int n, m;
bool vis[N];

struct Node{
    int x, step;
}; 

void bfs(){
    queue<Node> q;
    q.push({n, 0});
    vis[n] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(f.x == m){
            cout << f.step << "\n";
            return;
        }
        int tx = f.x + 1;
        if(tx > 0 && tx <= max(n, m * 2) && !vis[tx]){
            q.push({tx, f.step + 1});
            vis[tx] = 1;
        }
        tx = f.x - 1;
        if(tx > 0 && tx <= max(n, m * 2) && !vis[tx]){
            q.push({tx, f.step + 1});
            vis[tx] = 1;
        }
        tx = f.x * 2;
        if(tx > 0 && tx <= max(n, m * 2) && !vis[tx]){
            q.push({tx, f.step + 1});
            vis[tx] = 1;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        cin >> n >> m;
        bfs();
        for(int i = 1; i <= m * 2; i++){
            vis[i] = 0;
        }
    }
    return 0;
}

P1135 奇怪的电梯

\color{green} 100pts

思路

一维bfs模板

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(2e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
int n, a, b, x[N];
bool vis[N];

struct Node{
    int x, step;
}; 

void bfs(){
    queue<Node> q;
    q.push({a, 0});
    vis[a] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(f.x == b){
            cout << f.step;
            return;
        }
        int tx = f.x + x[f.x];
        if(tx > 0 && tx <= n && !vis[tx]){
            q.push({tx, f.step + 1});
            vis[tx] = 1;
        }
        tx = f.x - x[f.x];
        if(tx > 0 && tx <= n && !vis[tx]){
            q.push({tx, f.step + 1});
            vis[tx] = 1;
        }
    }
    cout << "-1"; 
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> a >> b;
    for(int i = 1; i <= n; i++){
        cin >> x[i];
    }
    bfs();
    return 0;
}

T725765 Sramoc 问题(EZ)

\color{green} 100pts

思路

枚举k的倍数,分解出每一位,看是否小于n,这个数据范围long long可过

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    for(ll i = k; ; i += k){
        ll j = i;
        bool flag = 0;
        while(j){
            if(j % 10 >= n){
                flag = 1;
                break;
            }
            j /= 10;
        }
        if(!flag){
            cout << i << "\n";
            return 0;
        }
    }
    return 0;
}

T725774 落落的去的数学问题五

\color{red} 25pts

死因

死磕模拟+剪枝,结果改了半天,拼尽全力无法过n=100000的样例

思路

bfs初始时加入1~9(因为一位整数一定是落落的去的数),bfs时入队当前数*10 + 与当前数个位上数字绝对值不超过一的数

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(2e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
int n, m, xx;
bool vis[N];

void bfs(){
    queue<ll> q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    q.push(5);
    q.push(6);
    q.push(7);
    q.push(8);
    q.push(9);
    while(q.size()){
        xx++;
        ll f = q.front();
        q.pop();
        if(xx >= n){
            cout << f;
            return;
        }
        for(int i = (f % 10) - 1; i <= (f % 10) + 1; i++){
            if(i < 0 || i > 9){
                continue;
            } 
            q.push(f * 10 + i);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    bfs();
    return 0;
}

P2730 [IOI 1996 / USACO3.2] 魔板 Magic Squares

\color{red} 0pts

死因

没时间了QwQ

思路

题目要求字典序最小,所以进行操作的顺序是先A,再B,最后C,进行操作时无任何技巧,题目怎样就怎样,直接进行,记录下答案,最后输出操作数+操作即可

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
string st = "12348765", ed;
unordered_map<string, bool> mp;

struct Node{
    int step;
    string s, path;
};

string A(string s){
    string ts = s;
    for(int i = 0; i < 4; i++){
        swap(ts[i], ts[i + 4]);
    }
    return ts;
}

string B(string s){
    string ts = s;
    char a = ts[0], b = ts[1], c = ts[2], d = ts[3], e = ts[4], f = ts[5], g = ts[6], h = ts[7];
    ts[0] = d;
    ts[4] = h;
    ts[1] = a;
    ts[2] = b;
    ts[3] = c;
    ts[5] = e;
    ts[6] = f;
    ts[7] = g;
    return ts;
}

string C(string s){
    string ts = s;
    char a = ts[1], b = ts[2], c = ts[5], d = ts[6];
    ts[2] = a;
    ts[6] = b;
    ts[5] = d;
    ts[1] = c;
    return ts;
}

void bfs(){
    queue<Node> que;
    que.push({0, st, ""});
    mp[st] = true;
    while(que.size()){
        Node f = que.front();
        que.pop();
        if(f.s == ed){
            cout << f.step << "\n" << f.path;
            return;
        }
        string ts = A(f.s);
        if(!mp[ts]){
            que.push({f.step + 1, ts, f.path + "A"});
            mp[ts] = 1;
        }
        ts = B(f.s);
        if(!mp[ts]){
            que.push({f.step + 1, ts, f.path + "B"});
            mp[ts] = 1;
        }
        ts = C(f.s);
        if(!mp[ts]){
            que.push({f.step + 1, ts, f.path + "C"});
            mp[ts] = 1;
        }
    }
}

int main(){
    ed = "HahAKIOI";
    int c;
    for(int i = 0; i < 4; i++){
        cin >> c;
        ed[i] = c + '0';
    }
    for(int i = 1; i <= 4; i++){
        cin >> c;
        ed[8 - i] = c + '0';
    }
    bfs();
    return 0;
}