728x90
예술성
- 링크: https://www.codetree.ai/training-field/frequent-problems/problems/artistry/description
- 기출: 삼성 SW 역량테스트 2022 상반기 오전 2번 문제
- 난이도:골드 3
- 분류: 시뮬레이션, DFS
- 직접 푼 여부: O 약 2시간 20분 걸림
- 언어: 파이썬
풀이 노트
오늘은 좀 뿌듯하다! 한 두번 만에 맞춘 것 같다! 이렇게 점점 나아지는 걸까, 더 열심히 해야지
위에 쓴 과정대로 생각하며 풀었다.
- 먼저 무슨 function을 정의해야 할지 생각하고 (결국 2번 function은 필요 없어서 안 썼다),
- 각 function을 어떻게 짤지 핵심 코드를 손코딩 해서 풀었다 (위 사진상 "별").
- 그리고 코드를 다 짜고 틀린 것은 각 function 마다 디버깅을 하며 문제를 찾았고, 그 문제는 위 사진상 빨간 글씨이다.
정답 코드 💻
# 코드트리 - 예술성
import sys
from collections import deque
import copy
input = sys.stdin.readline
# input------
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
# prepare ----
dr= [0,0,-1,1]
dc = [-1,1,0,0]
half = n // 2
startpoint = [(0,0), (half+1, 0), (0, half+1), (half+1, half+1)]
# group -----
def group():
"""bfs 이용. 그룹 만들기 [[(1,2),(2,2)],[ ], [ ] ] """
global a, groups, groupsnum
visited = [[False] *n for _ in range(n)]
for i in range(n):
for j in range(n):
if not visited[i][j]:
temp = [] # 한 그룹의 좌표값 넣을 곳
groupsnum.append(a[i][j]) # 해당 색깔에 해당하는 값 저장
q=deque()
q.append((i,j))
temp.append((i,j))
visited[i][j] =True
while q:
r, c = q.popleft()
for direction in range(4): # 4방향 다 둘러봄
newr, newc = r+dr[direction], c+dc[direction]
if 0<=newr<n and 0<=newc<n and not visited[newr][newc] and a[newr][newc] == a[i][j]:
temp.append((newr, newc)) # 현재 좌표값
visited[newr][newc] = True # 방문 했으니
q.append((newr,newc)) # 범위안, 안 가봤고, 같은 그룹 숫자면 더 탐색해 볼 필요 있음
groups.append(temp) # 한 그룹 확인 끝나면, groups에 그룹 append
# score compute --------
def score_compute():
"""점수 계산: (len(g1) + len(g2)) * g1num * g2num * 변 개수"""
total_score =0
# 0) combination
for g1 in range(len(groups)):
for g2 in range(g1+1, len(groups)):
# 1) 변 개수 구하기 -> g1기준으로
lines =0 # 겹치는 변의 개수
for p in groups[g1]:
r,c = p # 좌표 값
for direction in range(4):
newr, newc = r+dr[direction], c+dc[direction]
if 0<=newr<n and 0<=newc<n and (newr, newc) in groups[g2]:
lines +=1
# 2) 점수 계산
if lines>0:
total_score += (len(groups[g1]) + len(groups[g2])) * groupsnum[g1] * groupsnum[g2] * lines
return total_score
# rotate
def rotate():
"""돌리기 -> t / 나머지"""
global a
tempa = copy.deepcopy(a)
# 1) t 돌리기
for i in range(n):
a[n-1-i][half] = tempa[half][i] # 원래 가로
a[half][i] = tempa[i][half] # 원래 세로
# 2) 나머지 4개 돌리기
for r,c in startpoint:
for i in range(half):
for j in range(half):
a[r+j][c+half-1-i] = tempa[r+i][c+j]
# main
answer =0
for t in range(4):
"""회전: 0, 1, 2, 3 후 점수"""
# 1) 초기화
groups = []
groupsnum =[]
# 2) 그룹 만들고, 점수 계산, rotate
group()
answer += score_compute()
rotate()
print(answer)
정답 코드 💻
728x90
'취준! ✒ > 삼성' 카테고리의 다른 글
[코테준비] 삼성16. 코드트리: 포탑 부수기 python (1) | 2023.10.07 |
---|---|
[코테준비] 삼성15. 코드트리: 팩맨 python (1) | 2023.10.05 |
[코테준비] 삼성13. 코드트리: 메이즈 러너 python (2) | 2023.10.04 |
[코테준비] 삼성12. 코드트리: 나무박멸 python (0) | 2023.10.04 |
[코테준비] 11. 백준- 주사위 윷놀이 (17825) python "삼성 SW 역량 테스트 기출 문제" (0) | 2023.09.25 |