队列 queue

 

 

双端队列 deque

1.双端队列知识需知
由于队列是一种先进先出(FIFO)的数据结构,因此无法直接从队列的底部删除元素。如果希望从队列的底部删除元素,可以考虑使用双端队列(deque)。

双端队列(deque)是一种允许在两端插入和删除元素的数据结构。

使用 push_back() 和 push_front() 方法在双端队列的两端插入元素,

使用 pop_back() 和 pop_front() 方法在双端队列的两端删除元素。

 

下面是一个示例,演示如何使用双端队列从底部删除元素:

#include
#include
using namespace std;

int main() {
deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);

cout << d.back() << endl; // 输出 3
d.pop_back(); // 删除底部元素

cout << d.back() << endl; // 输出 2
}

 手写bfs队列

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

const int N = 21;

int A, B, C;
bool vis[N][N][N];
struct Node {
    int a, b, c;
}q[N * N * N];

int hh, tt;

void insert(int a, int b, int c) {
    if (!vis[a][b][c]) {
        q[++ tt ] = {a, b, c};
        vis[a][b][c] = true;
    }
}

void bfs() {
    q[0] = {0, 0, C};
    vis[0][0][C] = true;
    while (hh <= tt) {
        auto t = q[hh ++ ];
        int a = t.a, b = t.b, c = t.c;
        insert(a - min(a, B - b), min(a + b, B), c);
        insert(a- min(a, C - c), b, min(a + c, C));
        insert(min(a + b, A), b - min(b, A - a), c);
        insert(a, b - min(b, C- c), min(c + b, C));
        insert(min(a + c, A), b, c - min(c, A - a));
        insert(a, min(b + c, B), c - min(c, B - b));
    }
}

int main(void) {
    scanf("%d%d%d", &A, &B, &C);

    bfs();

    for (int c = 0; c <= C; c ++ ) 
        for (int b = 0; b <= B; b ++ ) 
            if(vis[0][b][c]) {
                cout << c << ' ';
                break;
            }

    return 0;
}

 

posted @ 2023-09-11 19:39  fyj!  阅读(14)  评论(0)    收藏  举报