2026.1.29_401_总结

· · 个人记录

总分

\color{green}100pts$+$\color{green}100pts$+$\color{green}100pts$+$\color{green}100pts$+$\color{yellow}70pts$=$ \color{green} 470pts

P2105 K皇后

\color{green}100pts

思路

把所有皇后能攻击到的地方全部标记,遍历每一格,不会被攻击则答案加一,剪枝:当前一行被标记,直接去下一行。

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(4e4 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
bool h[N], l[N], zdj[N], fdj[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, m, k, x, y;
    cin >> n >> m >> k;
    int cnt = 0;
    for(int i = 1; i <= k; i++){
        cin >> x >> y;
        h[x] = 1;
        l[y] = 1;
        fdj[x + y] = 1;     
        zdj[x - y + 20000] = 1;
    } 
    for(int i = 1; i <= n; i++){
        if(h[i]){
            continue;
        }
        for(int j = 1; j <= m; j++){
            if(l[j]){
                continue;
            }
            if(zdj[i - j + 20000] || fdj[i + j]){
                continue;
            }
            cnt++;
        }
    }
    cout << cnt;
    return 0;
}

T726810 搜索进阶1.29

\color{green}100pts

思路

与模板(第三题差不多),记录的时候加上一个维度"数字",就能检查某个数字是否放过

代码

#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;
char c[N][N];
int cnt, n;
bool h[N][10], l[N][10], z[N + N][10], f[N][10];

void dfs(int x, int y){
    if(x > n){
        cnt++;
        return;
    }
    if(c[x][y] != '*'){
        if(y == n){
            dfs(x + 1, 1);
        }else{
            dfs(x, y + 1);
        }
        return;
    }
    for(int j = 1; j <= n; j++){
        if(!h[x][j] && !l[y][j] && !z[x - y + n][j] && !f[y + x][j]){
            h[x][j] = 1;
            l[y][j] = 1;
            z[x - y + n][j] = 1;
            f[y + x][j] = 1;
            if(y == n){
                dfs(x + 1, 1);
            }else{
                dfs(x, y + 1);
            }
            h[x][j] = 0;
            l[y][j] = 0;
            z[x - y + n][j] = 0;
            f[y + x][j] = 0;
        }
    }
}

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 >> c[i][j];
            if(c[i][j] != '*'){
                h[i][c[i][j] - '0'] = 1; 
                l[j][c[i][j] - '0'] = 1;
                z[i - j + n][c[i][j] - '0'] = 1;
                f[i + j][c[i][j] - '0'] = 1;
            }
        }
    }
    dfs(1, 1);
    cout << cnt;
    return 0;
}

P1219 [USACO1.5] 八皇后 Checker Challenge

\color{green}100pts

思路

八皇后模板

代码

#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;
int ans[N], cnt, n;
bool l[N], z[N + N], f[N];

void dfs(int x){
    if(x > n){
        if(++cnt <= 3){
            for(int i = 1; i <= n; i++){
                cout << ans[i] << " ";
            }
            cout << "\n";
        }
        return;
    }
    for(int i = 1; i <= n; i++){
        if(!l[i] && !z[i - x + n] && !f[i + x]){
            l[i] = 1;
            z[i - x + n] = 1;
            f[i + x] = 1;
            ans[x] = i;
            dfs(x + 1);
            l[i] = 0;
            z[i - x + n] = 0;
            f[i + x] = 0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    dfs(1);
    cout << cnt;
    return 0;
}

P7012 [CERC2013] Draughts

\color{green}100pts

思路

普通dfs搜索,每次查看八个方向是否有敌人,有的话看敌人是否被杀了,如果没有,就走这一步

统计出所有答案,取最大值

某个子开始移动时要把那个子标记为空白棋盘

代码

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

void dfs(int x, int y, int cnt){
    ans = max(ans, cnt);
    for(int i = 0; i < 8; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx + dx[i] > 0 && ty + dy[i] > 0 && tx + dx[i] <= 10 && ty + dy[i] <= 10 && (c[tx + dx[i]][ty + dy[i]] == '.' || c[tx + dx[i]][ty + dy[i]] == '#') && c[tx][ty] == 'B' && !vis[tx][ty]){
            vis[tx][ty] = 1;
            dfs(tx + dx[i], ty + dy[i], cnt + 1);
            vis[tx][ty] = 0;
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        for(int i = 1; i <= 10; i++){
            for(int j = 1; j <= 10; j++){
                cin >> c[i][j];
            }
        } 
        for(int i = 1; i <= 10; i++){
            for(int j = 1; j <= 10; j++){
                if(c[i][j] == 'W'){
                    c[i][j] = '.';
                    dfs(i, j, 0);
                    c[i][j] = 'W';
                }
            } 
        }
        cout << ans << "\n";
        ans = 0;
        for(int i = 1; i <= 10; i++){
            for(int j = 1; j <= 10; j++){
                vis[i][j] = 0;
            } 
        }
    }
    return 0;
}

P1562 还是 N 皇后

\color{yellow}70pts

死因

写的暴力

思路

可以把放皇后的状态压缩成放了是一,不放是零,通过取它的反码,然后每次取出lowbit,就能快速得出所有能放的地方

代码

#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;
char c[N][N];
int ans[N], cnt, n;
bool l[N], z[N + N], f[N];

void dfs(int x){
    if(x > n){
        cnt++;
        return;
    }
    for(int i = 1; i <= n; i++){
        if(c[x][i] == '.'){
            continue;
        } 
        if(!l[i] && !z[i - x + n] && !f[i + x]){
            l[i] = 1;
            z[i - x + n] = 1;
            f[i + x] = 1;
            ans[x] = i;
            dfs(x + 1);
            l[i] = 0;
            z[i - x + n] = 0;
            f[i + x] = 0;
        }
    }
}

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 >> c[i][j];
        }
    }
    dfs(1);
    cout << cnt;
    return 0;
}