ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17140๋ฒˆ: ์ด์ฐจ์› ๋ฐฐ์—ด๊ณผ ์—ฐ์‚ฐ

์ฒซ์งธ ์ค„์— r, c, k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ r, c, k ≤ 100) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ 3๊ฐœ์˜ ์ค„์— ๋ฐฐ์—ด A์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฐฐ์—ด A์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋Š” 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ์—์„œ ๋ฐฐ์—ด A๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๋ฐฐ์—ด์˜ ํ–‰, ์—ด์˜ ์ˆ˜์— ๋”ฐ๋ผ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ ํ›„ ํ–‰๋ ฌ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—ด์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ
    • ๋ฐฐ์—ด A์˜ ๋ชจ๋“  ํ–‰์— ๋Œ€ํ•ด ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค
  • ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ ์—ด์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ
    • ๋ฐฐ์—ด A์˜ ๋ชจ๋“  ์—ด์— ๋Œ€ํ•ด ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • ์ •๋ ฌ์„ ์œ„ํ•ด์„œ๋Š” ํ•œ ํ–‰ ๋˜๋Š” ํ•œ ์—ด์˜ ์ˆ˜์™€ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
    • (count, number)์™€ ๊ฐ™์€ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
    • ์ •๋ ฌํ•  ๋•Œ, 0์€ ๋ฌด์‹œํ•œ๋‹ค.
  • ๋ณ€ํ™˜๋œ ํ–‰์—ด์„ A์— ๋ฐ˜ํ™˜ํ•  ๋•Œ, ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ํ–‰ ๋˜๋Š” ์—ด์„ ๊ธฐ์ค€์œผ๋กœ 0์œผ๋กœ ์ฑ„์šด๋‹ค.

์ด์™€ ๊ฐ™์€ ๊ณผ์ •์„ 100๋ฒˆ(์ดˆ)๋งŒํผ ์ง„ํ–‰ํ•˜๊ณ , `A[R][C] == K`๊ฐ€ ๋˜๋Š” ๋‹ต์„ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ดˆ๊ธฐ์— ๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ•  ๋•Œ, ์˜ˆ์ œ 6์„ ์ƒ๊ฐํ•˜์ง€ ์•Š์•„์„œ ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ–ˆ๋‹ค. ํ–‰์—ด์˜ ํฌ๊ธฐ๊ฐ€ 100์„ ๋„˜๋Š” ๊ฒฝ์šฐ, ์ฒ˜์Œ 100๊ฐœ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋Š” ์ž˜๋ผ๋‚ด์•ผ ํ•˜๋Š” ์ œํ•œ ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ๋งž์•˜์Šต๋‹ˆ๋‹ค๊ฐ€ ๋– ์„œ ๋‹นํ™ฉํ–ˆ๋‹ค.๐Ÿค”

 

์ฝ”๋“œ

from sys import stdin
from copy import deepcopy
from collections import Counter
from itertools import chain


def check_answer() -> bool:
    '''
    4 4 1
    1 2 1
    2 1 3
    3 3 3
    ์ด์™€ ๊ฐ™์€ ์˜ˆ์‹œ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์œผ๋ฉด, ์ฒ˜์Œ ๋ถ€ํ„ฐ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋ฐœ์ƒ!
    '''
    if R < num_rows() and C < num_columns():
        return A[R][C] == K
    return 0


def num_rows() -> int:
    return len(A)


def num_columns() -> int:
    return len(A[0])


def calc(cur_dir: str):
    global A
    temp = []
    max_len = 0
    # ๋ฐฉํ–ฅ์ด ์—ด์ธ ๊ฒฝ์šฐ, ์•„๋ž˜ ๋กœ์ง์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ–‰์—ด์ „ํ™˜
    cur_matrix = A if cur_dir == "row" else list(map(list, zip(*A)))

    for nums in cur_matrix:
        # ๊ฐ ์š”์†Œ๋ฅผ ์นด์šดํŠธ ํ•œ ํ›„, ์ˆซ์ž๊ฐ€ 0 ์ด์ƒ์ธ ๊ฒฝ์šฐ๋งŒ (์นด์šดํŠธ, ์ˆซ์ž) ์ˆœ์œผ๋กœ ์ •๋ ฌ
        cur_nums = list(chain(*sorted(Counter(filter(lambda x: x > 0, nums)).items(), key=lambda x: (x[1], x[0]))))
        max_len = max(len(cur_nums), max_len)
        temp.append(cur_nums)

    # ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์€ 0์œผ๋กœ ์ฑ„์šฐ๋Š” ๊ณผ์ • ํ•„์š”
    for idx, nums in enumerate(temp):
        cur_len = len(nums)

        if cur_len < max_len:
            temp[idx] += [0] * (max_len - cur_len)

    # ํ–‰์—ด์„ ์ „ํ™˜ํ•œ ๊ฒฝ์šฐ, ๋‹ค์‹œ ์›์ƒํƒœ๋กœ ์ „ํ™˜
    if cur_dir == "column":
        temp = list(map(list, zip(*temp)))

    A = deepcopy(temp)


if __name__ == '__main__':
    R, C, K = map(int, stdin.readline().split())
    R -= 1
    C -= 1
    A = [list(map(int, stdin.readline().split())) for _ in range(3)]

    # ์ฒ˜์Œ ๋ถ€ํ„ฐ ์ •๋‹ต์ธ ๊ฒฝ์šฐ
    if check_answer():
        print(0)
        exit(0)

    # ์กฐ๊ฑด์— ๋”ฐ๋ผ, ์‹œ๋ฎฌ๋ ˆ์ด์…˜์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
    for cur_time in range(100):
        direction = "row" if num_rows() >= num_columns() else "column"
        calc(direction)

        if check_answer():
            print(cur_time + 1)
            exit(0)
    # 100์ดˆ ํ›„์—๋„ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ
    else:
        print(-1)

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€