搜索与回溯算法C/C++实现

目录

搜索算法简介

回溯算法简介

C/C++实现示例

深度优先搜索(DFS)实现

广度优先搜索(BFS)实现

回溯算法实现 - N皇后问题


搜索与回溯算法是计算机科学中的重要概念,它们在解决许多问题中扮演着关键角色,比如路径寻找、游戏玩法、组合问题等。下面我将简要介绍搜索与回溯算法的基本概念,然后提供C/C++的实现示例。

搜索算法简介

搜索算法用于在数据结构中查找特定元素或满足特定条件的元素。常见的搜索算法有深度优先搜索(DFS)和广度优先搜索(BFS)。

-深度优先搜索(DFS):从根节点开始,尽可能深地搜索树的分支。当节点v的子节点都已被访问过,回退到发现节点v的那条边的起始节点。
- 广度优先搜索(BFS):从根节点开始,逐层遍历节点,即先访问所有邻接节点,再访问邻接点的邻接点。


回溯算法简介

回溯算法是一种通过试错的方式尝试分步解决问题的算法。它尝试分步去解决一个问题,如果某一步确定不会导致问题的解决,则放弃当前路径,回退到上一步,尝试另一种可能。


C/C++实现示例

深度优先搜索(DFS)实现
#include <iostream>
#include <vector>

using namespace std;

// 假设我们有一个图的邻接表表示
vector<int> graph[100];

// DFS函数
void DFS(int node, vector<bool>& visited) {
    visited[node] = true;
    cout << node << " ";

    for (int i = 0; i < graph[node].size(); i++) {
        int adjacent = graph[node][i];
        if (!visited[adjacent]) {
            DFS(adjacent, visited);
        }
    }
}

int main() {
    int n = 5; // 节点数量
    // 构建图的邻接表
    graph[0].push_back(1);
    graph[0].push_back(2);
    graph[1].push_back(2);
    graph[2].push_back(0);
    graph[2].push_back(3);
    graph[3].push_back(3);

    vector<bool> visited(n, false);
    DFS(0, visited); // 从节点0开始DFS

    return 0;
}
广度优先搜索(BFS)实现
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

vector<int> graph[100];

void BFS(int start, int n) {
    vector<bool> visited(n, false);
    queue<int> q;

    visited[start] = true;
    q.push(start);

    while (!q.empty()) {
        int node = q.front();
        q.pop();
        cout << node << " ";

        for (int i = 0; i < graph[node].size(); i++) {
            int adjacent = graph[node][i];
            if (!visited[adjacent]) {
                visited[adjacent] = true;
                q.push(adjacent);
            }
        }
    }
}

int main() {
    int n = 5;
    // 构建图的邻接表,与DFS示例相同

    BFS(0, n); // 从节点0开始BFS

    return 0;
}
回溯算法实现 - N皇后问题
#include <iostream>
#include <vector>

using namespace std;

bool isSafe(int row, int col, vector<vector<bool>>& board) {
    for (int i = 0; i < row; i++) {
        if (board[i][col]) return false;
    }
    for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
        if (board[i][j]) return false;
    }
    for (int i = row, j = col; j >= 0 && i < board.size(); i++, j--) {
        if (board[i][j]) return false;
    }
    return true;
}

bool solveNQueensUtil(int row, int n, vector<vector<bool>>& board) {
    if (row == n) return true;

    for (int col = 0; col < n; col++) {
        if (isSafe(row, col, board)) {
            board[row][col] = true;
            if (solveNQueensUtil(row + 1, n, board)) return true;
            board[row][col] = false; // 回溯
        }
    }
    return false;
}

void solveNQueens() {
    int n = 4; // 皇后的数量
    vector<vector<bool>> board(n, vector<bool>(n, false));
    if (!solveNQueensUtil(0, n, board)) {
        cout << "No solution exists";
        return;
    }

    for (auto &row : board) {
        for (bool cell : row) {
            cout << cell << " ";
        }
        cout << endl;
    }
}

int main() {
    solveNQueens();
    return 0;
}

这些示例提供了搜索和回溯算法的基本实现,你可以根据自己的需求进行修改和扩展。搜索算法和回溯算法在实际应用中非常灵活,可以解决各种复杂的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值