Acwing 1111. 字母

https://www.acwing.com/problem/content/1113/

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const LL MAXN=1e18,MINN=-MAXN,INF=0x3f3f3f3f;
const LL N=200200,M=2020;
LL n,m,maxn=1;
char c[M][M];
map<char,LL> mp;
int dx[]={-1,0,0,1},dy[]={0,1,-1,0};
void dfs(LL x,LL y,LL sum)
{
    maxn=max(maxn,sum);
    mp[c[x][y]]++;
    for(int i=0;i<4;i++)
    {
        int xx=dx[i]+x,yy=dy[i]+y;
        if(xx<1||xx>n||yy<1||yy>m||mp[c[xx][yy]]+1>=2)
            continue;
        mp[c[xx][yy]]=1;
        dfs(xx,yy,sum+1);
        mp[c[xx][yy]]=0;
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>c[i][j];
            }
        }
        dfs(1,1,1);
        cout<<maxn<<endl;
    }
    return 0;
}
posted @ 2024-03-29 11:50  Vijurria  阅读(7)  评论(0编辑  收藏  举报