https://softeer.ai/practice/info.do?eventIdx=1&psProblemId=577
Softeer
연습문제를 담을 Set을 선택해주세요. 취소 확인
softeer.ai
해결하지 못했던 문제를 해결하는 쾌감은 엄청나다는걸
다시 한번 느끼며?
import sys
from collections import deque
import copy
H, W = map(int,sys.stdin.readline().split())
graph = []
for i in range(H):
graph.append(list(sys.stdin.readline().rstrip()))
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def check_dir(x,y):
if x == -1:
return "^"
elif x == 1:
return "v"
elif y == -1:
return "<"
else:
return ">"
def search(graph):
start = []
for i in range(len(graph)):
for j in range(len(graph[i])):
count = 0
if graph[i][j] == "#":
for k in range(4):
x = i + dx[k]
y = j + dy[k]
if 0 <= x < H and 0 <= y < W:
if graph[x][y] == "#":
count += 1
if count == 1:
start.append([i,j])
start_dir = []
for i in range(len(start)):
x,y = start[i][0],start[i][1]
for j in range(4):
nx = x + dx[j]
ny = y + dy[j]
if 0 <= nx < H and 0 <= ny <W and graph[nx][ny] == "#":
direct = check_dir(dx[j],dy[j])
start_dir.append(direct)
return start, start_dir
def search_start(start, graph):
traking = []
q = deque()
graph1 = copy.deepcopy(graph)
q.append((start[0],start[1]))
while q:
x, y = q.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < H and 0 <= ny < W and graph1[nx][ny] == "#":
q.append((nx, ny))
graph1[x][y] = "."
direct=check_dir(dx[i], dy[i])
traking.append(direct)
return traking
start, start_dir = search(graph)
print(start[0][0]+1,start[0][1]+1)
print(start_dir[0])
for i in range(len(start)-1):
temp = []
traking = search_start(start[i], graph)
for j in range(0,len(traking),2):
temp.append(traking[j])
move = ["A"]
for j in range(1,len(temp)):
if temp[j] == temp[j-1]:
move.append("A")
else:
if temp[j-1] == "<" and temp[j]== "^" :
move.append("RA")
elif temp[j-1] == "<" and temp[j]== "v" :
move.append("LA")
elif temp[j-1] == "^" and temp[j]== ">" :
move.append("RA")
elif temp[j - 1] == "^" and temp[j] == "<" :
move.append("LA")
elif temp[j - 1] == ">" and temp[j] == "v" :
move.append("RA")
elif temp[j - 1] == ">" and temp[j] == "^" :
move.append("LA")
elif temp[j - 1] == "v" and temp[j] == ">" :
move.append("LA")
elif temp[j - 1] == "v" and temp[j] == "<" :
move.append("RA")
answer =""
for j in range(len(move)):
answer += move[j]
print(answer)
와우 엄청 고민하고 다풀었다!
코드 제출할때 예제로 제출하면 틀리다고 나오는데 예제의 답과 같아서 이상했지만
혹시몰라 제출했는데 정답으로 나왔다(맞는 코드입니다)
맞았다고 확신했기 대문에
시작 방향이 두방향이므로 위의 코드는
start, start_dir = search(graph)
print(start[0][0]+1,start[0][1]+1)
print(start_dir[0])
for i in range(len(start)-1):
temp = []
traking = search_start(start[i], graph)
이부분만 고치면 모든 경우 다 고려 가능하다 어디인지 모르겠는 분은 댓글을 달아주시면 답을 드리겠습니다!
차분히 풀면 해결 가능한 문제이다.
반응형
'코딩 > Softeer' 카테고리의 다른 글
[Softeer] 이미지 프로세싱- 파이썬 (0) | 2022.02.14 |
---|---|
[Softeer] 성적 평균 - 파이썬 (0) | 2022.02.07 |
[Softeer] 택배 마스터 광우 - 파이썬 (0) | 2022.02.07 |
[Softeer] 전광판 - 파이썬 (0) | 2022.02.07 |
[Softeer] 비밀메뉴 - 파이썬 (0) | 2022.02.06 |