티스토리 뷰

백준/실버

[백준 3085번] 사탕 게임 (C++)

게임개발기원 2023. 2. 6. 01:32

문제링크 : https://www.acmicpc.net/problem/3085

 

char arr[51][51];
int n;
int result;

int check()
{
	char c;
	for (int i = 0; i < n; i++)
	{
		int cnt = 1;
		for (int j = 0; j < n - 1; j++)
		{
			if (arr[i][j] == arr[i][j + 1])  //양옆이 같으면 증가
			{
				cnt++;
			}
			else
			{
				result = max(result, cnt);   //최댓값 갱신
				cnt = 1;                     //다를 경우 다시 카운트
			} 
			result = max(result, cnt);       //마무리 갱신
		}

	}

	for (int j = 0; j < n; j++)
	{
		int cnt = 1;
		for (int i = 0; i < n - 1; i++)
		{
			if (arr[i][j] == arr[i + 1][j])  //위아래가 같으면 증가
			{
				cnt++;
			}
			else
			{
				result = max(result, cnt);
				cnt = 1;
			}
			result = max(result, cnt);
		}
	}
	return result;
}

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> arr[i][j];
		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n - 1; j++)
		{
			swap(arr[i][j], arr[i][j + 1]);  //인접한 두 칸 고르기
			check();                         //가장 긴 연속 부분 확인
			swap(arr[i][j], arr[i][j + 1]);  //확인 후 배열 원상복귀
		}
	}

	for (int j = 0; j < n; j++)
	{
		for (int i = 0; i < n - 1; i++)
		{
			swap(arr[i][j], arr[i + 1][j]);
			check();
			swap(arr[i][j], arr[i + 1][j]);
		}
	}
	cout << result;
	return 0;
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함