AcWing 1111. 字母 解题思路及代码

先贴个题目:

简单的dfs,没啥难点,直接上代码。

cpp 复制代码
#include<iostream>
#include<cmath>
using namespace std;

const int N = 30;

int r, s;
int ans = 0;
char map[N][N];
bool st[26];
int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};

void dfs(int x,int y,int cnt)
{
    bool sign = false;
    st[map[x][y] - 'A'] = true;
    for (int i = 0; i < 4;++i)
    {
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx<0||tx>=r||ty<0||ty>=s)
        {
            continue;
        }
        if(!st[map[tx][ty]-'A'])
        {
            sign = true;
            dfs(tx, ty, cnt + 1);
        }
    }
    st[map[x][y] - 'A'] = false;
    if(!sign)
    {
        ans = max(ans, cnt);
        return;
    }
}

int main()
{
    cin >> r >> s;
    for (int i = 0; i < r;++i)
        scanf("%s", map[i]);
    st[map[0][0]-'A']=true;
    dfs(0, 0, 1);
    cout << ans;
}

by------------2024.4.11刷题记录

相关推荐
下一站春风里2 小时前
位运算完整学习笔记:按位与(&)+ 异或(^)
开发语言·c++·算法
DRobot2 小时前
【机械制图与CAD实战(二十五)】螺纹的表示方法
算法·机械制图
用户老李2 小时前
卡尔曼滤波自学教程 10:无迹卡尔曼滤波(UKF)
算法
深圳市益普科技有限公司4 小时前
半导体良率管理YMS:良率与设备参数的相关性,先拉哪几组数据
人工智能·python·算法
程序员清风5 小时前
算法题中的边界条件、测试与调试
开发语言·算法·kotlin
炘爚5 小时前
数据结构算法总结
数据结构·python·算法
Dageno_AI5 小时前
ChatGPT 不再引用 Reddit 之后,还值得在 Reddit 上做 AI 可见度吗?
算法
hetao17338375 小时前
2026-10-25 hetao1733837 的刷题记录
c++·算法
Jsundoku6 小时前
第七章 机器学习 -- 其他经典算法
人工智能·算法·机器学习
进击的丸子6 小时前
基于 ArcFace SDK 的跨平台人脸特征值下发、同步以及存储方案
后端·算法·代码规范