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

相关推荐
Trent19851 小时前
影楼精修-肤色统一算法解析
图像处理·人工智能·算法·计算机视觉
feifeigo1231 小时前
高光谱遥感图像处理之数据分类的fcm算法
图像处理·算法·分类
北上ing2 小时前
算法练习:19.JZ29 顺时针打印矩阵
算法·leetcode·矩阵
.格子衫.3 小时前
真题卷001——算法备赛
算法
XiaoyaoCarter3 小时前
每日一道leetcode
c++·算法·leetcode·职场和发展·二分查找·深度优先·前缀树
Hygge-star4 小时前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法
June`5 小时前
专题二:二叉树的深度搜索(二叉树剪枝)
c++·算法·深度优先·剪枝
好吃的肘子6 小时前
Elasticsearch架构原理
开发语言·算法·elasticsearch·架构·jenkins
胡耀超6 小时前
霍夫圆变换全面解析(OpenCV)
人工智能·python·opencv·算法·计算机视觉·数据挖掘·数据安全
软行7 小时前
LeetCode 每日一题 3341. 到达最后一个房间的最少时间 I + II
数据结构·c++·算法·leetcode·职场和发展