반응형

Development/Algorithm 33

[이코테 2021] 5장 그래프 탐색 알고리즘 [실전문제] 미로 탈출

나의 풀이 n, m = map(int, input().split()) world = [] for _ in range(n): world.append(list(map(int,input()))) from collections import deque dr = [0, -1, 0, 1] dc = [1, 0, -1, 0] def bfs(r, c): q = deque() q.append([r,c]) while q: r, c = q.popleft() for i in range(4): nr = r + dr[i] nc = c + dc[i] if 0 = m: continue # 벽인 경우 무시 if graph[nx][ny] == 0: continue # 해당 노드를 처음 방문하는 경우에만 최단 거리 기록 if graph[nx..

[이코테 2021] 5장 그래프 탐색 알고리즘 [실전문제] 음료수 얼려 먹기

나의 풀이 n, m = map(int, input().split()) world = [] for _ in range(n): world.append(list(map(int, input()))) def dfs(r, c): if r =n or c = m: return False else: if world[r][c] == 0: world[r][c] = 1 dfs(r-1, c) dfs(r+1, c) dfs(r, c-1) dfs(r, c+1) return True else: return False cnt = 0 for r in range(n): for c in range(m): if dfs(r, c): cnt += 1 print(cnt) 답안 예시 # N, M을 공백을 기준으로 ..

[이코테 2021] 4장 구현 [실전문제] 게임 개발

나의 풀이 n, m = map(int, input().split()) a, b, d = map(int, input().split()) world = [] for _ in range(n): world.append(list(map(int, input().split()))) def turn_left(): global d if d == 0: d = 3 else: d -= 1 def visited(): global world world[a][b] = 2 cnt = 1 turn_cnt = 0 left_types = [[0,-1], [-1,0], [0,1], [1,0]] back_types = [[1,0], [0,-1], [-1,0], [0,1]] visited() while True: tmp_a, tmp_b = a..

[이코테 2021] 4장 구현 [실전문제] 왕실의 나이트

나의 풀이 position = input() r = int(position[1]) c = ord(position[0]) - ord('a') + 1 move_types = [[2,1], [2,-1], [-2,1], [-2,-1], [1,2], [-1,2], [1,-2], [-1,-2]] cnt = 0 for m in move_types: tmp_r = r + m[0] tmp_c = c + m[1] if 0 < tmp_r < 9 and 0 < tmp_c < 9: cnt += 1 print(cnt) 답안 예시 # 현재 나이트의 위치 입력받기 input_data = input() row = int(input_data[1]) column = int(ord(input_data[0])) - int(ord('a')) ..

반응형