미확인도착지 -> 다익스트라로하면 경로가 1개만 저장되고 다른 경로를 체크할수 없음.
'sw > 알고리즘' 카테고리의 다른 글
백준 6987 월드컵 (0) | 2024.01.13 |
---|---|
백준 11501 주식 (0) | 2023.03.25 |
백준 16235 나무 재테크 (0) | 2023.01.28 |
백준 17070 파이프 옮기기1 (0) | 2023.01.24 |
프로그래머스 다리를 지나는 트럭 (0) | 2022.12.19 |
미확인도착지 -> 다익스트라로하면 경로가 1개만 저장되고 다른 경로를 체크할수 없음.
백준 6987 월드컵 (0) | 2024.01.13 |
---|---|
백준 11501 주식 (0) | 2023.03.25 |
백준 16235 나무 재테크 (0) | 2023.01.28 |
백준 17070 파이프 옮기기1 (0) | 2023.01.24 |
프로그래머스 다리를 지나는 트럭 (0) | 2022.12.19 |
A = []
B = deque(A.sort()) #불가능
B = deque(sorted(A)) #가능
#봄: 나이만큼 양분 먹고 / 나이 1 증가 / 여러 나무가 있으면 어린나무부터 양분먹기 / 양분없으면 사망
#여름: 죽은나무 나이 나누기2 만큼 양분추가(소수점 버림)
#가을: 5의 배수 나이인 경우 나무 번식 / 인접한 8개 칸에 나이 1인 나무 생성
#겨울: 주어진 양분 배급
#이동
dx=[-1, 0, 1,-1, 1,-1, 0, 1]
dy=[-1,-1,-1, 0, 0, 1, 1, 1]
for i in range(8):
nx=r+dx[i]
ny=c+dy[i]
if 0<=nx<n and 0<=ny<n:
tree[nx][ny].appendleft(1)
import sys
from collections import deque
sys.stdin = open("input.txt", "r")
n, m, k = map(int,sys.stdin.readline().split())
A=[False for _ in range(n)]
for _ in range(n):
A[_]=list(map(int,sys.stdin.readline().split()))
#현재 양분 값 저장하는 배열
B=[[5 for i in range(n)] for j in range(n)]
#트리 배열 생성
#트리 배열 각 노드의 값 == [1나무의 나이,2나무의 나이,..., x나무의 나이]
tree=[[[] for c in range(n)] for r in range(n)]
for _ in range(m):
x,y,z = map(int,sys.stdin.readline().split())
tree[x-1][y-1].append(z)
#처음 한번만 정렬
for i in range(n):
for j in range(n):
tree[i][j] = deque(sorted(tree[i][j]))
#이동
dx=[-1, 0, 1,-1, 1,-1, 0, 1]
dy=[-1,-1,-1, 0, 0, 1, 1, 1]
for y in range(k):
sons=[] #번식하는 나무 담아둘 곳.
for r in range(n):
for c in range(n):
cnt_tree = len(tree[r][c]) #현재 땅의 나무 개수 체크
if cnt_tree ==0:
B[r][c]+=A[r][c]
continue
#봄
tmp=deque()
die=0
for age in tree[r][c]:
if age > B[r][c]:# 잔여 양분 체크
die+= age//2 #죽은나무 나이 나누기2만큼 양분추가(소수점 버림)
continue
B[r][c]-=age
tmp.append(age+1)
tree[r][c]=tmp
#여름
B[r][c] += die
#가을
for t in tree[r][c]:
if t%5 == 0: sons.append((r,c))
#겨울
B[r][c]+=A[r][c]
#가을 적용
for son in sons:
(r,c) = son
for i in range(8):
nx=r+dx[i]
ny=c+dy[i]
if 0<=nx<n and 0<=ny<n:
tree[nx][ny].appendleft(1)
#나무 개수 카운트
ans =0
for i in range(n):
for j in range(n):
ans += len(tree[i][j])
print(ans)
백준 11501 주식 (0) | 2023.03.25 |
---|---|
백준 미확인도착지 (0) | 2023.03.10 |
백준 17070 파이프 옮기기1 (0) | 2023.01.24 |
프로그래머스 다리를 지나는 트럭 (0) | 2022.12.19 |
백준 2583 영역구하기 (0) | 2022.12.11 |
import sys
n= int(sys.stdin.readline())
graph= [list(map(int,sys.stdin.readline().split())) for _ in range(n)]
n-=1
ans = 0
def dfs(r,c,state):
global ans
if r==n and c == n:
ans += 1
return
if state==0:
if c < n and graph[r][c+1]==0:
dfs(r,c+1,0)
if r < n and graph[r+1][c]==0 and graph[r+1][c+1]==0: dfs(r+1,c+1,2)
elif state==1:
if r < n and graph[r+1][c]==0:
dfs(r+1,c,1)
if c < n and graph[r][c+1]==0 and graph[r+1][c+1]==0: dfs(r+1,c+1,2)
elif state==2:
if c < n and graph[r][c+1]==0: dfs(r,c+1,0)
if r < n and graph[r+1][c]==0: dfs(r+1,c,1)
if c < n and r < n and graph[r+1][c+1]==0 and graph[r+1][c] ==0 and graph[r][c+1] ==0: dfs(r+1,c+1,2)
dfs(0,1,0)
print(ans)
import sys
sys.stdin = open("input.txt", "r")
n= int(sys.stdin.readline())
graph= [list(map(int,sys.stdin.readline().split())) for _ in range(n)]
dp=[[[0 for _ in range(3)] for _ in range(n)]for _ in range(n)]
dp[0][1] = [1,0,0]
for i in range(2,n):
if graph[1][i]==0:
dp[0][i][0] = dp[0][i-1][0]
for r in range(1,n):
for c in range(1,n):
if graph[r][c]==0:
dp[r][c][0] = dp[r][c-1][0] + dp[r][c-1][2]
dp[r][c][1] = dp[r-1][c][1] + dp[r-1][c][2]
if graph[r][c-1] == 0 and graph[r-1][c]==0:
dp[r][c][2] = dp[r-1][c-1][0] + dp[r-1][c-1][1] +dp[r-1][c-1][2]
print(sum(dp[n-1][n-1]))
백준 미확인도착지 (0) | 2023.03.10 |
---|---|
백준 16235 나무 재테크 (0) | 2023.01.28 |
프로그래머스 다리를 지나는 트럭 (0) | 2022.12.19 |
백준 2583 영역구하기 (0) | 2022.12.11 |
백준 1012 유기농배추 (0) | 2022.12.11 |
import collections
def solution(bridge_length, weight, truck_weights):
answer =0
truck_que = collections.deque(truck_weights)
ing_que =collections.deque()
#초기화
time =1
ing_que.append((truck_que.popleft(),1)) #1초에 들어왔음.
while ing_que:
time +=1
#다리 지난 것은 q에서 빼주기
if time - ing_que[0][1] == bridge_length:
ing_que.popleft()
#무게 여분 남을 경우 트럭 추가로 올림
if truck_que and sum(i for i,j in ing_que) + truck_que[0] <= weight:
ing_que.append((truck_que.popleft(),time))
return time
백준 16235 나무 재테크 (0) | 2023.01.28 |
---|---|
백준 17070 파이프 옮기기1 (0) | 2023.01.24 |
백준 2583 영역구하기 (0) | 2022.12.11 |
백준 1012 유기농배추 (0) | 2022.12.11 |
백준 3986 좋은 단어 (0) | 2022.12.07 |
https://www.acmicpc.net/problem/2583
import sys
import collections
sys.stdin=open("input.txt","rt")
m, n, k = map(int,sys.stdin.readline().split())
graph = [[0]*n for _ in range(m)] # m행 n열
visited = [[0]*n for _ in range(m)]
for _ in range(k):
x1,y1,x2,y2 = map(int,sys.stdin.readline().split()) #사각형 정보 입력
for x in range(n):
for y in range(m):
if x>= x1 and x<= x2-1 and y >= y1 and y <= y2-1 :
graph[y][x] =1 # 행/열을 좌표계로 올리면 y,x 꼴이 된다.
cnt =0
dx =[1,0,-1,0]
dy =[0,1,0,-1]
q = collections.deque()
a = []
for i in range(m): #여기부턴 행/열 좌표계 이므로 위쪽 코드와 m n 순서가 다르다.
for j in range(n):
if graph[i][j]==0 and visited[i][j]==0:
cnt+=1
visited[i][j] =1
a.append(1)
q.append((i,j))
while q:
x, y = q.popleft()
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if nx >= 0 and nx < m and ny >= 0 and ny < n and graph[nx][ny]==0 and visited[nx][ny] ==0:
visited[nx][ny]=1
a[cnt-1] += 1
q.append((nx,ny))
print(cnt)
print(*sorted(a))
백준 17070 파이프 옮기기1 (0) | 2023.01.24 |
---|---|
프로그래머스 다리를 지나는 트럭 (0) | 2022.12.19 |
백준 1012 유기농배추 (0) | 2022.12.11 |
백준 3986 좋은 단어 (0) | 2022.12.07 |
백준 1940 주몽 (0) | 2022.12.07 |
https://www.acmicpc.net/problem/1012
import sys
import collections
T = int(sys.stdin.readline())
for _ in range(T):
m, n, k = map(int,sys.stdin.readline().split())
visited =[[False]*m for _ in range(n)]
graph =[[False]*m for _ in range(n)]
cnt = 0
for _ in range(k):
x,y = map(int,sys.stdin.readline().split())
graph[y][x] =1
dx=[1,0,-1,0]
dy=[0,1,0,-1]
for i in range(n):
for j in range(m):
if graph[i][j] == 1 and visited [i][j] == 0:
#발견하면 cnt+1, 그리고 그 주변을 모두 방문 시킨다.
cnt +=1
q= collections.deque()
q.append((i,j))
while q:
x, y = q.popleft()
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if nx < n and ny < m and nx >=0 and ny >= 0 and graph[nx][ny] == 1 and visited [nx][ny] == 0:
visited[nx][ny] = 1
q.append((nx,ny))
print(cnt)
프로그래머스 다리를 지나는 트럭 (0) | 2022.12.19 |
---|---|
백준 2583 영역구하기 (0) | 2022.12.11 |
백준 3986 좋은 단어 (0) | 2022.12.07 |
백준 1940 주몽 (0) | 2022.12.07 |
백준 1213 팰린드롬 만들기 (0) | 2022.12.07 |
https://www.acmicpc.net/problem/3986
import sys
import collections
n = int(sys.stdin.readline().strip())
cnt=0
q = collections.deque()
for _ in range(n):
input=sys.stdin.readline().strip()
for s in input:
if len(q) ==0:
q.append(s)
elif q[-1] ==s:
q.pop()
else:
q.append(s)
if len(q) ==0: #좋은 단어 성공일 경우 cnt + 1
cnt+=1
q.clear()
else:
q.clear()
print(cnt)
백준 2583 영역구하기 (0) | 2022.12.11 |
---|---|
백준 1012 유기농배추 (0) | 2022.12.11 |
백준 1940 주몽 (0) | 2022.12.07 |
백준 1213 팰린드롬 만들기 (0) | 2022.12.07 |
백준 2178 미로탐색 (0) | 2022.12.03 |
https://www.acmicpc.net/problem/1940
답은 맞았지만 시간 효율성이 낮았던 풀이
import sys
import itertools
n = int(sys.stdin.readline().strip())
m = int(sys.stdin.readline().strip())
lst = list(map(int,input().split()))
combi = itertools.combinations(lst,2)
result=0
for i in combi:
if i[0]+i[1] == m:
result +=1
print(result)
import sys
n = int(sys.stdin.readline().strip())
m = int(sys.stdin.readline().strip())
lst = list(map(int,sys.stdin.readline().split()))
lst.sort()
i_start = 0
i_end = len(lst)-1
cnt =0
while i_start < i_end:
value = lst[i_start] + lst[i_end]
if value ==m:
cnt+=1
i_start +=1
i_end -=1
elif value < m:
i_start +=1
elif value > m:
i_end -= 1
print(cnt)
백준 1012 유기농배추 (0) | 2022.12.11 |
---|---|
백준 3986 좋은 단어 (0) | 2022.12.07 |
백준 1213 팰린드롬 만들기 (0) | 2022.12.07 |
백준 2178 미로탐색 (0) | 2022.12.03 |
백준 2468 안전영역 (0) | 2022.12.03 |