본문 바로가기

분류 전체보기

(402)
[백준 4991번] 로봇 청소기 (Python3) import sys input = sys.stdin.readline from collections import deque dy = [1,-1,0,0] dx = [0,0,1,-1] INF = 10**6 def BFS(y,x,n): visited = [[0]*M for i in range(N)] dq = deque() dq.append((y,x,0)) while dq: y,x,d = dq.popleft() if visited[y][x]: continue visited[y][x] = 1 if room[y][x] == "*": n1 = numdict[(y,x)] graph[n][n1] = graph[n1][n] = d for i in range(4): y1,x1 = y+dy[i],x+dx[i] if N>y1>=..
[백준 18809번] Gaaaaaaaaaarden (Python3) import sys input = sys.stdin.readline from itertools import combinations from collections import deque dy = [1,-1,0,0] dx = [0,0,1,-1] def BFS(): flower = 0 while dq: y,x,ylast,xlast,time,color = dq.popleft() if visited[ylast][xlast] == 1: #전에 꽃이 폈으면 continue if visited[y][x]: if visited[y][x] == (time,-color): visited[y][x] = 1 flower += 1 continue visited[y][x] = (time,color) for i in range(4)..
[백준 23288번] 주사위 굴리기 2 (Python3) import sys input = sys.stdin.readline from collections import deque dy = [1,0,-1,0] dx = [0,1,0,-1] def roll(d): newdice = dice[:] if d == 0: for i in range(4): newdice[(i-1)%4] = dice[i] elif d == 1: newdice[0] = dice[5] newdice[5] = dice[2] newdice[2] = dice[4] newdice[4] = dice[0] elif d == 2: for i in range(4): newdice[(i+1)%4] = dice[i] else: newdice[5] = dice[0] newdice[2] = dice[5] newdic..
[백준 1486번] 등산 (Python3) import sys input = sys.stdin.readline from heapq import heappush,heappop INF = sys.maxsize dy = [1,-1,0,0] dx = [0,0,1,-1] def Djikstra(x): DP = [INF]*N*M hq = [] heappush(hq,(0,x)) while hq: w,now = heappop(hq) if DP[now] y1>=0 and M>x1>=0: h1 = MAP[y1][x1] if abs(h-h1) h: d = (h1-h)**2 graph[y*M+x].append((y1*M+x1,d)) DP = [INF]*N*M hq = [] heappush(hq,(0,0)) while hq: w,now = heappop(hq) if D..
[백준 1219번] 오민식의 고민 (Python3) 처음에는 너무 쉬운 문제라고 생각했다. 이전에 이미 음의 사이클의 존재 여부를 판별하는 웜홀이나 타임머신 같은 문제를 많이 풀어봤기에 "왜 이게 골드1이지?" 라고 생각했다. 그리고 N이 50밖에 안되는걸 보고 "왜 이렇게 N이 작지?" 라고 생각했다. 그러나 곧 정답률이 왜 15퍼밖에 안되는지 알 수 있었다. 처음에 벨만포드로 제출한 코드는 다음과 같다. import sys input = sys.stdin.readline INF = 10**9 def BF(): DP = [INF]*N DP[start] = 0 for n in range(N): for i in range(N): for j in range(N): if graph[i][j] == INF: continue if DP[j] > DP[i]+grap..
[백준 2836번] 수상 택시 (Python3) import sys input = sys.stdin.readline N,M = map(int,input().split()) taxi = [] for i in range(N): x,y = map(int,input().split()) if y>x: continue taxi.append((M-x,M-y)) taxi.sort() t = len(taxi) SUM = 0 x = 0 while x < t: start,end = taxi[x] next = x while next+1
[백준 19237번] 어른 상어 (Python3) import sys input = sys.stdin.readline dy = [-1,1,0,0] dx = [0,0,-1,1] def smellup(): global finish cnt = 0 for i in range(N): for j in range(N): if ocean[i][j]: smell[i][j] = [k,ocean[i][j]] cnt += 1 if cnt == 1: finish = True def smelldown(): for i in range(N): for j in range(N): if smell[i][j][0]: smell[i][j][0] -= 1 def moveshark(): newocean = [[0]*N for i in range(N)] for y in range(N): for ..
[백준 19236번] 청소년 상어 (Python3) import sys input = sys.stdin.readline from copy import deepcopy dy = [-1,-1,0,1,1,1,0,-1] dx = [0,-1,-1,-1,0,1,1,1] def movefish(n,ocean): y,x = where[n] d = ocean[y][x][1] for i in range(8): y1,x1 = y+dy[(i+d)%8],x+dx[(i+d)%8] if 4>y1>=0 and 4>x1>=0: if ocean[y1][x1]: if ocean[y1][x1][0] == -1: continue ocean[y][x] = ocean[y1][x1] where[ocean[y][x][0]] = (y,x) else: ocean[y][x] = 0 ocean[y1][x1..