acwing kuangbin专题搜索专题(1-5)

1.acwing 1114.棋盘问题
传送门

  • 题目大意:给你一个n * n的矩阵,其中里面包含两种字符’#‘和’.’,’#‘表示是棋盘,即可以放棋子,‘.’表示是空白区域不可以放棋子,现在给你k个棋子,要求每一行,每一列上不能有两颗棋子,问棋子有多少种摆放的可能
  • 思路:直接使用dfs进行爆搜即可
  • 代码如下
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 10;
char g[N][N];
bool line[N];//因为每次直接处理下一行,所以只需要标记列即可
int n, k, m;
int res;
void dfs(int x)//按行进行摆放然后爆搜
{
    if (m == k) 
    {
        res ++;
        return ;
    }
    else if (x == n) return ;
    for (int i = 0; i < n; i ++)
        if (!line[i] && g[x][i] == '#')
        {
            line[i] = true;//进行摆放
            m ++;
            dfs(x + 1);
            m --;
            line[i] = false;//恢复现场
        }
    dfs(x + 1);//此行也可以不放棋子
}
int main()
{
    while (scanf("%d%d", &n, &k) || ( n != -1 && k != -1))
    {
        for (int i = 0; i < n; i ++) cin >> g[i];
        res = 0;
        m = 0;
        memset(line, false, sizeof(line));
        dfs(0);
        printf("%d\n", res);
    }
    return 0;
}

2.地牢大师
题目链接

  1. 思路:这是每次移动权重都为1的图,要求算出最短路,使用bfs即可,需要注意的是,这个图是三维图形,需要一个三维数组来存储,同时因为终点为’E’,判断是否可以走的条件就应该是用’#'来判断,这里我debug了好久,其它都是模板。
  2. ac代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int dx[6] = {-1, 1, 0, 0, 0, 0}, dy[6] = {0, 0, -1, 1, 0, 0}, dz[6] = {0, 0, 0, 0, -1, 1};
const int N = 110;
char g[N][N][N];
int res[N][N][N];
int l, r, c;
struct node
{
    int z, x, y;
}st, ed;
void bfs()
{
    queue<node> q;
    q.push(st);
    res[st.z][st.x][st.y] = 0;
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        for (int i = 0; i < 6; i ++)
        {
            int z = t.z + dz[i], y = t.y + dy[i], x = t.x + dx[i];
            if (x >= 0 && x < r && y >= 0 && y < c && z >= 0 && z < l && res[z][x][y] == -1 && g[z][x][y] != '#')
            {
                q.push({z, x, y});
                res[z][x][y] = res[t.z][t.x][t.y] + 1;
                if (z == ed.z && y == ed.y && x == ed.x) return ;
            }
        }
    }
}
int main()
{
    while (scanf("%d%d%d", &l, &r, &c) && l)
    {
        memset(res, -1, sizeof(res));
        for (int i = 0; i < l; i ++)
            for (int j = 0; j < r; j ++)
            {
                for (int k = 0; k < c; k ++)
                {
                    cin >> g[i][j][k];
                    if (g[i][j][k] == 'S') st = {i, j, k};
                    else if(g[i][j][k] == 'E') ed = {i, j, k};
                }
                while (getchar() == ' ') ;
            }
        bfs();
        int ans = res[ed.z][ed.x][ed.y];
        if (ans == -1) printf("Trapped!\n");
        else printf("Escaped in %d minute(s).\n", ans);
    }
    return 0;
}

3.抓住那头牛
题目链接

  • 简单的bfs,代码如下
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int res[N];
int n, k;
int bfs()
{
    queue<int> q;
    q.push(n);
    res[n] = 0;
    while (q.size())
    {
        int t = q.front();
        q.pop();
        int a = 2 * t, b = t + 1, c = t - 1;
        if (a >= 0 && a <= 1e5 && res[a] == -1)
        {
            q.push(a);
            res[a] = res[t] + 1;
        }
        if(b >= 0 && b <= 1e5 && res[b] == -1)
        {
            q.push(b);
            res[b] = res[t] + 1;
        }
        if(c >= 0 && c <= 1e5 && res[c] == -1)
        {
            q.push(c);
            res[c] = res[t] + 1;
        }
        //cout << res[t] << endl;
        if (res[k] != -1) break;
    }
    return res[k];
}
int main()
{
    scanf("%d%d", &n, &k);
    memset(res, -1, sizeof(res));
    cout << bfs() << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin带你飞」专题五并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BlueSkyπ_π

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值