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

相关推荐
数据大魔方4 分钟前
【期货量化实战】日内动量策略:顺势而为的短线交易法(Python源码)
开发语言·数据库·python·mysql·算法·github·程序员创富
POLITE34 分钟前
Leetcode 23. 合并 K 个升序链表 (Day 12)
算法·leetcode·链表
楚来客23 分钟前
AI基础概念之八:Transformer算法通俗解析
人工智能·算法·transformer
Echo_NGC22371 小时前
【神经视频编解码NVC】传统神经视频编解码完全指南:从零读懂 AI 视频压缩的基石
人工智能·深度学习·算法·机器学习·视频编解码
会员果汁1 小时前
leetcode-动态规划-买卖股票
算法·leetcode·动态规划
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——二进制求和
算法·leetcode·哈希算法·散列表·结构与算法
地平线开发者3 小时前
征程 6 | cgroup sample
算法·自动驾驶
姓蔡小朋友4 小时前
算法-滑动窗口
算法
君义_noip4 小时前
信息学奥赛一本通 2134:【25CSPS提高组】道路修复 | 洛谷 P14362 [CSP-S 2025] 道路修复
c++·算法·图论·信息学奥赛·csp-s