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刷题记录

相关推荐
星空露珠21 分钟前
速算24点检测生成核心lua
开发语言·数据库·算法·游戏·lua
happygrilclh41 分钟前
高压高频电源的pid算法
算法
山峰哥1 小时前
SQL优化全解析:从索引策略到查询性能飞跃
大数据·数据库·sql·编辑器·深度优先
格林威1 小时前
Baumer相机铸件气孔与缩松识别:提升铸造良品率的 6 个核心算法,附 OpenCV+Halcon 实战代码!
人工智能·opencv·算法·安全·计算机视觉·堡盟相机·baumer相机
葫三生1 小时前
存在之思:三生原理与现象学对话可能?
数据库·人工智能·神经网络·算法·区块链
Evand J1 小时前
【MATLAB例程】无人机三维路径规划|A*,RRT(快速随机树算法), APF(人工势场法)算法对比|可自定义起终点、障碍物坐标。附下载链接
算法·matlab·无人机·astar·路径规划·rrt·apf
少许极端1 小时前
算法奇妙屋(二十七)-全排列与子集问题
算法·剪枝·回溯·递归
sali-tec1 小时前
C# 基于OpenCv的视觉工作流-章20-仿射变换
图像处理·人工智能·opencv·算法·计算机视觉
u0109272711 小时前
实时数据流处理
开发语言·c++·算法
独自破碎E1 小时前
【滑动窗口+计数】LCR015找到字符串中所有字母异位词
数据结构·算法