๐Ÿ™‡โ€โ™€๏ธ[Gold IV] ์•ŒํŒŒ๋ฒณ - 1987

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2028 KB, ์‹œ๊ฐ„: 1624 ms

๋ถ„๋ฅ˜

๋ฐฑํŠธ๋ž˜ํ‚น, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

์ œ์ถœ ์ผ์ž

2024๋…„ 1์›” 17์ผ 10:31:40

๋ฌธ์ œ ์„ค๋ช…

์„ธ๋กœ R์นธ, ๊ฐ€๋กœ C์นธ์œผ๋กœ ๋œ ํ‘œ ๋ชจ์–‘์˜ ๋ณด๋“œ๊ฐ€ ์žˆ๋‹ค. ๋ณด๋“œ์˜ ๊ฐ ์นธ์—๋Š” ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์ด ํ•˜๋‚˜์”ฉ ์ ํ˜€ ์žˆ๊ณ , ์ขŒ์ธก ์ƒ๋‹จ ์นธ (1ํ–‰ 1์—ด) ์—๋Š” ๋ง์ด ๋†“์—ฌ ์žˆ๋‹ค.

๋ง์€ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ธ์ ‘ํ•œ ๋„ค ์นธ ์ค‘์˜ ํ•œ ์นธ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ƒˆ๋กœ ์ด๋™ํ•œ ์นธ์— ์ ํ˜€ ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ์€ ์ง€๊ธˆ๊นŒ์ง€ ์ง€๋‚˜์˜จ ๋ชจ๋“  ์นธ์— ์ ํ˜€ ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ๊ณผ๋Š” ๋‹ฌ๋ผ์•ผ ํ•œ๋‹ค. ์ฆ‰, ๊ฐ™์€ ์•ŒํŒŒ๋ฒณ์ด ์ ํžŒ ์นธ์„ ๋‘ ๋ฒˆ ์ง€๋‚  ์ˆ˜ ์—†๋‹ค.

์ขŒ์ธก ์ƒ๋‹จ์—์„œ ์‹œ์ž‘ํ•ด์„œ, ๋ง์ด ์ตœ๋Œ€ํ•œ ๋ช‡ ์นธ์„ ์ง€๋‚  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋ง์ด ์ง€๋‚˜๋Š” ์นธ์€ ์ขŒ์ธก ์ƒ๋‹จ์˜ ์นธ๋„ ํฌํ•จ๋œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— R๊ณผ C๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1โ‰คR,Cโ‰ค20) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ R๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋ณด๋“œ์— ์ ํ˜€ ์žˆ๋Š” C๊ฐœ์˜ ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ๋“ค์ด ๋นˆ์นธ ์—†์ด ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ง์ด ์ง€๋‚  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€์˜ ์นธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

DFS๋ฌธ์ œ๋กœ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ•ด์•ผํ•œ๋‹ค.

์ฒ˜์Œ์— BFS๋กœ ์ ‘๊ทผํ–ˆ๋‹ค๊ฐ€ ์•ˆํ’€๋ ค์„œ ๋‹ค์‹œ ํ–ˆ๋‹ค.

char๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•  ์•ŒํŒŒ๋ฒณ์ด ๊ธฐ์กด์— ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉด์„œ DFS๋ฅผ ๋Œ๋ ค์•ผํ•œ๋‹ค.

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
int seqSize = 1;
void DFS(pair<int, int> here)
{
	ch[seqSize++] = board[here.first][here.second];
	res = max(res, seqSize);

    // ๊ธฐ๋ก๋œ ch ํ™•์ธ์šฉ
	//cout << endl;
	//cout << "ํ˜„์žฌ ch ์ƒํƒœ " << endl;
	//for (int i = 1; i <= seqSize; ++i)
	//{
	//	cout << ch[i] << " ";
	//}
	//cout << endl;

	for (int i = 0; i < 4; ++i)
	{
		int ny = here.first + dy[i];
		int nx = here.second + dx[i];

		if (ny < 1 || nx < 1 || ny > r || nx > c)
			continue;
		if (visited[ny][nx] == 1)
			continue;
		bool flag = false;
		for (int j = 1; j <= seqSize; ++j)
		{
			if (ch[j] == board[ny][nx])
			{
				flag = true;
				break;
			}
		}
		if (flag == true)
			continue;

		visited[ny][nx] = 1;
		DFS({ ny, nx });
		visited[ny][nx] = 0;
		ch[--seqSize] = ' ';
	}
}

์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€ ์ž…๋ ฅ์ด ๋“ค์–ด์˜ค๋ฉด

2 4
CAAB
ADCB

์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ธ๋‹ค.
image

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int r, c, res = 0;
char ch[500];
vector<vector<char>> board;
vector<vector<int>> visited;
void setting()
{
	cin >> r >> c;
	board = vector<vector<char>>(r + 1, vector<char>(c + 1));
	visited = vector<vector<int>>(r + 1, vector<int>(c + 1));

	string str;
	for (int i = 1; i <= r; ++i)
	{
		cin >> str;
		for (int j = 0; j < str.size(); ++j)
		{
			board[i][j + 1] = str[j];
		}
	}
}

int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
int seqSize = 1;
void DFS(pair<int, int> here)
{
	ch[seqSize++] = board[here.first][here.second];
	res = max(res, seqSize);

	//cout << endl;
	//cout << "ํ˜„์žฌ ch ์ƒํƒœ " << endl;
	//for (int i = 1; i <= seqSize; ++i)
	//{
	//	cout << ch[i] << " ";
	//}
	//cout << endl;

	for (int i = 0; i < 4; ++i)
	{
		int ny = here.first + dy[i];
		int nx = here.second + dx[i];

		if (ny < 1 || nx < 1 || ny > r || nx > c)
			continue;
		if (visited[ny][nx] == 1)
			continue;
		bool flag = false;
		for (int j = 1; j <= seqSize; ++j)
		{
			if (ch[j] == board[ny][nx])
			{
				flag = true;
				break;
			}
		}
		if (flag == true)
			continue;

		visited[ny][nx] = 1;
		DFS({ ny, nx });
		visited[ny][nx] = 0;
		ch[--seqSize] = ' ';
	}
}

void solve()
{
	DFS({ 1, 1 });
	cout << res - 1;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	setting();
	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: