๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿงฉps/๐Ÿ”ฅHard

[๋ฐฑ์ค€ 2503๋ฒˆ] ์ˆซ์ž ์•ผ๊ตฌ

by goguma.dev 2024. 10. 14.

๐Ÿ“–๋ฌธ์ œ:

 

๐Ÿ“™ํ’€์ด:

๋ฌธ์ œ๋ฅผ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฐฉํ–ฅ์œผ๋กœ ํ’€๋‹ค ๋ณด๋‹ˆ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ์†Œ์š”๋˜์—ˆ๋‹ค. (1์‹œ๊ฐ„ ๋ฐ˜..)

๊ฒฐ๊ตญ ๊ตฌ๊ธ€๋ง์„ ํ•˜์—ฌ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•จ.

๊ธฐ์กด์— ์ฝ”๋“œ๋ฅผ ์ง  ๋ฐฉ์‹์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

๋ธŒ๋ฃจํŠธํฌ์Šค ๋ฐฉ์‹์œผ๋กœ ์งœ๋ ค๊ณ  ์˜๋„ํ–ˆ์œผ๋‚˜, ๊ฒฐ๊ณผ์— ๋งž๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ขํ˜€๋‚˜๊ฐ€๋ ค๋‹ค ๋ณด๋‹ˆ๊นŒ ์™„์ „ ํ•˜๋“œ ์ฝ”๋”ฉ์ด ๋˜์–ด ๋ฒ„๋ ธ๋‹ค..

์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ๊ฐ„์†Œํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์„์ง€, ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ ์ค„์ผ ์ˆ˜ ์žˆ์„์ง€๋ฅผ ๋งŽ์ด ๊ณ ๋ฏผํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

 

'''
https://www.acmicpc.net/problem/2503
๋ฌธ์ œ: ์ˆซ์ž ์•ผ๊ตฌ
๋‚œ์ด๋„: ์‹ค๋ฒ„3
'''

answer_box = []
# ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜
for i in range(1, 10):
    for j in range(1, 10):
        for k in range(1, 10):
            if i != j and j != k and i != k:
                answer_box.append(str(i*100 + j *10 + k))

three_strike = False

testCase = int(input())
for _ in range(testCase):
    num, s, b = map(str, input().split())
    this_case_box = []
    if s == '3': # 3์ŠคํŠธ๋ผ์ดํฌ: ์ •๋‹ต์ด ํ•˜๋‚˜๋กœ ํ™•์ •๋จ
        answer_box = [num]
        break
    elif s == '2': # 2์ŠคํŠธ๋ผ์ดํฌ
        for i in range(1, 10):
            if num[0] != str(i):
                this_case_box.append(str(i)+num[1]+num[2])
            if num[1] != str(i):
                this_case_box.append(num[0]+str(i)+num[2])
            if num[2] != str(i):
                this_case_box.append(num[0]+num[1]+str(i))
    elif s == '1': # 1์ŠคํŠธ๋ผ์ดํฌ
        # 1์ŠคํŠธ๋ผ์ดํฌ & 2๋ณผ
        if b == '2':
            this_case_box.append(num[0]+num[2]+num[1])
            this_case_box.append(num[2]+num[1]+num[0])
            this_case_box.append(num[1]+num[0]+num[2])
        # 1์ŠคํŠธ๋ผ์ดํฌ & 1๋ณผ
        elif b == '1':
            for i in range(1, 10):
                this_case_box.append(num[0]+num[2]+str(i))
                this_case_box.append(num[0]+str(i)+num[1])
                this_case_box.append(str(i)+num[1]+num[0])
                this_case_box.append(num[2]+num[1]+str(i))
                this_case_box.append(str(i)+num[0]+num[2])
                this_case_box.append(num[1]+str(i)+num[2])
        # 1์ŠคํŠธ๋ผ์ดํฌ & 0๋ณผ
        elif b == '0':
            for i in range(1, 10):
                for j in range(1, 10):
                    if str(i) != num[1] or str(j) != num[2] or str(i) != num[2] or str(j) != num[1]:
                        this_case_box.append(num[0]+str(i)+str(j))
                    if str(i) != num[0] or str(j) != num[2] or str(i) != num[2] or str(j) != num[0]:
                        this_case_box.append(str(i)+num[1]+str(j))
                    if str(i) != num[0] or str(j) != num[1]:
                        this_case_box.append(str(i)+str(j)+num[2])
    elif s == '0': # 0์ŠคํŠธ๋ผ์ดํฌ
        # 0์ŠคํŠธ๋ผ์ดํฌ & 3๋ณผ
        if b == '3':
            this_case_box.append(num[1]+num[2]+num[0])
            this_case_box.append(num[2]+num[0]+num[1])
        # 0์ŠคํŠธ๋ผ์ดํฌ & 2๋ณผ
        if b == '2':
            for i in range(1, 10):
                this_case_box.append(str(i)+num[0]+num[1])
                this_case_box.append(str(i)+num[2]+num[0])
                this_case_box.append(str(i)+num[2]+num[1])
                this_case_box.append(num[1]+str(i)+num[0])
                this_case_box.append(num[2]+str(i)+num[0])
                this_case_box.append(num[1]+num[0]+str(i))
                this_case_box.append(num[1]+num[2]+str(i))
                this_case_box.append(num[2]+num[0]+str(i))
        # 0์ŠคํŠธ๋ผ์ดํฌ & 1๋ณผ
        if b == '1':
            for i in range(1, 10):
                for j in range(1, 10):
                    if str(i) != num[1] or str(j) != num[2] or str(i) != num[2] or str(j) != num[1]:
                        this_case_box.append(str(i)+num[0]+str(j))
                    if str(i) != num[1] or str(j) != num[2] or str(i) != num[2] or str(j) != num[1]:
                        this_case_box.append(str(i)+str(j)+num[0])
                    if str(i) != num[0] or str(j) != num[2] or str(i) != num[2] or str(j) != num[0]:
                        this_case_box.append(num[1]+str(i)+str(j))
                    if str(i) != num[0] or str(j) != num[2] or str(i) != num[2] or str(j) != num[0]:
                        this_case_box.append(str(i)+str(j)+num[1])
                    if str(i) != num[0] or str(j) != num[1] or str(i) != num[1] or str(j) != num[0]:
                        this_case_box.append(num[2]+str(i)+str(j))
                    if str(i) != num[0] or str(j) != num[1] or str(i) != num[1] or str(j) != num[0]:
                        this_case_box.append(str(i)+num[2]+str(j))
        # 0์ŠคํŠธ๋ผ์ดํฌ & 0๋ณผ
        if b == '0':
            for i in range(1, 10):
                for j in range(1, 10):
                    for k in range(1, 10):
                        if i != num[0] or i != num[1] or i != num[2] or j != num[0] or j != num[1] or j != num[2] or k != num[0] or k != num[1] or k != num[2]:
                            this_case_box.append(str(i)+str(j)+str(k))

    answer_box = list(set(answer_box) & set(this_case_box))
    print(answer_box)


