[BOJ][๐ŸŸก5][๋ฐฑ์ค€#21608] ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #21608


๋ฌธ์ œ

์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต์—๋Š” ๊ต์‹ค์ด ํ•˜๋‚˜ ์žˆ๊ณ , ๊ต์‹ค์€ Nร—N ํฌ๊ธฐ์˜ ๊ฒฉ์ž๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ํ•™๊ต์— ๋‹ค๋‹ˆ๋Š” ํ•™์ƒ์˜ ์ˆ˜๋Š” N2๋ช…์ด๋‹ค. ์˜ค๋Š˜์€ ๋ชจ๋“  ํ•™์ƒ์˜ ์ž๋ฆฌ๋ฅผ ์ •ํ•˜๋Š” ๋‚ ์ด๋‹ค. ํ•™์ƒ์€ 1๋ฒˆ๋ถ€ํ„ฐ N2๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ , (r, c)๋Š” rํ–‰ c์—ด์„ ์˜๋ฏธํ•œ๋‹ค. ๊ต์‹ค์˜ย ๊ฐ€์žฅ ์™ผ์ชฝ ์œ— ์นธ์€ (1, 1)์ด๊ณ , ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋žซ ์นธ์€ (N, N)์ด๋‹ค. ์„ ์ƒ๋‹˜์€ ํ•™์ƒ์˜ ์ˆœ์„œ๋ฅผ ์ •ํ–ˆ๊ณ , ๊ฐ ํ•™์ƒ์ด ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ 4๋ช…๋„ ๋ชจ๋‘ ์กฐ์‚ฌํ–ˆ๋‹ค. ์ด์ œย ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์„ ์ด์šฉํ•ด ์ •ํ•ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ํ•™์ƒ์˜ ์ž๋ฆฌ๋ฅผ ์ •ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•œ ์นธ์—๋Š” ํ•™์ƒ ํ•œ ๋ช…์˜ ์ž๋ฆฌ๋งŒ ์žˆ์„ ์ˆ˜ ์žˆ๊ณ , |r1 - r2| + |c1 - c2| = 1์„ ๋งŒ์กฑํ•˜๋Š” ๋‘ ์นธ์ด (r1, c1)๊ณผย (r2, c2)๋ฅผ ์ธ์ ‘ํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

๋น„์–ด์žˆ๋Š” ์นธ ์ค‘์—์„œ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ์ธ์ ‘ํ•œ ์นธ์—ย ๊ฐ€์žฅ ๋งŽ์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค. 1์„ ๋งŒ์กฑํ•˜๋Š” ์นธ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด, ์ธ์ ‘ํ•œ ์นธ ์ค‘์—์„œ ๋น„์–ด์žˆ๋Š” ์นธ์ด ๊ฐ€์žฅ ๋งŽ์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค. 2๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์นธ๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ํ–‰์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์œผ๋กœ, ๊ทธ๋Ÿฌํ•œ ์นธ๋„ ์—ฌ๋Ÿฌ ๊ฐœ์ด๋ฉด ์—ด์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์นธ์œผ๋กœ ์ž๋ฆฌ๋ฅผ ์ •ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, N = 3์ด๊ณ , ํ•™์ƒ N2๋ช…์˜ ์ˆœ์„œ์™€ ๊ฐ ํ•™์ƒ์ด ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.

ํ•™์ƒ์˜ ๋ฒˆํ˜ธ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์˜ ๋ฒˆํ˜ธ

4 2, 5, 1, 7

3 1, 9, 4, 5

9 8, 1, 2, 3

8 1, 9, 3, 4

7 2, 3, 4, 8

1 9, 2, 5, 7

6 5, 2, 3, 4

5 1, 9, 2, 8

2 9, 3, 1, 4

๊ฐ€์žฅ ๋จผ์ €, 4๋ฒˆ ํ•™์ƒ์˜ ์ž๋ฆฌ๋ฅผ ์ •ํ•ด์•ผ ํ•œ๋‹ค. ํ˜„์žฌ ๊ต์‹ค์˜ ๋ชจ๋“  ์นธ์€ ๋นˆ ์นธ์ด๋‹ค. 2๋ฒˆ ์กฐ๊ฑด์— ์˜ํ•ด ์ธ์ ‘ํ•œ ์นธ ์ค‘์—์„œ ๋น„์–ด์žˆ๋Š” ์นธ์ด ๊ฐ€์žฅ ๋งŽ์€ ์นธ์ธ (2, 2)์ด 4๋ฒˆ ํ•™์ƒ์˜ ์ž๋ฆฌ๊ฐ€ ๋œ๋‹ค.

ย  ย  ย 

ย  4 ย 

ย  ย  ย 

๋‹ค์Œ ํ•™์ƒ์€ 3๋ฒˆ์ด๋‹ค. 1๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์นธ์€ (1, 2), (2, 1), (2, 3), (3, 2) ์ด๋‹ค. ์ด ์นธ์€ ๋ชจ๋‘ ๋น„์–ด์žˆ๋Š” ์ธ์ ‘ํ•œ ์นธ์ด 2๊ฐœ์ด๋‹ค. ๋”ฐ๋ผ์„œ, 3๋ฒˆ ์กฐ๊ฑด์— ์˜ํ•ด (1, 2)๊ฐ€ 3๋ฒˆ ํ•™์ƒ์˜ ์ž๋ฆฌ๊ฐ€ ๋œ๋‹ค.

ย  3 ย 

ย  4 ย 

ย  ย  ย 

๋‹ค์Œ์€ 9๋ฒˆ ํ•™์ƒ์ด๋‹ค. 9๋ฒˆ ํ•™์ƒ์ด ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์˜ ๋ฒˆํ˜ธ๋Š” 8, 1, 2, 3์ด๊ณ , ์ด ์ค‘์— 3์€ ์ž๋ฆฌ์— ์•‰์•„์žˆ๋‹ค. ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์ด ๊ฐ€์žฅ ๋งŽ์ด ์ธ์ ‘ํ•œ ์นธ์€ (1, 1), (1, 3)์ด๋‹ค. ๋‘ ์นธ ๋ชจ๋‘ ๋น„์–ด์žˆ๋Š” ์ธ์ ‘ํ•œ ์นธ์ด 1๊ฐœ์ด๊ณ , ํ–‰์˜ ๋ฒˆํ˜ธ๋„ 1์ด๋‹ค. ๋”ฐ๋ผ์„œ, 3๋ฒˆ ์กฐ๊ฑด์— ์˜ํ•ด (1, 1)์ด 9๋ฒˆ ํ•™์ƒ์˜ ์ž๋ฆฌ๊ฐ€ ๋œ๋‹ค.

9 3 ย 

ย  4 ย 

ย  ย  ย 

์ด๋ฒˆ์— ์ž๋ฆฌ๋ฅผ ์ •ํ•  ํ•™์ƒ์€ 8๋ฒˆ ํ•™์ƒ์ด๋‹ค. (2, 1)์ด 8๋ฒˆ ํ•™์ƒ์ด ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ๊ณผ ๊ฐ€์žฅ ๋งŽ์ด ์ธ์ ‘ํ•œ ์นธ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๊ธฐ๊ฐ€ ๊ทธ ํ•™์ƒ์˜ ์ž๋ฆฌ์ด๋‹ค.

9 3 ย 

8 4 ย 

ย  ย  ย 

7๋ฒˆ ํ•™์ƒ์˜ ์ž๋ฆฌ๋ฅผ ์ •ํ•ด๋ณด์ž. 1๋ฒˆ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์นธ์€ (1, 3), (2, 3), (3, 1), (3, 2)๋กœ ์ด 4๊ฐœ๊ฐ€ ์žˆ๊ณ , ๋น„์–ด์žˆ๋Š” ์นธ๊ณผ ๊ฐ€์žฅ ๋งŽ์ด ์ธ์ ‘ํ•œ ์นธ์€ (2, 3), (3, 2)์ด๋‹ค. ํ–‰์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ (2, 3)์ด 7๋ฒˆ ํ•™์ƒ์˜ ์ž๋ฆฌ๊ฐ€ ๋œ๋‹ค.

9 3 ย 

8 4 7

ย  ย  ย 

์ด๋Ÿฐ์‹์œผ๋กœ ํ•™์ƒ์˜ ์ž๋ฆฌ๋ฅผ ๋ชจ๋‘ ์ •ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

9 3 2

8 4 7

5 6 1

์ด์ œ ํ•™์ƒ์˜ ๋งŒ์กฑ๋„๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. ํ•™์ƒ์˜ ๋งŒ์กฑ๋„๋Š” ์ž๋ฆฌ ๋ฐฐ์น˜๊ฐ€ ๋ชจ๋‘ ๋๋‚œ ํ›„์— ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•™์ƒ์˜ ๋งŒ์กฑ๋„๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด ๊ทธ ํ•™์ƒ๊ณผย ์ธ์ ‘ํ•œ ์นธ์— ์•‰์€ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ ๊ฐ’์ด 0์ด๋ฉด ํ•™์ƒ์˜ ๋งŒ์กฑ๋„๋Š” 0, 1์ด๋ฉด 1, 2์ด๋ฉด 10, 3์ด๋ฉด 100, 4์ด๋ฉด 1000์ด๋‹ค. ํ•™์ƒ์˜ ๋งŒ์กฑ๋„์˜ ์ด ํ•ฉ์„ ๊ตฌํ•ด๋ณด์ž.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N2๊ฐœ์˜ ์ค„์— ํ•™์ƒ์˜ ๋ฒˆํ˜ธ์™€ ๊ทธ ํ•™์ƒ์ด ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ 4๋ช…์˜ ๋ฒˆํ˜ธ๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์„ ์ƒ๋‹˜์ด ์ž๋ฆฌ๋ฅผ ์ •ํ• ย ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ํ•™์ƒ์˜ ๋ฒˆํ˜ธ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š์œผ๋ฉฐ, ์–ด๋–ค ํ•™์ƒ์ด ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ 4๋ช…์€ ๋ชจ๋‘ ๋‹ค๋ฅธ ํ•™์ƒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ํ•™์ƒ์˜ ๋ฒˆํ˜ธ, ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ์˜ ๋ฒˆํ˜ธ๋Š” N2๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์–ด๋–ค ํ•™์ƒ์ด ์ž๊ธฐ ์ž์‹ ์„ ์ข‹์•„ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํ•™์ƒ์˜ ๋งŒ์กฑ๋„์˜ ์ด ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.


์ œํ•œ

3 โ‰ค N โ‰ค 20


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

3
4 2 5 1 7
3 1 9 4 5
9 8 1 2 3
8 1 9 3 4
7 2 3 4 8
1 9 2 5 7
6 5 2 3 4
5 1 9 2 8
2 9 3 1 4


์ถœ๋ ฅ

54


์˜ˆ์ œ 2

์ž…๋ ฅ

3
4 2 5 1 7
2 1 9 4 5
5 8 1 4 3
1 2 9 3 4
7 2 3 4 8
9 8 4 5 7
6 5 2 3 4
8 4 9 2 1
3 9 2 1 4


์ถœ๋ ฅ

1053


My Sol

def fill_order(n):
    global mat, N
    def check_like(n, i, j):
        global mat, N, likes_arr
        blank, ret = 0, 0
        for di, dj in ((-1,0),(1,0),(0,-1),(0,1)):
            si, sj = i+di, j+dj
            if not (0<=si<N and 0<=sj<N): continue
            if not mat[si][sj]: blank += 1
            elif mat[si][sj] in likes_arr[n]: ret += 1
        return ret, blank

    ret = []
    max_like_cnt = 0
    for i in range(N):
        for j in range(N):
            if mat[i][j]: continue
            check_like_ret, check_blank_ret = check_like(n, i, j)
            if max_like_cnt > check_like_ret: continue
            elif max_like_cnt == check_like_ret:
                ret.append((i, j, check_blank_ret))
            elif max_like_cnt < check_like_ret:
                max_like_cnt = check_like_ret
                ret = [(i, j, check_blank_ret)]
    return ret


N = int(input())
likes_arr = [set() for _ in range(N**2+1)]
orders = []
for _ in range(N**2):
    n, *likes = map(int, input().split())
    orders.append(n)
    likes_arr[n] = set(likes)

mat = [[0]*N for _ in range(N)]
for order in orders:
    points = fill_order(order)
    points.sort(key= lambda x: (-x[2], x[0], x[1]))
    ai, aj, ablank = points[0]
    mat[ai][aj] = order

def check_score(ki, kj):
    global mat, N, likes_arr
    n = mat[ki][kj]
    match = 0
    for di, dj in ((-1,0),(1,0),(0,-1),(0,1)):
        si, sj = i+di, j+dj
        if not (0<=si<N and 0<=sj<N): continue
        if not mat[si][sj]: continue
        if mat[si][sj] in likes_arr[n]: match += 1
    return 10**(match-1) if match else 0

scores = 0
for i in range(N):
    for j in range(N):
        scores += check_score(i, j)

print(scores)

์‚ผ์„ฑ SW์—ญ๋Ÿ‰ํ‰๊ฐ€ ๋Œ€๋น„ ๊ตฌํ˜„ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋‹ค. ๋‹จ๊ณ„๋ณ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ฒœ์ฒœํžˆ ํ’€์–ด๋‚ด๋ฉด ๋œ๋‹ค.

  1. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ด๋•Œ ์ข‹์•„ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•  ๋•Œ ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ์„ฑ์งˆ๋กœ O(1)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด set์„ ์ด์šฉํ•œ๋‹ค.
  2. fill_order ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋‘๋Š”๋ฐ, 2์ฐจ์› ๋ฐฐ์—ด mat์„ ์™„์ „ํƒ์ƒ‰ํ•˜์—ฌ ๋งค ์นธ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํ™•์ธํ•œ๋‹ค.
  3. ํ•ด๋‹น ํ•™์ƒ ๋ฒˆํ˜ธ๊ฐ€ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ ๋ฒˆํ˜ธ set์— ์ƒํ•˜์ขŒ์šฐ์˜ ๊ฐ’์ด ๊ฐ๊ฐ ์žˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค. ์ด๋•Œ ์ตœ๋Œ“๊ฐ’๋ณด๋‹ค ํ˜„์žฌ ์กฐํšŒ์˜ ํฌ๊ธฐ๊ฐ€ ๋” ํฌ๋ฉด ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
  4. 2์ˆœ์œ„๊ฐ€ ์ฃผ๋ณ€์— ๋นˆ์นธ์ด ๋งŽ์€ ์ˆœ์ด๋ฏ€๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์กฐํšŒํ•  ๋•Œ ๋นˆ์นธ, ์ฆ‰ 0์ธ ๊ฒฝ์šฐ blank๋ฅผ ๋†’์ด๊ณ , ์กฐํšŒ๊ฐ€ ๋๋‚˜๋ฉด ํ–‰๋ ฌ๊ณผ ํ•จ๊ป˜ tuple๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  5. 2์ˆœ์œ„๊ฐ€ ์ƒํ•˜์ขŒ์šฐ ๋นˆ์นธ์˜ ๊ฐœ์ˆ˜, 3์ˆœ์œ„๊ฐ€ ๊ฐ๊ฐ ํ–‰๊ณผ ์—ด์ด ์ž‘์€ ์ˆœ์ด๋ฏ€๋กœ lambda๋ฅผ ํ™œ์šฉํ•ด ํ•œ ๋ฒˆ์— ๋‚ด๋ฆผ์ฐจ์ˆœ, ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ๋‹ค.
  6. ์ •๋ ฌ๋œ ๋ฐฐ์—ด์˜ ์ฒซ ์š”์†Œ์˜ ํ–‰๋ ฌ๊ฐ’์ด ํ˜„์žฌ ํ•™์ƒ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋“ค์–ด๊ฐˆ mat์—์„œ์˜ ์œ„์น˜์ด๋‹ค.
  7. ์ด๋ฅผ ์ด์šฉํ•ด ๋ชจ๋‘ mat์„ ์ฑ„์šด๋‹ค.
  8. ๊ฐ๊ฐ์˜ mat์˜ ํ•™์ƒ ๋ฒˆํ˜ธ๋ฅผ ๋ชจ๋‘ ์™„์ „ํƒ์ƒ‰ํ•˜๋ฉฐ ์ƒํ•˜์ขŒ์šฐ์˜ ๊ฐ’๋“ค์„ ๊ฐ๊ฐ ์ข‹์•„ํ•˜๋Š” ํ•™์ƒ set์—์„œ ์ฐพ๋Š”๋‹ค. ๊ทธ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  9. ์ „์—ญ์—์„œ ์ด ๋ฐ˜ํ™˜๊ฐ’์„ ๋ชจ๋‘ ๋ˆ„์ ํ•œ ๋’ค, ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

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

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