코테 준비
-
[백준] 12852. 1로 만들기 2코테 준비/DP 2023. 2. 25. 00:23
pypy3로 해야지 통과됨 import sys from collections import deque n=int(sys.stdin.readline()) queue=deque() queue.append([n]) result=[] while queue: q=queue.popleft() i=q[0] if i==1: result=q break if i%3==0: queue.append([i//3]+q) if i%2==0: queue.append([i//2]+q) queue.append([i-1]+q) print(len(result)-1) result.reverse() print(*result)
-
[백준] 2667. 단지번호 붙이기코테 준비/DFS, BFS 2023. 2. 10. 01:18
from collections import deque dx=[-1,1,0,0] dy=[0,0,-1,1] def bfs(graph,x,y): cnt=1 # 1부터 시작임 0부터 아님 queue=deque() queue.append((x,y)) graph[x][y]=0 #이거 안해줘서 첫번째 오류남 while queue: x,y=queue.popleft() for i in range(4): nx=x+dx[i] ny=y+dy[i] if nx=n: continue if graph[nx][ny]==0: continue if graph[nx][ny]==1: graph[nx][ny]=0 queue.append((nx,ny)) cnt+=1 return cnt n=int(input()) graph=[[0]*n for _..
-
[백준] 16236. 아기상어 (BFS) -너무 어려움..꼭 다시풀기!!코테 준비/DFS, BFS 2023. 2. 9. 02:37
고려할 사항이 많아서 스스로 짠 코드는 이정도 밖에 안된다.. from collections import deque dx=[-1,1,0,0] dy=[0,0,-1,1] def bfs(graph,x,y,shark_size): distance=[[0]*n for _ in range(n)] #거리 측정 visited=[[0]*n for _ in range(n)] #방문여부 측정 queue=deque() queue.append((x,y)) visited[x][y]=1 #방문 처리 tmp=[] #먹을 수 있는 물고기 저장 while queue: x,y=queue.popleft() for i in range(4): nx=x+dx[i] #이동한 x좌표 ny=y+dy[i] #이동한 y좌표 if nx=n: continue..
-
[백준] 1389. 케빈 베이컨의 6단계 법칙코테 준비/DFS, BFS 2023. 2. 7. 02:55
from collections import deque def bfs(graph,start,visited): queue=deque([start]) visited[start]=True while queue: v=queue.popleft() for i in range(n+1): if graph[v][i]==1 and not visited[i]: queue.append(i) visited[i]=True result[i]=result[v]+1 #다음숫자로 이동할때마다 이전 숫자+1 n,m=map(int,input().split()) graph=[[0]*(n+1) for _ in range(n+1)] for i in range(m): a,b=map(int,input().split()) graph[a][b]=grap..
-
[백준] 10844. 쉬운 계단 수코테 준비/DP 2023. 2. 5. 02:26
첫 풀이(시간초과....) def stair(num): num=list(map(int,str(num))) for i in range(1,len(num)): k=abs(num[i]-num[i-1]) if k!=1: return False return True count=0 for i in range(10**(n-1),10**n): if stair(i)==True: count+=1 print(count) 정답(인터넷 참고함...) import sys n=int(sys.stdin.readline()) dp=[[0]*10 for _ in range(n+1)] for i in range(1,10):#n이 1일때 (한자리 수일때) dp[1][i]=1 #1~9 모두 계단수 for i in range(2,n+1): #..
-
[백준] 10026. 적록색약 (BFS)코테 준비/DFS, BFS 2023. 2. 4. 03:02
연속된 영역의 개수를 세는 문제이다 from collections import deque dx=[-1,1,0,0] dy=[0,0,-1,1] def bfs(graph,x,y): queue=deque() queue.append((x,y)) color=graph[x][y] graph[x][y]=0 while queue: x,y=queue.popleft() for i in range(4): nx=x+dx[i] ny=y+dy[i] if nx=n: continue if graph[nx][ny]==color: #현재 칸의 색깔과 상하좌우의 색이 같으면 graph[nx][ny]=0 #0으로 방문처리 queue.append((nx,ny)) n=int(input()) graph=[[0]*n for _ in range(n)..