โ 2503 ์ซ์ ์ผ๊ตฌ โ
import sys
from itertools import permutations
input=sys.stdin.readline
N=int(input())
pos=list(permutations(['1','2','3','4','5','6','7','8','9'],3))
idxs=[n for n in range(len(pos))]
def get_strike_ball(a,b):
strike=0
for x,y in zip(a,b):
if x==y: strike+=1
return (str(strike),str(len(a+b)-len(set(a+b))-strike))
for _ in range(N):
num,S,B=input().split()
didxs=[]
for i in idxs:
posi=''.join(list(pos[i]))
if get_strike_ball(list(posi),list(num)) != (S,B): didxs.append(i)
for di in didxs: idxs.remove(di)
print(len(idxs))
๐ 123๊ณผ ๊ฐ์ด 9๊ฐ์ ์์ฐ์ ์ค ์์ ์๋ ์๋ก ๋ค๋ฅธ 3๊ฐ์ ์์ฐ์๋ก ์ด๋ฃจ์ด์ง ์์ ๊ฐ์ 9P3 permutations()
๐ strike / ball ๊ฐ์ count
: ๋จผ์ strike ๊ฐ์ ์ธ๊ณ , ์ ์ฒด ๊ฐ์ - ์ค๋ณต ์๋ ๊ฐ์ - strike ๊ฐ์๋ฅผ ball ๊ฐ์
๐ permutations() ๊ฒฐ๊ณผ pos list brute-force(๋ฒ์ ์์์ brute-force ๊ฐ๋ฅ) → join() / list()๋ก type ํต์ผ ๋ค ์๋์ (S,B)์ ๋น๊ต → ๋ค๋ฅด๋ค๋ฉด ๋ณ๋์ ์ญ์ idx ๋ชจ์ didx update(๊ทธ ์๋ฆฌ์์ removeํ๋ฉด ์ค๋ฅ ๋ฐ์: ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ณ๋์ ๋ฆฌ์คํธ ๋ง๋ค์ด ํ ๋ฒ์ ๋ชจ์ ๋ค ๋ฐ๋ก for loop ๋๋ ค remove) → ์ดํ didx for loop ๋๋ ค ๋ฐ๋ก idx remove() → ์ต์ข ๋จ์ idx list ์์ ์ ์ถ๋ ฅ
โ 3085 ์ฌํ ๊ฒ์ โ
#potential swap included in an array
import sys,copy
input=sys.stdin.readline
def get_consecutive_max(arr):
ans,cur=1,1
for i in range(1,len(arr)):
if arr[i]==arr[i-1]:
cur+=1
else:
ans=max(ans,cur)
cur=1
return max(ans,cur)
def bomboni(arr):
cur=0
n=len(arr)
#row-wise
for i in range(0,n):
cur=max(cur,get_consecutive_max(arr[i]))
#column-wise
for i in range(n):
column_arr=[]
for j in range(n):
column_arr.append(arr[j][i])
cur=max(cur,get_consecutive_max(column_arr))
return cur
N=int(input())
ans,arr=0,[]
for _ in range(N): arr.append(list(input().rstrip())) #made an array
ans=0
for i in range(N):
for j in range(N):
if j<=(N-2) and arr[i][j] != arr[i][j+1]: #right dir
arr2=copy.deepcopy(arr)
arr2[i][j], arr2[i][j+1] = arr2[i][j+1], arr2[i][j]
ans=max(ans,bomboni(arr2))
if i<=(N-2) and arr[i][j] != arr[i+1][j]: #down dir
arr2=copy.deepcopy(arr)
arr2[i][j], arr2[i+1][j] = arr2[i+1][j], arr2[i][j]
ans=max(ans,bomboni(arr2))
print(ans)
โ array traversal ๊ณผ์ ์์ swap๋๋ direction → right direction / downward direction
: ์ด๋ฏธ ๋ง๋ค์ด์ง array๊ฐ ์๋ ์๋ก์ด arr2 ๋ง๋ค์ด์(deepcopy() ์ฌ์ฉ) bomboni() ์งํ
โก bomboni(arr)
: row-wise / column-wise ๋ฐฉํฅ traversal
โข get_consecutive(arr)
: ์ฃผ์ด์ง 1์ฐจ์ array์์ ๊ฐ์ฅ ๊ธธ๊ฒ ์ฐ์ํ๋ ๋ฐฐ์ด ๊ธธ์ด ์ถ๋ ฅ
: ans ๋ณ์ ๋์ update(cur ๋ณ์ iterate) → ๋ง์ง๋ง return์ max() ํ๋ฒ ๋ ์ ๊ฒ(!)
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ Implementation&Simulation Upper-Intermediate I - 3 Solvedโ (0) | 2024.01.08 |
---|---|
โ Tree Upper-Intermediate I - 3 Solvedโ (0) | 2023.12.19 |
โ Greedy Intermediate II - 11 Solvedโ (0) | 2023.08.20 |
โ Combinatorics Upper-Intermediate I - 4 Solvedโ (0) | 2023.08.16 |
โ Regular Expression Upper + Intermediate - 3 Solvedโ (0) | 2023.08.14 |
๋๊ธ