ACM算法学习-第七周

第七周

题解

1. [洛谷B3625] 迷宫寻路

1-1. 题干
1-2. 解析
  • 深搜,对于上下左右四个方向,若满足在迷宫范围内且不为墙与未到达过两个条件进行递归
  • 移动
  • 递归后还原到达状态,到达位置则跳出循环
1-3. 原码
#include <iostream>
#include <utility>
#include <queue>
using namespace std;
int n, m;
queue<pair<int, int>> q;
int cx[4] = {0, 0, 1, -1}, cy[4] = {1, -1, 0, 0};
bool vis[110][110], map[110][110], result;

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char c;
            cin >> c;
            if (c == '.')
            {
                map[i][j] = true;
            }
            else if (c == '#')
            {
                map[i][j] = false;
            }
        }
    }
    q.push({1, 1});
    while (!q.empty())
    {
        pair<int, int> now = q.front();
        q.pop();
        if (now.first == n && now.second == m)
        {
            result = true;
            break;
        }
        if (vis[now.first][now.second])
        {
            continue;
        }
        vis[now.first][now.second] = true;
        for (int i = 0; i < 4; i++)
        {
            int nowx = now.first + cx[i];
            int nowy = now.second + cy[i];
            if (nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m)
            {
                if (map[nowx][nowy])
                {
                    q.push({nowx, nowy});
                }
            }
        }
    }
    if (result)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;
}

2. [洛谷P1706] 全排列问题

2-1. 题干
2-2. 解析
  • 深搜,对于未使用的所有数字,若满足未超过数字范围与未使用过两个条件则进行递归
  • 存储
  • 递归后还原使用状态,超出数字范围则输出并跳出循环
2-3. 原码
#include <iostream>
#include <iomanip>
#include <functional>
using namespace std;
int n, num[20];
bool vis[20];

void (*op)(int) = [](int cnt)
{
    for (int i = 1; i <= cnt; i++)
    {
        cout << setw(5) << std::right << num[i];
    }
    cout << endl;
};

void dfs(int cnt, int begin)
{
    if (cnt > n)
    {
        op(n);
        return;
    }
    for (int i = begin + 1; i <= n; i++)
    {
        if (vis[i])
        {
            continue;
        }
        vis[i] = true;
        num[cnt] = i;
        dfs(cnt + 1, 0);
        vis[i] = false;
    }
}

int main()
{
    cin >> n;
    dfs(1, 0);
    return 0;
}

3. [洛谷P1451] 求细胞数量

3-1. 题干
3-2. 解析
  • 使用函数判断一个位置八向是否有棋子
  • 深搜,对于坐标位置上下左右四个方向,若满足未超过范围、未使用过与函数三个条件则进行递归
  • 将本位置周围不为0处标记为true
  • 递归后还原使用状态,超出数字范围则输出并跳出循环
3-3. 原码
#include <iostream>
using namespace std;
int n, m, ans;
int cx[] = {0, 0, 1, -1}, cy[] = {1, -1, 0, 0};
bool vis[110][110], map[110][110];

void dfs(int x, int y)
{
    if (vis[x][y])
    {
        return;
    }
    vis[x][y] = true;
    if (!map[x][y])
    {
        return;
    }
    for (int i = 0; i < 4; i++)
    {
        int nowx = x + cx[i];
        int nowy = y + cy[i];
        if (nowx >= 1 && nowx <= n && nowy >= 1 && nowy <= m)
        {
            if (map[nowx][nowy])
            {
                dfs(nowx, nowy);
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char c;
            cin >> c;
            if (c == '0')
            {
                map[i][j] = false;
            }
            else
            {
                map[i][j] = true;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (map[i][j] && !vis[i][j])
            {
                ans++;
            }
            dfs(i, j);
        }
    }
    cout << ans << endl;
    return 0;
}

4. [洛谷P1219] [USACO1.5] 八皇后 Checker Challenge

4-1. 题干
4-2. 解析
  • 深搜,对于坐标位置上下左右四个方向,若满足未超过范围、未使用过与不为0三个条件则进行递归
  • 记录本位置到数组
  • 递归后还原使用状态,递归开始时计数器+1
4-3. 原码
#include <iostream>
using namespace std;
int n, num[20], ans;
bool vis[20], vis2[20][20], map[20][20];

void (*op)(int) = [](int cnt)
{
    cout << num[1];
    for (int i = 2; i <= cnt; i++)
    {
        cout << " " << num[i];
    }
    cout << endl;
};

bool check(int x, int y)
{
    for (int i = 1; i <= n; i++)
    {
        if (vis2[x][i] || vis2[i][y])
        {
            return false;
        }
        int newy1 = y - x + i;
        if (newy1 >= 1 && newy1 <= n)
        {
            if (vis2[i][newy1])
            {

                return false;
            }
        }
        int newy2 = y + x - i;
        if (newy2 >= 1 && newy2 <= n)
        {
            if (vis2[i][newy2])
            {

                return false;
            }
        }
    }
    return true;
}

void dsf(int cnt, int begin)
{
    if (cnt > n)
    {
        ans++;
        if (ans <= 3)
        {
            op(n);
        }
        return;
    }
    for (int i = begin + 1; i <= n; i++)
    {
        if (vis[i])
        {
            continue;
        }
        if (check(cnt, i))
        {

            vis[i] = true;
            vis2[cnt][i] = true;
            num[cnt] = i;
            dsf(cnt + 1, 0);
            vis[i] = false;
            vis2[cnt][i] = false;
        }
    }
}

int main()
{
    cin >> n;
    dsf(1, 0);
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值