πŸ™‡β€β™€οΈ[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;
}

νƒœκ·Έ:

μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ: