๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 3009 - ๋„ค ๋ฒˆ์งธ ์ 

by YWTechIT 2021. 5. 30.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 3009 - ๋„ค ๋ฒˆ์งธ ์ 

๋ฐฑ์ค€ 3009 - ๋„ค ๋ฒˆ์งธ ์ 


โšก๏ธ ๋‚˜์˜ ํ’€์ด

์ˆ˜ํ•™์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ์ง€ ๋งŽ์€ ๊ณ ๋ฏผ์„ ํ–ˆ๋Š”๋ฐ ๊ฒฐ๋ก ์ ์œผ๋กœ ๊ฐ๊ฐ์˜ ์ž…๋ ฅ ์ค‘์—์„œ ์ž…๋ ฅ[0]๋งŒ์„ ๋ชจ์•„๋‘” ๋ฆฌ์ŠคํŠธ column, ์ž…๋ ฅ[1]๋งŒ์„ ๋ชจ์•„๋‘” ๋ฆฌ์ŠคํŠธ๋ฅผ row๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์„ธ ์ค„์˜ ์ž…๋ ฅ ์ค‘ ์ž…๋ ฅ[0]๋งŒ์„ ์ƒ๊ฐํ–ˆ์„ ๋•Œ ๋‘ ๊ฐœ์˜ ์ž…๋ ฅ[0]์ด ๊ฐ™์€ ๊ฐ’์ด๋ฉด ๋‚˜๋จธ์ง€ ํ•œ ๊ฐœ์™€ ์ž„์‹œ๋กœ ์„ ์–ธํ•œ `a`๊ฐ’์ด ๊ฐ™์€ ๊ฐ’์ด ๋˜์–ด์•ผ ํ•˜๊ณ  ๋ฐ˜๋Œ€๋กœ ์ž…๋ ฅ[1]๋งŒ์„ ์ƒ๊ฐํ–ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€ ํ•œ๊ฐœ์™€ `b`๋„ ๊ฐ™์€ ๊ฐ’์ด ๋˜์–ด์•ผํ•œ๋‹ค. ๋‚˜๋Š” ์ด๋Ÿฐ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค.

 

  1. defaultdict๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  2. ์ž…๋ ฅ[0]๋ฒˆ๋งŒ ๋ชจ์•„๋†“์€ column ๋ฆฌ์ŠคํŠธ์™€ ์ž…๋ ฅ[1]๋ฒˆ๋งŒ ๋ชจ์•„๋†“์€ row ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•œ๋‹ค.
  3. value๋งŒํผ +=1 ์„ ํ•ด์ค€๋‹ค.
  4. index๊ฐ€ 1๊ฐœ์ธ column_dict์™€ row_dict๋ฅผ ๊ฐ๊ฐ a์™€ b๋กœ ์„ ์–ธํ•ด์ค€๋‹ค.

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๊ตณ์ด defaultdict๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ countํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ 1๊ฐœ์ธ ๊ฐ’์„ ์ถœ๋ ฅํ•ด a์™€ b์— ์†์‰ฝ๊ฒŒ ๋Œ€์ž…ํ•ด๋„ ๋์—ˆ๋‹ค.

# ๋‚˜์˜ ์ฝ”๋“œ
from collections import defaultdict
column, row = [], []
a, b = -3333, -3333
column_dic, row_dic = defaultdict(int), defaultdict(int)

for _ in range(3):
    x, y = map(int, input().split())
    column.append(x)
    row.append(y)

for i in range(3):
    column_dic[column[i]] += 1
    row_dic[row[i]] += 1

for key, index in column_dic.items():
    if index == 1:
        a = key

for key, index in row_dic.items():
    if index == 1:
        b = key
print(a, b)
# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ
n = 3
column, row = [], []
a, b = -3333, -3333

for _ in range(3):
    x, y = map(int, input().split())
    column.append(x)
    row.append(y)

for i in range(n):
    if column.count(column[i]) == 1:
        a = column[i]
    if row.count(row[i]) == 1:
        b = row[i]
print(a, b)
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€