Skip to content

Commit 657a8d3

Browse files
committed
add files
1 parent 4cbf71c commit 657a8d3

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

week16/bsw/13116_30번.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
input = sys.stdin.readline
3+
# input이 5만개라 readline 쓰는게 효율적일듯
4+
5+
T = int(input())
6+
7+
tree = {}
8+
9+
for i in range(1, 1024):
10+
tree[i] = i//2
11+
12+
#print(tree)
13+
for _ in range(T):
14+
a, b = map(int, input().split())
15+
answer = 0
16+
17+
ans_a = set()
18+
ans_b = set()
19+
20+
while a in tree:
21+
ans_a.add(a)
22+
a = tree[a]
23+
24+
while b in tree:
25+
ans_b.add(b)
26+
b = tree[b]
27+
28+
print(max(ans_a & ans_b)*10)
29+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
N,M,T = map(int, input().split())
2+
INF = 1e9
3+
graph = [list(map(int, input().split())) for _ in range(N)]
4+
times = [[INF for _ in range(M)] for _ in range(N)]
5+
times[0][0] = 0
6+
# print(times)
7+
# print('')
8+
9+
from collections import deque
10+
q=deque()
11+
12+
# r, c
13+
q.append((0, 0))
14+
answer2=1e9
15+
while q:
16+
r, c = q.popleft()
17+
18+
for dr, dc in (1,0), (0,1), (-1,0), (0,-1):
19+
nr, nc = r + dr, c + dc
20+
21+
if 0<=nr<N and 0<=nc<M:
22+
23+
if times[nr][nc] != 1e9:
24+
continue
25+
26+
if graph[nr][nc] == 1:
27+
continue
28+
29+
# 칼
30+
if graph[nr][nc] == 2:
31+
answer2 = times[r][c] + 1 + ((N-1)-nr) + ((M-1)-nc)
32+
33+
34+
q.append((nr,nc))
35+
times[nr][nc] = times[r][c] + 1
36+
37+
# print(graph)
38+
# print(times)
39+
40+
times[N-1][M-1] = min(times[N-1][M-1], answer2)
41+
42+
if times[N-1][M-1] > T:
43+
print('Fail')
44+
else:
45+
print(times[N-1][M-1])
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from collections import deque
2+
import sys
3+
input = sys.stdin.readline
4+
5+
T = int(input())
6+
7+
def check(x1, y1, x2, y2):
8+
if abs(x1 - x2) + abs(y1 - y2) > 1000:
9+
return False
10+
elif abs(x1 - x2) + abs(y1 - y2) <= 1000:
11+
return True
12+
13+
for _ in range(T):
14+
n = int(input())
15+
16+
point = []
17+
graph = {}
18+
19+
#정점
20+
for _ in range(n+2):
21+
x, y = map(int, input().split())
22+
point.append((x,y))
23+
24+
for i in range(n+2):
25+
x1, y1 = point[i]
26+
for j in range(n+2):
27+
if i==j:
28+
continue
29+
x2, y2 = point[j]
30+
31+
if check(x1, y1, x2, y2):
32+
if not (x1, y1) in graph:
33+
graph[(x1, y1)] = []
34+
graph[(x1, y1)].append((x2, y2))
35+
36+
37+
q = deque()
38+
visited = {}
39+
40+
q.append(point[0])
41+
for p in point:
42+
visited[p] = 0
43+
visited[point[0]] = 1
44+
45+
while q:
46+
x, y = q.popleft()
47+
48+
if (x, y) not in graph:
49+
continue
50+
for nx, ny in graph[(x, y)]:
51+
if visited[(nx, ny)] == 1:
52+
continue
53+
q.append((nx, ny))
54+
visited[(nx, ny)] = 1
55+
56+
if visited[point[-1]] == 1:
57+
print('happy')
58+
elif visited[point[-1]] == 0:
59+
print('sad')
60+
61+
62+
63+
64+
65+
66+
67+

0 commit comments

Comments
 (0)