2026.1.25_401_总结

· · 个人记录

总分

\color{green}100pts$+$\color{green}100pts$+$\color{red}10pts$+$\color{green}100pts$+$\color{yellow}60pts$=$\color{yellow}370pts

T725238 bfs1

\color{green} 100pts

思路

广搜模板题,板得不能再板

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e3 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char a[N][N];
bool vis[N][N];
int sx, sy, ex, ey, n;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

struct Node{
    int x, y, step;
};

void bfs(){
    queue<Node> q;
    q.push({sx, sy, 0});
    vis[sx][sy] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(f.x == ex && f.y == ey){
            cout << f.step;
            return;
        }
        for(int i = 0; i < 4; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > 0 && ty > 0 && tx <= n && ty <= n && !vis[tx][ty] && a[tx][ty] != '1'){
                vis[tx][ty] = 1;
                q.push({tx, ty, f.step + 1});
            }
        }
    }
}

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

T725273 bfs2

\color{green} 100pts

思路

与第一题很相似,需要修改的只有记录、输出答案,以及移动的数组,以及SPJ

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(4e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
bool vis[N][N];
int sx, sy, n, m, dis[N][N];
int dx[] = {-1, 1, 2, 2, 1, -1, -2, -2};
int dy[] = {-2, -2, -1, 1, 2, 2, 1, -1};

struct Node{
    int x, y, step;
};

void bfs(){
    queue<Node> q;
    q.push({sx, sy, 0});
    vis[sx][sy] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        for(int i = 0; i < 8; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty]){
                vis[tx][ty] = 1;
                q.push({tx, ty, f.step + 1});
                dis[tx][ty] = f.step + 1;
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> sx >> sy;
    bfs();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(!vis[i][j]){
                cout << "-1 ";
                continue;
            }
            cout << dis[i][j] << " "; 
        } 
        cout << "\n";
    }
    return 0;
}

T725275 bfs3

\color{red} 10pts

死因

数组开小了。。。

思路

bfs开始的时候直接把所有感染源入队,bfs时记录下所有人被感染的时间,最后输出所有领主被感染的时间即可

其余的与第一题无异

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(5e2 + 10)
#define M (int)(1e5 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
bool vis[N][N];
int n, m, a, b, dis[N][N], x[M], y[M], lzx[M], lzy[M];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

struct Node{
    int x, y, step;
};

void bfs(){
    queue<Node> q;
    for(int i = 1; i <= a; i++){
        q.push({x[i], y[i], 0});
        vis[x[i]][y[i]] = 1;
    }
    while(q.size()){
        Node f = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty]){
                vis[tx][ty] = 1;
                q.push({tx, ty, f.step + 1});
                dis[tx][ty] = f.step + 1;
            }
        }
    }
}

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

T725279 bfs4

\color{green} 100pts

思路

bfs多了记录路径,方法:

bfs队列结构体中多一个string s,用来记录路径

bfs前入队的s为"1 1",bfs时每走一次入队的s就是 上一步的s + "\n" + 这步的x坐标 + " " + 这步的y坐标,到达后输出当时的s即可

代码

#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;
char a[N][N];
bool vis[N][N];
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

struct Node{
    int x, y;
    string s;
};

void bfs(){
    queue<Node> q;
    q.push({1, 1, "1 1"});
    vis[1][1] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(f.x == n && f.y == m){
            cout << f.s;
            return;
        }
        for(int i = 0; i < 4; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && a[tx][ty] != '*'){
                vis[tx][ty] = 1;
                q.push({tx, ty, f.s + "\n" + to_string(tx) + " " + to_string(ty)});
            }
        }
    }
}

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

P4328 [COCI 2006/2007 #1] Slikar

估值++

\color{yellow} 60pts

死因

把t数组初始化为0了,没有考虑洪水淹不过来的情况

思路

先bfs一遍记录下每个位置洪水淹过来的最小时间,再bfs一遍计算出答案,如果无法到达或会被淹,就输出"KAKTUS"

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
bool vis[N][N];
int sx, sy, ex, ey, n, m, t[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

struct Node{
    int x, y, step;
};
queue<Node> q;

void init(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(t[i][j] == 114514){
                q.push({i, j, 0});
                vis[i][j] = 1;
                t[i][j] = 0;
            }
        }
    }
    while(q.size()){
        Node f = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && t[tx][ty] != -1e9 && (tx != ex || ty != ey)){
                vis[tx][ty] = 1;
                q.push({tx, ty, f.step + 1});
                t[tx][ty] = f.step + 1;
            }
        }
    }
}

void bfs(){
    q.push({sx, sy, 0});
    vis[sx][sy] = 1;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(f.x == ex && f.y == ey){
            cout << f.step;
            return;
        }
        for(int i = 0; i < 4; i++){
            int tx = f.x + dx[i];
            int ty = f.y + dy[i];
            if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && t[tx][ty] > f.step + 1){
                vis[tx][ty] = 1;
                q.push({tx, ty, f.step + 1});
            }
        }
    }
    cout << "KAKTUS";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    char c;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            t[i][j] = 1e7;
            cin >> c;
            if(c == 'S'){
                sx = i;
                sy = j;
            }
            if(c == 'D'){
                ex = i;
                ey = j;
            }
            if(c == 'X'){
                t[i][j] = -1e9;
            }
            if(c == '*'){
                t[i][j] = 114514;
            }
        }
    }
    init();
    t[ex][ey] = 1e9;
    while(q.size()){
        q.pop();
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            vis[i][j] = 0;
        }
    }
    bfs();
    return 0;
}