complete 27323 and aloha bfs(1260, 1697, 14940)

This commit is contained in:
2024-03-25 19:30:38 +09:00
parent 00bbedd83c
commit be0b5e12ba
4 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
#include <iostream>
using namespace std;
int main() {
int A, B;
cin >> A >> B;
cout << A * B;
return 0;
}

View File

@@ -0,0 +1,45 @@
def dfs(N, M, E, V):
D = []
D.append((V))
visited = []
while D:
now = D.pop()
if now in visited:
continue
else:
visited.append(now)
for i in E[now][::-1]:
D.append(i)
return visited
def bfs(N, M, E, V):
D = []
D.append((V))
visited = []
while D:
now = D.pop(0)
if now in visited:
continue
else:
visited.append(now)
for i in E[now]:
D.append(i)
return visited
if __name__ == '__main__':
N, M, V = map(int, input().split())
E = {}
for i in range(1, N + 1):
E[i] = []
for i in range(1, M + 1):
a, b = map(int, input().split())
E[a].append(b)
E[b].append(a)
for i in range(1, N + 1):
E[i].sort()
print(*dfs(N, M, E, V))
print(*bfs(N, M, E, V))

View File

@@ -0,0 +1,36 @@
def solve(N, M, X, dest):
delta = ((-1, 0), (1, 0), (0, 1), (0, -1))
D = []
# X = [[-1] * M for _ in range(N)]
D.append((dest, 0))
while D:
now, cost = D.pop(0)
if X[now[0]][now[1]] == -1:
X[now[0]][now[1]] = cost
else:
continue
for dx, dy in delta:
new = now[0] + dx, now[1] + dy
if 0 <= new[0] < N and 0 <= new[1] < M:
D.append((new, cost + 1))
return X
if __name__ == '__main__':
N, M = map(int, input().split())
U = []
X = [[-1] * M for _ in range(N)]
dest: tuple
for i in range(N):
temp = list(map(int, input().split()))
for j, v in enumerate(temp):
if v == 0:
X[i][j] = 0
elif v == 2:
dest = (i, j)
for i in solve(N, M, X, dest):
print(*i)

View File

@@ -0,0 +1,33 @@
from queue import Queue
def solve(N, K):
D = Queue()
M = [0] * 100001
D.put((N, 0))
while D:
now, t = D.get()
if now > 100000 or now < 0:
continue
if M[now] == 0:
M[now] = t
elif t > M[now]:
continue
target = []
if now > K:
target.append((now - 1, t + 1))
elif now == K:
return t
elif now < K:
target.append((2 * now, t + 1))
target.append((now + 1, t + 1))
target.append((now - 1, t + 1))
for temp in target:
D.put(temp)
if __name__ == '__main__':
N, K = map(int, input().split())
print(solve(N, K))