top1编程
← 返回题目
题解

LETTERS

1 条题解

  • 0
    @ 2026-7-28 22:45:20
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    char a[30][30];
    int num[30];
    int vis[30][30];
    int maxx = 0;
    int r, s;
    int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
    void dfs(int x, int y, int step)
    {
    	if (maxx < step)
    	{
    		maxx = step;
    	}
    	for (int i = 0; i < 4; i++)
    	{
    		
    		int nx = x + dir[i][0];
    		int ny = y + dir[i][1];
    		if (nx >= 0 && nx < r && ny >= 0 && ny < s && vis[nx][ny] == 0 && num[a[nx][ny] - 'A'] == 0)
    		{
    			vis[nx][ny] = 1;
    			num[a[nx][ny] - 'A'] = 1;
    			dfs(nx, ny, step + 1);
    			vis[nx][ny] = 0;
    			num[a[nx][ny] - 'A'] = 0;
    		}
    	}
    }
    int main()
    {
    	cin >> r >> s;
    	for (int i = 0; i < r; i++)
    		for (int j = 0; j < s; j++)
    			cin >> a[i][j];
    	num[a[0][0] - 'A'] = 1;
    	vis[0][0] = 1;
    	dfs(0, 0, 1);
    	cout << maxx << endl;
    	return 0;
    }
    
    • 1