Skip to content

Commit

Permalink
7/5 PS 특정 거리의 도시 찾기
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoiEungi committed Jul 5, 2021
1 parent a1c89ca commit 1b2c044
Show file tree
Hide file tree
Showing 580 changed files with 209,320 additions and 29 deletions.
Binary file modified choieungi/.DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions choieungi/.idea/choieungi.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion choieungi/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions choieungi/1260-DFS와 BFS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys
from collections import deque

from typing import List, Any

Input = sys.stdin.readline

n, m, start = map(int, Input().split())

graph = [[] for _ in range(n+1)]
visited = [False] * (n+1)
bfs_visited = [False] * (n+1)
for _ in range(m):
p, q = map(int, Input().split())
graph[p].append(q)
graph[q].append(p)


def recursion_dfs(graph, visited, start):
visited[start] = True
print(start, end=" ")
for i in graph[start]:
if not visited[i]:
recursion_dfs(graph, visited, i)


def stack_dfs(graph, start):
ans = [] # type: List[Any]
visited = [False] * (n + 1)
stack = [start]
while len(stack):
v = stack.pop()
if not visited[v]:
visited[v] = True
stack.extend(sorted(graph[v], reverse=True)) # 깊이가 같을 시, 번호가 낮은 노드를 우선 탐색
ans.append(v)
print(*ans)


def bfs(graph, visited, start):
q = deque([start])
visited[start] = True
while q:
v = q.popleft()
print (v, end=" ")
for i in graph[v]:
if not visited[i]:
q.append(i)
visited[i] = True


recursion_dfs(graph,visited,start)
print()
stack_dfs(graph,start)

bfs(graph,bfs_visited,start)
File renamed without changes.
34 changes: 34 additions & 0 deletions choieungi/NewThisIsCT/13_15 특정 거리의 도시 찾기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys
from collections import deque

Input = sys.stdin.readline

n, m, k, x = map(int, Input().split())
graph = [[] for _ in range(n+1)]
distance = [-1] * (n+1)

for _ in range(m):
a, b = map(int, Input().split())
graph[a].append(b)


def bfs(graph,distance, start):
distance[start] = 0
q = deque([start])
while len(q):
now = q.popleft()
for i in graph[now]:
if distance[i] == -1:
distance[i] = distance[now] + 1
q.append(i)

bfs(graph,distance, x)

flag = False
for i in range(n+1):
if distance[i] == k:
print(i)
flag = True

if not flag:
print(-1)
31 changes: 31 additions & 0 deletions choieungi/lv3_네트워크.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from collections import deque


def solution(n, computers):
answer = 0
visited = [False] * n

def dfs(graph, start, visited):
visited[start] = True
for i in range(n):
if not visited[i] and graph[start][i]:
dfs(graph, i, visited)

def bfs(graph, start, visited):
visited[start] = True
q = deque([start])
while len(q):
v = q.popleft()
for i in range(n):
if not visited[i] and graph[v][i]:
q.append(i)
visited[i] = True

for i in range(n):
if not visited[i]:
bfs(computers, i, visited)
answer += 1

return answer

print(solution(3, [[1, 1, 0], [1, 1, 0], [0, 0, 1]] ))
47 changes: 24 additions & 23 deletions choieungi/main.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import sys
from itertools import combinations
from collections import deque

Input = sys.stdin.readline

n, m = map(int, Input().split())
space = [Input().split() for _ in range(n)]
chicken = []
home = []
n, m, k, x = map(int, Input().split())
graph = [[] for _ in range(n+1)]
distance = [-1] * (n+1)

for _ in range(m):
a, b = map(int, Input().split())
graph[a].append(b)

def min_distance(case, home):
ans = 0
for p,q in home:
distance = int(1e9)
for x,y in case:
distance = min(distance, abs(p-x)+abs(q-y))
ans += distance
return ans

def bfs(graph,distance, start):
distance[start] = 0
q = deque([start])
while len(q):
now = q.popleft()
for i in graph[now]:
if distance[i] == -1:
distance[i] = distance[now] + 1
q.append(i)

for i in range(n):
for j in range(n):
if space[i][j] == '1': home.append((i,j))
elif space[i][j] == '2': chicken.append((i,j))

answer = int(1e9)
for case in combinations(chicken, m):
answer = min(answer, min_distance(case, home))

print(answer)
bfs(graph,distance, x)

flag = False
for i in range(n+1):
if distance[i] == k:
print(i)
flag = True

if not flag:
print(-1)
59 changes: 56 additions & 3 deletions choieungi/main2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
a = [[0,2],[5,6]]
def boost_dec2hex(number):
notation = "0123456789ABCDEF"
base = len(notation)
q, r = divmod(number, base)
n = notation[r]
return boost_dec2hex(q)+n if q else n # str type

b = [1,2]

print(list(map(lambda x: [x[0][0],x[0][1],x[1]], zip(a,b))))
def boost_dec2bin(number): # input is int type
ans =""
while number:
ans = str(number % 2) + ans
number //= 2

while len(ans) < 8:
ans = "0" + ans

return ans


def boost_bin2hex(number):
ans = 0
number = number[::-1]
for i in range(len(number)):
ans += (2**i)*int(number[i])
return "0x" + boost_dec2hex(ans) #return str


def LD_cmd(register, target, origin):
if target == origin : return "NOOP"
real_cmd = "01" + register[target] + register[origin]
return boost_bin2hex(real_cmd)

def LN_cnd(register, target, origin):
bin_val = boost_dec2bin(int(origin))
real_cmd = "00" + register[target] + "110" + bin_val
return boost_bin2hex(real_cmd)


def solution(param0):
if param0[0:2] not in ["LD", "LN"] or param0[2] != " " :
return "ERROR"
elif "," not in param0: return "ERROR"
# need to imple
register = {"A": "111", "B": "000", "C": "001", "D": "010", "E": "011", "H": "100", "L": "101"}


answer = ''
cmd, r = param0.split()
target, origin = r.split(",")
if target not in register.keys(): return "ERROR"
elif origin not in register.keys() and cmd == "LD": return "ERROR"

if cmd == "LD":
return LD_cmd(register, target, origin)

elif cmd == "LN":
return LN_cnd(register, target, origin)
Loading

0 comments on commit 1b2c044

Please sign in to comment.