来追梦2026寒假DFS专题入门总结

· · 个人记录

今日的DFS主题,满分500分,AC了4道,得到450分,有一道得了50分,是因为题目有点"小bug",丢失了50分。错题总结+题目分析如下:

一,错题总结

T3 : T726114 家族

错因:

题目有点"小bug",不能直接判 != '*' && != ' ',得判 <= 'a' || >= 'z', 不然会WA五个点。

注意:

  1. 数组不能只开a[100][100],要开a[100][200],不然会WA四个点。
  2. 记得在输完n之后吃掉回车。

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int n, ans;
string s;
int m[N];
char a[N][2 * N];
bool f[N][2 * N];
int dx[5] = {0, 0, -1, 1};
int dy[5] = {-1, 1, 0, 0};
void dfs(int x, int y){
    if(x < 1 || x > n || y < 1 || y > m[x]) return;//边界
    if(f[x][y] == true) return;//是否走到过
    if(a[x][y] < 'a' || a[x][y] > 'z') return;//判是否符合要求
    f[x][y] = true;//标记
    for(int i = 0; i < 4; i++)
        dfs(x + dx[i], y + dy[i]);//转移方程
}
int main(){
    cin >> n;
    getline(cin, s);//一定记得吃掉回车!
    for(int i = 1; i <= n; i++){
        getline(cin, s);
        for(int j = 0; j < s.size(); j++)
            a[i][j + 1] = s[j];
        m[i] = s.size();
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m[i]; j++)
            if(a[i][j] >= 'a' && a[i][j] <= 'z' && f[i][j] == false){
                ans++;
                dfs(i, j);
            }
    cout << ans; 
    return 0;
}

二,题目分析

T1: T726111 跳马问题

类型:

DFS求到达方式数量

简易思路:

DFS,到终点ans++,最后输出ans

注意:

要判边界,不能判是否到达

代码:

#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
int dx[5] = {-2, -1, 1, 2};
int dy[5] = {1, 2, 2, 1};
void dfs(int x, int y){
    if(x == n && y == m){
        ans++;
        return;
    }
    if(x < 0 || x > n || y < 0 || y > m) return;
    for(int i = 0; i < 4; i++)
        dfs(x + dx[i], y + dy[i]);
}
int main(){
    cin >> n >> m;
    dfs(0, 0);
    cout << ans;
    return 0;
} 

这里就不具体注释了~

T2: T726113 求细胞数量

类型:

连通块个数

简易思路:

遍历每个点,找它属于的一整个连通块,ans++,最后输出ans即可。

注意:

要判它走没走过

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int n, m, ans;
char a[N][N];
bool f[N][N];
int dx[5] = {0, 0, -1, 1};
int dy[5] = {-1, 1, 0, 0};
void dfs(int x, int y){
    if(x < 1 || x > n || y < 1 || y > m) return;
    if(a[x][y] == '0') return;
    if(f[x][y] == true) return;
    f[x][y] = true;
    for(int i = 0; i < 4; i++)
        dfs(x + dx[i], y + dy[i]);
}
int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> a[i][j];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(a[i][j] != '0' && f[i][j] == false){
                ans++;
                dfs(i, j);
            }
    cout << ans;
    return 0;
}

这里就不具体注释了~

T3已在错题总结里分析过了

T4: T726117 海战

类型:

求连通块数量

简易思路:

先判每个点是否合法,再按正常的DFS连通块求解即可。

注意:

要判是否合法

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int r, c, ans;
char a[N][N];
bool f[N][N];
int dx[5] = {0, 0, -1, 1};
int dy[5] = {-1, 1, 0, 0};
bool Bp(int x, int y){
    int cnt = 0;
    if(a[x][y] == '#') cnt++;
    if(a[x + 1][y] == '#') cnt++;
    if(a[x][y + 1] == '#') cnt++;
    if(a[x + 1][y + 1] == '#') cnt++;
    return cnt == 3;
}//判断是否合法 
void dfs(int x, int y){
    if(x < 1 || x > r || y < 1 || y > c) return;
    if(f[x][y] == true || a[x][y] == '.') return;
    f[x][y] = true;
    for(int i = 0; i < 4; i++)
        dfs(x + dx[i], y + dy[i]);
}
int main(){
    cin >> r >> c;
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= c; j++)
            cin >> a[i][j];
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= c; j++)
            if(Bp(i, j) == true){
                cout << "Bad placement.";
                return 0;
            } 
            else if(a[i][j] == '#' && f[i][j] == false){
                ans++;
                dfs(i, j);
            }
    printf("There are %d ships.", ans);
    return 0;
}

T5: T663551 小田玩扫雷

类型:

连通块BFS+简易标记数组

简易思路:

先求周围八格没有 “空格” 的数字个数,再求“空” 的个数,最后求和。

注意:

细心细心再细心,要仔细看for循环的变量名

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, m, a[N][N], ans1, ans2;
bool f[N][N];
int dx[10] = {-1, -1, -1, 1, 1, 1, 0, 0};
int dy[10] = {-1, 0, 1, -1, 0, 1, -1, 1};
void dfs(int x, int y){
    if(x < 1 || x > n || y < 1 || y > m) return;
    if(a[x][y] != 3 || f[x][y] == true) return;
    f[x][y] = true;
    for(int i = 0; i < 8; i++)
        dfs(x + dx[i], y + dy[i]);
}
int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> a[i][j];
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            if(a[i][j] == 1) continue;
            bool flag = true;
            for(int k = 0; k < 8; k++){
                int nx = i + dx[k];
                int ny = j + dy[k];
                if(a[nx][ny] == 1){
                    flag = false;
                    break;
                }
            }
            if(flag == false) a[i][j] = 2;
            else a[i][j] = 3;
        }//标记数组 
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            if(a[i][j] != 2) continue;
            bool flag = true;
            for(int k = 0; k < 8; k++){
                int nx = i + dx[k];
                int ny = j + dy[k];
                if(a[nx][ny] == 3){
                    flag = false;
                    break;
                }
            }
            if(flag == true) ans1++;
        }//周围八格没有 “空格” 的数字个数
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(a[i][j] == 3 && f[i][j] == false){
                ans2++;
                dfs(i, j);
            }//“空” 的个数
    cout << ans1 + ans2;
    return 0;
}

这就是今日的题目总结,请jw老师查收~