print(len(answer_box))

 

โœ๏ธ์ •๋‹ต ์ฝ”๋“œ:

'''
https://www.acmicpc.net/problem/2503
๋ฌธ์ œ: ์ˆซ์ž ์•ผ๊ตฌ
๋‚œ์ด๋„: ์‹ค๋ฒ„3
'''

import sys
from itertools import permutations
input = sys.stdin.readline

n = int(input())
nums = list(permutations(list(range(1, 10)), 3))	# 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์„œ๋กœ ๋‹ค๋ฅธ 3์ž๋ฆฌ ์ˆซ์ž ๋ชจ์Œ

for _ in range(n):
    num, s, b = map(int, input().split())
    tmp = []	# ํ˜„์žฌ ์กฐ๊ฑด์— ๋งž๋Š” ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐˆ ๋ฐฐ์—ด

    for check in nums:		# 3์ž๋ฆฌ ์ˆซ์ž ๋ชจ์Œ ํƒ์ƒ‰
        cnt_s, cnt_b = 0, 0	# ์ŠคํŠธ๋ผ์ดํฌ์™€ ๋ณผ์˜ ๊ฐฏ์ˆ˜

        for i, str_num in enumerate(str(num)):	# ์ž…๋ ฅํ•œ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด ์ˆซ์ž์˜ ์œ„์น˜์™€ ๊ฐ’์„ ํ•œ ๋ฒˆ์— ํ™•์ธ
            if int(str_num) == check[i]:		# ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ์ˆซ์ž์™€ ์ž…๋ ฅํ•œ ์ˆซ์ž ์ค‘ ์œ„์น˜์™€ ๊ฐ’์ด ๊ฐ™์€ ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด
                cnt_s += 1						# ์ŠคํŠธ๋ผ์ดํฌ ๊ฐฏ์ˆ˜ ์ฆ๊ฐ€
            if int(str_num) != check[i] and int(str_num) in check:	# ์œ„์น˜๋Š” ๋‹ค๋ฅด๋‚˜ ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด
                cnt_b += 1						# ๋ณผ ๊ฐฏ์ˆ˜ ์ฆ๊ฐ€

        if s == cnt_s and b == cnt_b:			# ์ŠคํŠธ๋ผ์ดํฌ์™€ ๋ณผ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ์ž…๋ ฅ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด
            tmp.append(check)					# tmp์— ํ˜„์žฌ ์ˆซ์ž ์ถ”๊ฐ€
    nums = tmp									# ๊ธฐ์กด 3์ž๋ฆฌ ์ˆซ์ž์˜ ๋ชจ์Œ์„ ํ˜„์žฌ๊นŒ์ง€์˜ ์กฐ๊ฑด์— ๋งž์ถฐ ์ตœ์‹ ํ™”

print(len(nums))								# ์ตœ์ข… ์กฐ๊ฑด์— ๋งž๋Š” ์ˆ˜๋“ค์˜ ๊ธธ์ด ์ถœ๋ ฅ

 

๐Ÿ”—๋งํฌ:

https://www.acmicpc.net/problem/2503

'๐Ÿงฉps > ๐Ÿ”ฅHard' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[๋ฐฑ์ค€ 18115๋ฒˆ] ์นด๋“œ ๋†“๊ธฐ  (0) 2024.09.30