1+ """
2+ #2024์ผ์ฑ ์๋ฐ๊ธฐ ์ค์ 1๋ฒ ๋ฌธ์ / ๊ณ ๋๋ฌธ๋ช
์ ์ ํ์ฌ
3+ #๋งํฌ : https://www.codetree.ai/ko/frequent-problems/problems/ancient-ruin-exploration/description?introductionSetId=&bookmarkId=
4+
5+
6+ """
7+ """
8+ # ์ ํ : ๊ฑ ๊ตฌํ
9+ - . 5x5์ 7๊ฐ์ง ์ ๋ฌผ(1-7)
10+ 1. ํ์ฌ ์งํ - ์ง์ ๋ 3x3ํ์ ์๊ณ๋ฐฉํฅ์ผ๋ก [90,180 , 270] ์ค ํ๋์ ๊ฐ๋๋ก ํ์
11+ => ๊ฐ ํ์ ์ "๊ฐ๋| ํ๋๊ฐ์น | ์ค์ฌ์ขํ" ์ ์ฅ
12+ - (1) ์ ๋ฌผ 1์ฐจ ํ๋ ๊ฐ์น ์ต๋ํ
13+ - (2) ํ์ ๊ฐ๋ ์ต์ํ
14+ - (3) ํ์ ์ค์ฌ ์ขํ ์ด(row)์ด ์ต์ -> ํ ์ต์
15+
16+
17+ 2. ์ ๋ฌผ ํ๋
18+ - 3๊ฐ ๊ฐ์ ์ข
๋ฅ ์ด์ํ๋ฉด -> ์ฌ๋ผ์ง๋ ์กฐ๊ฐ ๊ฐ์ = ์ ๋ฌผ ๊ฐ์ง => ํํธ์ ๋น
19+ - ์์ฑ ์์๋ ์ ์ ๋ฒฝ๋ฉด ์์ -> ์ด์ด ์์ ์ชฝ -> & ํ ํฐ๊ฒ ์์ up
20+ - ์กฐ๊ฐ ๊ณ ๊ฐ ๋ฌธ์ ๋ ์์, ๋จ ์ฌ์ฉํ ์กฐ๊ฐ์ ์ฌํ์ฉ x >
21+
22+ 3. ํ์ฌ ๋ฐ๋ณต (์ถ๋ ฅ์กฐ๊ฑด)
23+ - K๋ฒ (1๋ฒ : ํ์ฌ์งํ -> ์ ๋ฌผํ๋) => ํ๋ ์ ๋ฌผ ๊ฐ์น ์ถ๋ ฅ
24+ - ์ค๊ฐ ํ๋ ์ ๋ฌผ ๋ฐฉ๋ฒ ์กด์ฌ x -> ์ข
๋ฃ (์ถ๋ ฅ : x )
25+
26+ """
27+
28+ """
29+ <์ ์ฒด flow>
30+ # 0. ๋ณ์ ์
๋ ฅ : ํ์ฌ ํ์ K , ์ ๋ฌผ ์คํ์ด ๊ฐ์ M
31+ ### K ๋ฒ ๋ฐ๋ณต
32+ # ์ค์ฌ์ขํ ํ๋ณด๊ตฐ 9๊ฐ / ๊ฐ๋ ํ๋ณด๊ตฐ 3๊ฐ -> 27๋ฒ ๋ฐ๋ณต
33+ #1. def ํ์ (์ค์ฌ์ขํ, ๊ฐ๋)
34+
35+ #2. ์ ๋ฌผ ํ๋ -> ์ ๋ฌผ ๊ฐ์น ์ ์ฅ
36+ #-> 27๋ฒ ๋ฐ๋ณต
37+ #3. ํด๋น ํด์์ Best choice ์ธ ์ํฉ ๊ฒฐ์
38+ # 3.์ ๋ฌผ ์
๋ฐ์ดํธ
39+ ###
40+ """
41+ """
42+ input : ์ค์ฌ ์ขํ, angle , field
43+ ouptut : ๋ณํ๋ field
44+
45+ ์์น idx :
46+ [[i-1 , j-1], [i-1, j],[i-1,j+1],[i+1,j-1] , [i,j] [ i,j+1], [i+1,j-1],[i+1,j],[i+1,j+1]]
47+ 90 = [7,5,1,8,5,2,9,6,3]
48+ 80 = [7,8,9,4,5,6,1,2,3]
49+ 270 = [1,4,7,2,5,8,3,6,9]
50+ """
51+ """
52+ # ์ ๋ฌผ ํ๋ ํจ์ : BFS
53+ # ํ field ์ํฉ์์ ์ป์ ์ ์๋ ๊ฒฝ์ฐ์ ์
54+ def get_old (field):
55+ """
56+
57+ # ์ค์ฌ ์ขํ ํ๋ณด๊ตฐ +
58+ # 0. ์
๋ ฅ ๋ณ์ ์
๋ ฅ ๋ฐ๊ธฐ
59+ K , M = map (int , input ().split ())
60+ # 0-1. ์ด๊ธฐ ํ๋ ๊ฐ ๋ฐ๊ธฐ
61+
62+ sfield = [list (map (int ,input ().split ())) for _ in range (5 ) ]
63+ wall = list (map (int , input ().split ()))
64+ # ์ํ์ข์ฐ
65+ dy = [- 1 ,1 ,0 ,0 ]
66+ dx = [0 ,0 ,- 1 ,1 ]
67+
68+
69+ # (1) 90/180/270 ํ์ ํจ์
70+ def rotate (i ,j , angle ,field ) :
71+ old_33 = [[i - 1 , j - 1 ], [i - 1 , j ],[i - 1 ,j + 1 ],[i + 1 ,j - 1 ] , [i ,j ] ,[ i ,j + 1 ], [i + 1 ,j - 1 ],[i + 1 ,j ],[i + 1 ,j + 1 ]]
72+ ro_pos = []
73+ if angle == "90" :
74+ ro_pos = [6 ,4 ,0 ,7 ,4 ,1 ,8 ,5 ,2 ]
75+ elif angle == "180" :
76+ ro_pos = [6 ,7 ,8 ,3 ,4 ,5 ,0 ,1 ,2 ]
77+ else :
78+ ro_pos = [0 ,3 ,6 ,1 ,4 ,7 ,2 ,5 ,8 ]
79+ new_field = [row [:] for row in field ]
80+ for p in range (0 ,9 ):
81+
82+ old = field [old_33 [p ][0 ]][old_33 [p ][1 ]]
83+ new_field [old_33 [ro_pos .index (p )][0 ]][old_33 [ro_pos .index (p )][1 ]] = old
84+ return field
85+
86+ # ํ field ์ํฉ์์ ๊ฐ์น ์
๋ฐ์ดํธ
87+ def get_old (field ):
88+ del_pos = []
89+ visited = [] # ๋ฐฉ๋ฌธ ์ฌ๋ถ
90+ oldest = [] # ๊ฐ start point์์ ์ฐ๊ฒฐ๋ ์ ๋ฌผ ์์น
91+ for i in range (5 ):
92+ for j in range (5 ):
93+ if [i ,j ] not in visited :
94+ # start point์ ๊ฐ์ ์ข
๋ฅ์ ์ ๋ฌผ๋ง ํ๋ ๊ฐ๋ฅ
95+ q = []
96+ q .append ([i ,j ])
97+ visited .append ([i ,j ])
98+ while q :
99+ cy ,cx = q .pop ()
100+ for d in range (4 ) :
101+ ny , nx = cy + dy [d ] , cx + dx [d ]
102+ if 0 <= ny < 5 and 0 <= nx < 5 :
103+ if [ny ,nx ] not in visited and field [ny ][nx ] == field [cy ][cx ]:
104+ q .append ([ny ,nx ])
105+ visited .append ([ny ,nx ])
106+ oldest .append ([ny ,nx ])
107+
108+ # ํ๋ ์์ผ๋ฉด -> ์ ๋ฌผ ๊ฐ์ + ์์น ๋์
109+ if len (oldest ) >= 3 : # 3๊ฐ ์ด์ ์ฐ๊ฒฐ์ ํ๋ ๊ฐ๋ฅ
110+ del_pos .extend (oldest )
111+ # print(del_pos)
112+ oldest .clear ()
113+
114+ return len (del_pos ) , del_pos
115+ # best ์ํฉ ์ ํ
116+ def cur_best (current_case ):
117+ arr = sorted (current_case , key = lambda x : (- x [0 ] , x [1 ] , x [2 ], x [3 ]))
118+ return arr [0 ]
119+
120+
121+ answer = []
122+
123+ for k in range (K ):
124+ current_case = [] # 27๊ฒฝ์ฐ [์ ๋ฌผ๊ฐ์ง, ๊ฐ๋ , ์ด, ํ ,์ญ์ ์์น ]
125+ # 9๊ฐ์ ์ค์ฌ์ขํ ํ๋ณด๊ตฐ
126+ ro_sub = [90 ,180 ,270 ]
127+ center_sub = [[1 ,1 ], [1 ,2 ],[1 ,3 ],[2 ,1 ] ,[2 ,2 ] ,[2 ,3 ],[3 ,1 ],[3 ,2 ],[3 ,3 ]]
128+ for center_y , center_x in center_sub :
129+ for ro_angle in ro_sub :
130+ sub_field = rotate (center_y ,center_x , ro_angle ,sfield )
131+ value_sub , del_sub = get_old (sub_field )
132+ current_case .append ([value_sub , ro_angle , center_y , center_x , sub_field ,del_sub ])
133+
134+ #3. ํด๋น ํด์์ ๋ฒ ์คํธ ์ํฉ 1๊ฐ ์ ํ
135+ # value ๊ฐ ์๋ ๊ฒฝ์ฐ -> ๋
136+ if len (current_case ) <= 0 :
137+ break
138+ best_sit = cur_best (current_case )
139+ answer .append (best_sit [0 ])
140+ #4. fiedl ์ํฉ ์
๋ฐ์ดํธ
141+ # ์ ๋ฌผ ๋งค๊พธ๊ธฐ - sort๋ก ์ญ์ ๋ ์์น ์ ๋ ฌ ํ wall(์ ๋ฌผ๋ฒฝ๋ฉด) ์ํ
142+ arr = sorted (best_sit [- 1 ] , key = lambda x : (x [0 ] , - x [1 ])) # ์ฌ๋ผ์ง ์ ๋ฌผ ์์น
143+ sfield = best_sit [- 2 ]
144+ pointer = 0
145+ for y ,x in arr :
146+ node = wall [pointer ]
147+ sfield [y ][x ] = node
148+ pointer = (pointer + 1 )% len (wall )
149+
150+ print (answer )
0 commit comments