[BOJ][๐ŸŸก4][๋ฐฑ์ค€#01987] ์•ŒํŒŒ๋ฒณ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1987


๋ฌธ์ œ

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


์ž…๋ ฅ

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


์ถœ๋ ฅ

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


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

2 4
CAAB
ADCB


์ถœ๋ ฅ

3


์˜ˆ์ œ 2

์ž…๋ ฅ

3 6
HFDFFB
AJHGDH
DGAGEH


์ถœ๋ ฅ

6


์˜ˆ์ œ 3

์ž…๋ ฅ

5 5
IEFCJ
FHFKC
FFALF
HFGCF
HMCHH


์ถœ๋ ฅ

10


My Sol

maxCnt = 0
I, J = map(int, input().split())
mat = [list(input()) for _ in range(I)]
visits = [[[] for _ in range(J)] for _ in range(I)]
visits[0][0].append((1, 0^(1<<(ord(mat[0][0])-65))))

cntSet = set()
Q = set()
Q.add((0, 0))

while Q:
    ni, nj = Q.pop()
    while visits[ni][nj]:
        cntNow, visit = visits[ni][nj].pop()
        needCnt = 1
        for di, dj in ((-1,0),(1,0),(0,-1),(0,1)):
            si, sj = ni+di, nj+dj
            if not (0<=si<I and 0<=sj<J): continue
            if visit&(1<<(ord(mat[si][sj])-65)): continue
            needCnt = 0
            visits[si][sj].append((cntNow+1, visit|(1<<(ord(mat[si][sj])-65))))
            Q.add((si, sj))

        if needCnt:
            maxCnt = max(maxCnt, cntNow)
print(maxCnt)

๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„ ๋ชจ๋‘ ๊ณ ๋ คํ•ด์•ผ ํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ord๋กœ ASCII๋ฌธ์ž ์ˆซ์ž๋กœ ๋ฐ”๊พผ ๋’ค, ๋น„ํŠธ๋งˆ์Šคํ‚น์„ ์‚ฌ์šฉํ•ด visit์— ์ €์žฅํ•˜์˜€๋‹ค. ์ฆ‰, A๋ผ๋ฉด 0๋ฒˆ์งธ, B๋ผ๋ฉด 1๋ฒˆ์งธ ์ฒดํฌ๋ฅผ ๋งค ์ขŒํ‘œ๋งˆ๋‹ค ๋‘์—ˆ๊ณ , ๋งค๋ฒˆ ์ˆ˜๋ฅผ ์„ธ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ํƒ์ƒ‰ ์ขŒํ‘œ๊ฐ€ visit์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด cnt๋ฅผ 1 ์ถ”๊ฐ€ํ•œ ๊ฐ’๊ณผ ํ•จ๊ป˜ visit ์ฒดํฌํ•˜์—ฌ tuple๋กœ ์ €์žฅํ•˜์˜€๋‹ค.

ํƒ์ƒ‰ ์ขŒํ‘œ์—์„œ ๋˜ ์ฃผ๋ณ€ ์ธ์ ‘ ์ขŒํ‘œ๋กœ visitํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•˜๋ฏ€๋กœ Q์— ์ง‘์–ด๋„ฃ๊ณ  Q์—์„œ ๋งค๋ฒˆ ์ขŒํ‘œ๋ฅผ ๊บผ๋‚ด์„œ ์ธ์ ‘ ์ขŒํ‘œ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๋ชจ๋“  Q์—์„œ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ๊ฐ€ ์—†๋‹ค๋ฉด ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์–ด๋–ค ๊ธฐ์ค€์ ์—์„œ ์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ visitํ•  ์ˆ˜ ์—†๋‹ค๋ฉด needCnt๊ฐ€ 0์œผ๋กœ ๊ฐฑ์‹ ๋˜์ง€ ์•Š์•„ maxCnt๋ฅผ cntNow์™€ ๋น„๊ตํ•˜์—ฌ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.


์–ด๋ ค์› ๋˜ ์ 

ํƒ์ƒ‰ ์ขŒํ‘œ๋ฅผ Q์— ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •์—์„œ ๊นŠ์ด๊ฐ€ ๊นŠ์–ด์งˆ์ˆ˜๋ก ์ค‘๋ณต๋˜๋Š” ์ขŒํ‘œ๋“ค์ด ๋งŽ์•„์กŒ๊ณ , ์ตœ์ดˆ deque๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒƒ์„ set์œผ๋กœ ๋ฐ”๊พธ์–ด add์™€ pop ํ•ด์ฃผ์—ˆ๋‹ค. deque๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด popleft๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๊ตณ์ด ์ถ”๊ฐ€๋œ ์ˆœ์„œ๋Œ€๋กœ ํ•  ํ•„์š” ์—†์ด Q๊ฐ€ ์กด์žฌํ•˜๋Š”๋™์•ˆ๋งŒ ์ง„ํ–‰ํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ค‘๋ณต์ขŒํ‘œ๋ฅผ ํš๊ธฐ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ตœ์ดˆ์—๋Š” visits์˜ ๊ฐ ์ขŒํ‘œ์— visitํ•  ์ˆ˜ ์žˆ์œผ๋ฉด visit ์ฒดํฌ ํ›„ visit๋งŒ ์ถ”๊ฐ€ํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†์„ ๋•Œ visit์˜ ๊ฐ ์ž๋ฆฌ์˜ 1์ด ์žˆ๋‹ค๋ฉด cnt๋ฅผ 1 ์˜ฌ๋ฆฌ๊ณ  toggle ํ•˜๋ฉฐ ๋น„ํŠธ๋ฐฉ์‹์œผ๋กœ cnt๋ฅผ ์„ผ ๋’ค์—, visit์ด 0์ด ๋˜๋ฉด cnt๋ฅผ maxCnt์™€ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ visit์ด ๋งŽ์•„์ง€๋ฉด์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ์˜ ์ฃผ๋ฒ”์ด ๋˜์—ˆ๊ณ , tuple ๋ฐฉ์‹์œผ๋กœ cnt๋ฅผ ํ•œ ๋ฒˆ์— maxCnt์™€ ๋น„๊ตํ•˜์—ฌ ๊ฐฑ์‹ ํ•˜๋„๋ก ํ•˜์˜€๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys

read = sys.stdin.readline
R, C = map(int, read().split())
graph = [read().strip() for _ in range(R)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]


def bfs(a, b):
    q = {(a, b, graph[a][b])}
    check = [['' for _ in range(C)] for _ in range(R)]
    check[a][b] = graph[a][b]
    result = 1
    while q:
        x, y, track = q.pop()
        result = max(result, len(track))
        if result == 26:
            break
        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            # ์ขŒํ‘œ nx, ny์„ ๊ธฐ์ค€์œผ๋กœ (ํ˜„์žฌ ์œ„์น˜์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฒฐ๊ณผ ์™€ ์ด์ „ ๊ฒฐ๊ณผ + graph ํ˜„์žฌ ์œ„์น˜) ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด
            # check nx, ny์— ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•œ๋‹ค.
            if 0 <= nx < R and 0 <= ny < C and graph[nx][ny] not in track and check[nx][ny] != track + graph[nx][ny]:
                check[nx][ny] = track + graph[nx][ny]
                q.add((nx, ny, check[nx][ny]))
    return result


print(bfs(0, 0))

์—ฐ์‚ฐ์‹œ๊ฐ„์ด ํš๊ธฐ์ ์œผ๋กœ ์งง์€ ํ’€์ด๋ฅผ ๋ฐœ๊ฒฌํ•˜์—ฌ ๋ถ„์„ํ•œ๋‹ค. ์—ญ์‹œ q๋Š” set์„ ์ด์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๋ถ™์—ฌ๊ฐ€๋ฉฐ ๋ถ™์—ฌ์˜จ ๊ฒฝ๋กœ ๋ฌธ์ž์—ด ๋‚ด์— ํƒ์ƒ‰ ์ขŒํ‘œ์˜ ๋ฌธ์ž์—ด์ด ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ not in์„ ์ด์šฉํ•ด ํ™•์ธํ•ด์ฃผ์—ˆ๋‹ค.

์ธ์ƒ์ ์ธ ๋ถ€๋ถ„์€ ํƒ์ƒ‰ ์ขŒํ‘œ์— visitํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ขŒํ‘œ์˜ ๊ฐ’๊นŒ์ง€ ๊ฒฝ๋กœ์— ๋ถ™์—ฌ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š”๋ฐ, ์ด๋ฏธ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋กœ ํ•ด๋‹น ์ขŒํ‘œ์— ์ €์žฅ์ด ๋˜์–ด์žˆ๋˜ ๊ฒฝ์šฐ ๊ฒฝ๋กœ๊ฐ€ ๋งค๋ฒˆ ๋‹ค๋ฅธ๋ฐ check์— ๋ฎ์–ด์”Œ์›Œ๋„ ๋˜๋Š”๊ฐ€ ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