站外题,玄3关(求助!)
  • 板块灌水区
  • 楼主Wendy_Hello_qwq
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/5/14 21:01
  • 上次更新2024/5/15 08:12:04
查看原帖
站外题,玄3关(求助!)
1039211
Wendy_Hello_qwq楼主2024/5/14 21:01

原题戳这里

代码!

#include <cstdio>
#include <iostream>
using namespace std;
int r, s, ans = 1, max1;
char a[27][27];
int b[207], dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void dfs (int, int, int);
int main()
{
	scanf ("%d%d", &r, &s);
	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= s; j++)
			cin >> a[i][j];
	}
	b[a[1][1]]++;
	dfs (1, 1, 1);
	printf ("%d", max1);
	return 0;
}
void dfs (int x, int y, int step)
{
	if (b[a[x][y]] > 0 && step > 1)
	{
		if (ans > max1)
			max1 = ans;
		return;
	}
	else
	{
		for (int i = 0; i <= 3; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx >= 1 && xx <= r && yy >= 1 && yy <= s && b[a[xx][yy]] <= 0)
			{
				ans++;
				b[a[xx][yy]] ++;
				dfs (xx, yy, step + 1);
				b[a[xx][yy]] --;
			}
		}
	}
}
2024/5/14 21:01
加载中...