AcWing 1111. 字母 bfs dfs两份代码

地址 https://www.acwing.com/problem/content/description/1113/

复制代码
给定一个 R×S 的大写字母矩阵,你的起始位置位于左上角,你可以向上下左右四个方向进行移动,但是不能移出边界,或者移动到曾经经过的字母(左上角字母看作第一个经过的字母)。

请问,你最多可以经过几个字母。

输入格式
第一行包含两个整数 R 和 S,表示字母矩阵的行和列。

接下来 R 行,每行包含一个长度为 S 的大写字母构成的字符串,共同构成字母矩阵。

输出格式
输出一个整数,表示最多能够经过的字母个数。

数据范围
1≤R,S≤20
输入样例:
3 6
HFDFFB
AJHGDH
DGAGEH
输出样例:
6
复制代码

解答

算法1
搜索 裸题
蛮简单的
但是用BFS比较麻烦
DFS 天然的是深度搜索后折返,可以很方便的在函数进入和退出的时候将状态恢复。
如果用BFS 他不是线性而是发散的 要用额外的空间存储当前的状态(多少字母经过) 空间不划算,在不要求最小最快到达终点的话 用BFS不划算 也麻烦

结果: Accepted 通过了 10/10个数据 运行时间: 34 ms 运行空间: 728 KB 提交时间: 7分钟前

C++ 代码

复制代码
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <memory.h>
#include <algorithm>

using namespace std;

int n, m;
const int N = 50;

vector<string> V;

int mp[26];

/*
输入样例:
3 6
HFDFFB
AJHGDH
DGAGEH
输出样例:
6
*/

struct STATE {
    int x; int y;
    int mp[26];
    int step;
};

int ans = -999999;

int addx[] = { 1,-1,0,0 };
int addy[] = { 0,0,-1,1 };

void Bfs()
{
    queue<struct STATE> Q;
    struct STATE s;
    memset(s.mp, 0, sizeof s.mp);
    //标记第一个字母已经 经过了
    s.mp[V[0][0] - 'A'] = 1;
    s.step = 1; s.x = 0; s.y = 0;

    Q.push(s);


    while (Q.size()) {
        struct STATE t = Q.front(); Q.pop();
        ans = max(ans, t.step);
        for (int i = 0; i < 4; i++) {
            int newx = t.x + addx[i];
            int newy = t.y + addy[i];

            if (newx >= 0 && newx < n && newy >= 0 && newy < m) {
                int idx = V[newx][newy] - 'A';
                if (t.mp[idx] == 1) continue;
                //入队
                struct STATE next;
                next.x = newx; next.y = newy;
                next.step = t.step + 1;
                memcpy(next.mp, t.mp, sizeof t.mp);
                next.mp[idx] = 1;

                Q.push(next);
            }
        }

    }
}


int main()
{
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        V.push_back(s);
    }

    Bfs();
    cout << ans;

    return 0;
}

作者:itdef
链接:https://www.acwing.com/solution/content/15385/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
复制代码

算法2
DFS
结果: Accepted 通过了 10/10个数据 运行时间: 21 ms 运行空间: 216 KB 提交时间: 23分钟前

C++ 代码

复制代码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
const int N = 50;

vector<string> V;

int mp[26];

/*
输入样例:
3 6
HFDFFB
AJHGDH
DGAGEH
输出样例:
6
*/

int addx[] = { 1,-1,0,0 };
int addy[] = { 0,0,-1,1 };

int ans = -999999;

void Dfs(int x, int y, int step)
{
    int idx = V[0][0] - 'A';
    mp[idx] = 1;

    for (int i = 0; i < 4; i++) {
        int newx = x + addx[i];
        int newy = y + addy[i];

        if (newx >= 0 && newx < n && newy >= 0 && newy < m) {
            int idx = V[newx][newy] - 'A';
            if (mp[idx] == 1) continue;
            mp[idx] = 1;
            Dfs(newx, newy, step+1);
            mp[idx] = 0;
        }
    }

    ans = max(ans, step);

    return;
}

int main()
{
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        V.push_back(s);
    }
    Dfs(0,0,1);
    cout << ans << endl;

    return 0;
}

作者:itdef
链接:https://www.acwing.com/solution/content/15385/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
复制代码

 

posted on   itdef  阅读(215)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2016-06-27 boost asio 学习(八) 网络基础 二进制写发送和接收
2016-06-27 boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示