一道入门图搜索算法题

DFS:

c 复制代码
#include <stdio.h>

#define MAX 25          // 题目最大 20,留一点余量
int W, H;               // 列数、行数
char grid[MAX][MAX];    // 地图
int visited[MAX][MAX];  // 访问标记,0 未访问,1 已访问
int cnt;                // 能到达的黑色砖块总数

// 四个方向:上、下、左、右
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

// 深度优先搜索
void dfs(int x, int y) {
    int i;
    for (i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        // 边界检查
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
        // 只走黑色砖且未访问过
        if (grid[nx][ny] == '#' || visited[nx][ny]) continue;
        visited[nx][ny] = 1;
        cnt++;
        dfs(nx, ny);
    }
}

int main() {
    int i, j;
    int sx = 0, sy = 0;  // 起点坐标
    while (scanf("%d %d", &W, &H) == 2) {
        if (W == 0 && H == 0) break;  
        getchar();  // 吃掉行尾换行
        // 读地图
        for (i = 0; i < H; i++) {
            scanf("%s", grid[i]);
            for (j = 0; j < W; j++) {
                visited[i][j] = 0;
                if (grid[i][j] == '@') {
                    sx = i;
                    sy = j;
                }
            }
        }
        // 初始化
        cnt = 1;
        visited[sx][sy] = 1;
        // 从起点开始 DFS
        dfs(sx, sy);
        printf("%d\n", cnt);
    }
    return 0;
}

BFS:

c 复制代码
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

#define MAX 25

int W, H;
char g[MAX][MAX];
int vis[MAX][MAX];

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

struct Point {
    int x, y;
};

int bfs(int sx, int sy) {
    int cnt = 1;
    queue<Point> q;
    q.push({sx, sy});
    vis[sx][sy] = 1;

    while (!q.empty()) {
        Point cur = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if (g[nx][ny] == '#' || vis[nx][ny]) continue;
            vis[nx][ny] = 1;
            cnt++;
            q.push({nx, ny});
        }
    }
    return cnt;
}

int main() {
    int sx, sy;
    while (cin >> W >> H && (W || H)) {
        for (int i = 0; i < H; i++) {
            cin >> g[i];
            for (int j = 0; j < W; j++)
                if (g[i][j] == '@') { 
                    sx = i; 
                    sy = j; 
                }
        }
        // 清空 vis
        memset(vis, 0, sizeof(vis));
        cout << bfs(sx, sy) << endl;
    }
    return 0;
}
相关推荐
Tisfy1 天前
LeetCode 0865.具有所有最深节点的最小子树:深度优先搜索(一次DFS + Python5行)
算法·leetcode·深度优先·dfs·题解
闻缺陷则喜何志丹1 天前
【图论 DFS 换根法】3772. 子图的最大得分|2235
c++·算法·深度优先·力扣·图论·换根法
Tisfy3 天前
LeetCode 1339.分裂二叉树的最大乘积:深度优先搜索(一次DFS+存数组并遍历)
算法·leetcode·深度优先·题解
小龙报4 天前
【算法通关指南:数据结构与算法篇 】二叉树相关算法题:1.新二叉树 2.二叉树的遍历
c语言·数据结构·c++·人工智能·物联网·算法·深度优先
Boilermaker19926 天前
[算法基础] FooldFill(DFS、BFS)
算法·深度优先·宽度优先
平生不喜凡桃李6 天前
Leetcode-240 :搜索二维矩阵
leetcode·矩阵·深度优先
WW_千谷山4_sch7 天前
洛谷P1120&UVA307 [CERC 1995] 小木棍
c++·算法·深度优先
WW_千谷山4_sch8 天前
洛谷P8653:[模板] [蓝桥杯 2017 国 C] 分考场(染色最小色数)
c++·算法·蓝桥杯·深度优先
Swift社区8 天前
LeetCode 464 我能赢吗
算法·leetcode·深度优先
毅炼10 天前
hot100打卡——day08
java·数据结构·算法·leetcode·深度优先