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
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自www.cnblogs.com/itdef/p/13199752.html
今日推荐