stack = []
stack.append(5) #push 5
stack.append(2) #push 2
stack.append(3) #push 3
stack.append(7) #push 7
stack.pop()
stack.append(1) #push 1
stack.append(4) #push 4
stack.pop()
print(stack[::-1]) #์ต์๋จ ์์๋ถํฐ ์ถ๋ ฅ
print(stack) #์ตํ๋จ ์์๋ถํฐ ์ถ๋ ฅ
from collections import deque
queue = deque()
queue.append(5)
queue.append(2)
queue.append(3)
queue.append(7)
queue.popleft()
queue.append(1)
queue.append(4)
queue.popleft()
print(queue) # ๋จผ์ ๋ค์ด์จ ์์๋๋ก ์ถ๋ ฅ.
queue.reverse() # ์ญ์์ผ๋ก ๋ฐ๊พธ๊ธฐ
print(queue) # ๋์ค์ ๋ค์ด์จ ์์๋ถํฐ ์ถ๋ ฅ.
def factorial_iterative(n): # ๋ฐ๋ณต์ ์ผ๋ก ๊ตฌํ
result = 1
for i in range(1, n+1):
result = result * i
return result
def factorial_recursive(n): # ์ฌ๊ท์ ์ผ๋ก ๊ตฌํ
if n<=1:
return 1
else :
return n*factorial_recursive(n-1)
print('๋ฐ๋ณต์ ์ผ๋ก ๊ตฌํ:', factorial_iterative(5))
print('์ฌ๊ท์ ์ผ๋ก ๊ตฌํ:', factorial_recursive(5))
def gcd(a,b):
if a % b == 0:
return b
else :
return gcd(b,a % b)
print(gcd(192,162))
def dfs(graph, v ,visited):
visited[v] = True #๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
print(v, end=' ')
for i in graph[v]:
if visited[i] == False: #์์ง ๋ฏธ๋ฐฉ๋ฌธ์ด๋ผ๋ฉด
dfs(graph, i, visited)
graph = [
[],
[2,3,8],
[1,7],
[1,4,5],
[3,5],
[3,4],
[7],
[2,6,8],
[1,7]
]
visited = [False] * 9
dfs(graph, 1, visited) # ์์ ๋
ธ๋=1
from collections import deque
def bfs(graph, start, visited):
queue = deque([start])
visited[start] = True # ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
while queue: #ํ๊ฐ ๋น ๋๊น์ง
v = queue.popleft() # ์์ ์์๋๋ก
print(v, end =' ')
for i in graph[v]: # ์ธ์ ๋
ธ๋๋ค์ ๋ํด
if not visited[i]:
queue.append(i)
visited[i] = True # ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
graph = [
[],
[2,3,8],
[1,7],
[1,4,5],
[3,5],
[3,4],
[7],
[2,6,8],
[1,7]
]
visited = [False] * 9
bfs(graph, 1, visited) # ์์ ๋
ธ๋=1
def dfs(x,y):
if x < -1 or x >= N or y <= -1 or y >= M:
return False
if graph[x][y] == 0: #๋ฏธ๋ฐฉ๋ฌธ์
graph[x][y] = 1 #๋ฐฉ๋ฌธ์ฒ๋ฆฌ
dfs(x-1,y)
dfs(x,y-1)
dfs(x+1,y)
dfs(x,y+1)
return True
else:
return False #[x][y]๊ฐ ๋ฐฉ๋ฌธ ๋
ธ๋์ธ ๊ฒฝ์ฐ
N,M = map(int,input().split())
graph = []
for i in range(N):
graph.append(list(map(int,input())))
result = 0
for i in range(N):
for j in range(M):
if dfs(i,j) == True:
result += 1
print(result)
from collections import deque
def bfs(x,y):
queue = deque()
queue.append((x,y))
while queue:
x,y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= n or ny < 0 or ny >= m:
continue
if graph[nx][ny] == 0: #๋ฐฉ๋ฌธ์์น์ด๋ฉด ์๊ฐ
continue
if graph[nx][ny] == 1: #๋ฏธ๋ฐฉ๋ฌธ์์น์ด๋ฉด ๊ฐ
graph[nx][ny] = graph[x][y] + 1
queue.append((nx,ny))
return graph[n-1][m-1]
n,m = map(int,input().split())
graph = []
for i in range(n):
graph.append(list(map(int,input())))
# ์ด๋ ๋ฐฉํฅ [์,ํ,์ข,์ฐ]
dx = [-1,1,0,0]
dy = [0,0,-1,1]
print(bfs(0,0))
728x90
':: Python ๐ฉ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๋ฉ] [๋ฐฑ์ค 2884 python] (0) | 2023.01.19 |
---|---|
[์ฝ๋ฉ] [๋ฐฑ์ค 14681 python] (0) | 2023.01.19 |
[์ฝ๋ฉ] [๋ฐฑ์ค 10808 python] (0) | 2023.01.19 |
๋๊ธ