IT/coding study 44

[PCCP 기출문제] 4번 / 수레 움직이기 (python)

import sysfrom collections import dequedirs = [(0, 1), (1, 0), (-1, 0), (0, -1)]board = []red,blue=[],[]N,M=0,0answer=sys.maxsizevisit=[]# 백트래킹def dfs(red,blue,count): global answer #종료지점 도착했는지 if board[red[0]][red[1]]==3 and board[blue[0]][blue[1]]==4: answer=min(answer,count) # 파란 수레가 이미 도착했다면 if board[blue[0]][blue[1]]==4: for i in range(4): rnr,rnc=red[0]+dir..

IT/coding study 2025.03.09

[acmicpc] 16933. 벽 부수고 이동하기 3(python)

2차원 배열을 사용하여 구현하였습니다. 벽 부수고 이동하기 2 : https://seyeon-hello.tistory.com/62 import sys from collections import deque input=sys.stdin.readline N,M,K=map(int,input().split()) arr=[list(map(int,input().strip())) for _ in range(N)] dirs=[(1,0),(0,1),(-1,0),(0,-1)] q=deque() q.append((0,0,0,1,1)) #가로,세로,벽,이동,낮 visit=[[sys.maxsize]*M for _ in range(N)] visit[0][0]=0 def bfs(): while q: r,c,cnt,move,day=q..

IT/coding study 2023.08.22

[acmicpc] 14442. 벽 부수고 이동하기 2(python)

3차원 배열을 사용 안하는 방법으로 구현하였습니다. 아래 블로그를 참고했습니다. https://hongjw1938.tistory.com/163 import sys from collections import deque input=sys.stdin.readline n,m,k=map(int,input().split()) arr=[list(map(int,input().strip())) for _ in range(n)] dirs=[(1,0),(0,1),(-1,0),(0,-1)] visit=[[sys.maxsize for _ in range(m)] for _ in range(n)] q=deque() q.append((0,0,0,1)) visit[0][0]=1 answer=-1 while q: r,c,cnt,move..

IT/coding study 2023.08.12

[acmicpc] 13913. 숨바꼭질 4(python)

리스트에 방문했던 위치를 계속 누적했을 경우 메모리초과가 나와서, 딕셔너리를 통해 해당 위치의 전 위치를 저장하는 방법으로 구현하였습니다. import sys from collections import deque, defaultdict input=sys.stdin.readline N,K=map(int,input().split()) visit=[0]*100001 q=deque() dicts=defaultdict(int) q.append(N) while q: num=q.popleft() if num==K: print(visit[num]) answer=[] while num!=N: answer.append(num) num=dicts[num] answer.append(N) answer.reverse() prin..

IT/coding study 2023.03.28

[acmicpc] 1938. 통나무 옮기기(python)

필자는 통나무 모든 좌표를 방문체크를 했는데, 타 블로그 코드들을 확인해보니 중심좌표만 방문체크를 하고 있었다. 이 점을 개선해나가면 좋을 것 같다. import sys from collections import deque input=sys.stdin.readline N=int(input()) board=[list(input().strip()) for _ in range(N)] visit=[[[0]*2 for _ in range(N)] for _ in range(N)] dirs=[(1,0),(0,1),(-1,0),(0,-1),[(1,0),(0,0),(-1,0)],[(0,1),(0,0),(0,-1)]] slog=[] dlog=[] for r in range(N): for c in range(N): if b..

IT/coding study 2023.03.05