๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

1987๋ฒˆ: ์•ŒํŒŒ๋ฒณ

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

www.acmicpc.net

 

๐Ÿง‘‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

DFS + ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ๊ฐ™์€ ๋ฌธ์ž ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์œ„ํ•ด set์„ ์ด์šฉํ–ˆ๋Š”๋ฐ, ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”. ๊ทธ๋ž˜์„œ ์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์•ŒํŒŒ๋ฒณ์€ 26๊ฐœ๊ฐ€ ์žˆ๊ณ  ๊ฐ๊ฐ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ, vector<bool> Alphabet(26, false) ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ด€๋ฆฌํ•˜๋‹ˆ ํ†ต๊ณผ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

\(\mathrm{O(1)} \) ๊ณผ \( \mathrm{O(log \ N)} \) ์˜ ํƒ์ƒ‰ ๋น„์šฉ ์ฐจ์ด๋ฅผ ์‹ค๊ฐํ–ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

์•„๋ฌดํŠผ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌ์„ฑํ•ด์„œ ํ’€๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

  1. ๋ฌธ์ž์—ด ๋ณด๋“œํŒ ์ •๋ณด๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„, ํฌ๊ธฐ 26์˜ bool ๋ฐฐ์—ด(Alphabet)์„ ์„ ์–ธํ•ด ๋ชจ๋‘ false๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  2. ์‹œ์ž‘์  ๋˜ํ•œ ๊ธธ์ด์— ํฌํ•จ๋˜๋ฏ€๋กœ, Alphabet[0][0] = true ๋ฅผ ํ•˜๊ณ , ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ DFS ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.
    • ๋งต ๋ฐ–์„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธํ–ˆ๋˜ ์•ŒํŒŒ๋ฒณ์ธ ๊ฒฝ์šฐ, ์›€์ง์ธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ  ๋‹ค์Œ ๋ฐฉํ–ฅ์œผ๋กœ DFS ํƒ์ƒ‰ ์‹œ์ž‘
    • (1)์ฒ˜์Œ ๋ฐฉ๋ฌธํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์ธ ๊ฒฝ์šฐ์—๋Š” ๋ฐฉ๋ฌธ ์ฒดํฌ ํ•ด์ฃผ๊ณ , (2)ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ ์ขŒํ‘œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰์„ ์‹œ์ž‘
    • (2) ํƒ์ƒ‰์ด ๋๋‚ฌ๋‹ค๋ฉด, (1) ์•ŒํŒŒ๋ฒณ์€ ๋ฐฉ๋ฌธ ํ•ด์ œ ํ‘œ์‹œ(๋ฐฑํŠธ๋ž˜ํ‚น)

 

โœ๏ธ ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>

using namespace std;
using Direction = pair<int, int>;

#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define ALPHABET_COUNT 26

vector<Direction> Directions { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
vector<bool> Alphabet(ALPHABET_COUNT, false);
int R, C;

void BackTracking(const vector<vector<char>>& field, int& maxStep, int currentStep, int row, int col) {

    for (const auto& direction : Directions) {
        int nextRow = row + direction.first;
        int nextCol = col + direction.second;

        if (nextRow < 0 or nextRow >= R or nextCol < 0 or nextCol >= C) {
            maxStep = max(maxStep, currentStep);
            continue;
        }

        int index = field[nextRow][nextCol] - 'A';
        if (Alphabet[index]) {
            maxStep = max(maxStep, currentStep);
            continue;
        }

        Alphabet[index] = true;
        BackTracking(field, maxStep, currentStep + 1, nextRow, nextCol);
        Alphabet[index] = false;
    }
}

int GetMaximumAlphabet(const vector<vector<char>>& field) {
    int maxStep = 1;
    int index = field[0][0] - 'A';
    
    Alphabet[index] = true;
    BackTracking(field, maxStep, 1, 0, 0);
    return maxStep;
}

int main() {
    FAST_IO

    cin >> R >> C;
    vector<vector<char>> field(R, vector<char>(C));
    for (int r = 0; r < R; r++)
        for (int c = 0; c < C; c++)
            cin >> field[r][c];
    
    int answer = GetMaximumAlphabet(field);
    cout << answer;

    return 0;
}

 

 

 

728x90
๋ฐ˜์‘ํ˜•