一道入门图搜索算法题

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;
}
相关推荐
一条大祥脚17 小时前
2021-2022 ICPC Southwestern Europe Regional Contest
算法·深度优先·图论
whuhewei18 小时前
React diff算法为什么是DFS,不是BFS
算法·react.js·深度优先
Project_Observer2 天前
使用Zoho Projects AI自动项目管理
大数据·数据库·人工智能·深度学习·机器学习·深度优先
Hesionberger2 天前
LeetCode105:前序中序构建二叉树(三解法)
java·数据结构·python·算法·leetcode·深度优先
ccLianLian3 天前
图论·刷题总结
算法·深度优先·图论
木井巳4 天前
【递归算法】不同路径Ⅲ
java·算法·leetcode·深度优先
一条大祥脚5 天前
Codeforces Round 1098 (Div. 2)
算法·深度优先
人道领域5 天前
【LeetCode刷题日记】513.二叉树左下角值的三种解法:从常规BFS到DFS的优雅之旅
数据结构·算法·leetcode·深度优先·广度优先
周末也要写八哥5 天前
浅谈二叉树的深度优先搜索(DFS)算法
算法·深度优先
运筹vivo@5 天前
1306. 跳跃游戏 III — 图搜索思路拆解
游戏·深度优先·图搜索算法