从头开始学算法:考研机试题练习(C/C++)–搜索

从头开始学算法:考研机试题练习(C/C++)–搜索

最近重学C语言,刷的是胡凡写的《算法笔记》,这本书的题主要是面向考研机试和一般算法考试的,零基础入门,还不错,在此记录学习过程。

本文主要记录一些涉及简单深度优先和广度优先的搜索算法

#include<stdio.h>
#include<queue>
using namespace std;


void recussive_DFS(int n, int index,int cost[], int volume[], int sum_c, int sum_v, int *max_c, int total_v)
{
    //边界条件
    if (index == n) {

        return;
    }
    //递归体
    //不选择该项
    recussive_DFS(n, index + 1, cost, volume, sum_c, sum_v, max_c, total_v);
    //把剪枝要求放在岔道口
    //选择该项
    if (sum_v <= total_v) {
        if (sum_c>*max_c) {
            *max_c = sum_c;
        }
        recussive_DFS(n, index + 1, cost, volume, sum_c + cost[index + 1], sum_v + volume[index + 1], max_c, total_v);

    }

}

void DFS_fun()
{
    int cost[20], volume[20], n, total_volume, sum_c=0, sum_v=0, max_c=0;
    scanf("%d %d", &n, &total_volume);
    for (int i = 0; i < n; i++){
        scanf("%d %d", &cost[i], &volume[i]);
    }
    recussive_DFS(n, 0, cost, volume, sum_c, sum_v, &max_c, total_volume);

    printf("%d", max_c);

}

bool state_chose[20][20] = { false };
int matrix[20][20];
bool can_choose(int y, int x, int row, int col)
{
    if (matrix[y][x] != 1)
        return false;
    if (x < 0 || x >= col || y < 0 || y >= row)
        return false;
    if (state_chose[y][x])
        return false;
    return true;
}

struct my_coodinate {
    int x;
    int y;
    my_coodinate(int y1, int x1) {
        x = x1;
        y = y1;
    }
};



void BFS_fun(int y, int x, int row, int col)
{
    queue<my_coodinate> now_q;
    int x_add[4] = { 0,0,1,-1 };
    int y_add[4] = { 1,-1,0,0 };
    for (int i = 0; i < 4; i++) {
        if (can_choose(y + y_add[i], x + x_add[i], row, col))
        {
            state_chose[y + y_add[i]][x + x_add[i]] = true;
            now_q.push(my_coodinate(y + y_add[i], x + x_add[i]));

        }

    }
    while (!now_q.empty()){
        int now_x, now_y;
        now_y = now_q.front().y;
        now_x = now_q.front().x;
        now_q.pop();
        for (int i = 0; i < 4; i++) {
            if (can_choose(now_y + y_add[i], now_x + x_add[i], row, col))
            {
                now_q.push(my_coodinate(now_y + y_add[i], now_x + x_add[i]));
                state_chose[now_y][now_x] = true;
            }
        }
    }


}
void matrix_block()
{
    int col, row, block_num=0;
    scanf("%d %d", &row, &col);
    for (int i = 0; i < row; i++){
        for (int j = 0; j < col; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            if (can_choose(i, j, row, col)) {
                state_chose[i][j] = true;
                block_num++;
                BFS_fun(i, j, row, col);
            }
        }
    }

    printf("%d", block_num);

}

int main()
{
    //DFS_fun();
    matrix_block();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值