728x90
๋ฐ˜์‘ํ˜•

BFS 5

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] DFS/BFS level2 ํƒ€๊ฒŸ๋„˜๋ฒ„

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ์ฝ”๋”ฉ์—ฐ์Šต ์ค‘ level2์˜ DFS/BFS ์นดํ…Œ๊ณ ๋ฆฌ์˜ "ํƒ€๊ฒŸ๋„˜๋ฒ„" ๋ฌธ์ œ ํ’€์ด (๋ฌธ์ œ : ์•„๋ž˜ ๋งํฌ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œํ’€์ด ์ฒ˜์Œ ์ ‘๊ทผ ๋ฐฉ์‹ (์ •ํ™•์„ฑ 75%, ํ…Œ์ผ€ 1,2๋ฒˆ ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ํ†ต๊ณผ ๋ชปํ•จ) ๋‚˜์˜ ๊ฒฝ์šฐ๋Š” ์ฒ˜์Œ ๋‘ ํ…Œ์ผ€์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค. ์ ‘๊ทผ ๋ฐฉ์‹์€ ์ฃผ์–ด์ง„ numbers ๊ธธ์ด๋งŒํผ '+','-'๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์ƒ์„ฑํ•ด์ฃผ๊ณ  ์ด๋ฅผ numbers์— ๋ถ™์—ฌ์ฃผ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. (itertools์˜ productํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ) ์˜ˆ๋ฅผ ๋“ค์–ด, [4,1,2,1] ๊ฐ€ numbers๋กœ ์ฃผ์–ด์ง„๋‹ค๋ฉด, '+'์™€ '..

[๋ฐฑ์ค€ 1012] [dfs/bfs] ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

๐Ÿ’ป ๋ฐฑ์ค€ 1012๋ฒˆ [์œ ๊ธฐ๋† ๋ฐฐ์ถ”] 1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ” ์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— www.acmicpc.net ํ’€์ด) import sys sys.setrecursionlimit(100000000) T = int(input()) cnt_lst = [] for t in range(T) : m,n,k = map(int,input().split()) cab = [list(map(int,input().split())) for _ in range(k)] map_ = [[0]*m for _ in range(n)] for i,j in cab : map_[j][i..

[๋ฐฑ์ค€ 1012] [dfs/bfs] ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

๐Ÿ’ป ๋ฐฑ์ค€ 1012๋ฒˆ [์œ ๊ธฐ๋† ๋ฐฐ์ถ”] 1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ” ์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— www.acmicpc.net ํ’€์ด) import sys sys.setrecursionlimit(100000000) T = int(input()) cnt_lst = [] for t in range(T) : m,n,k = map(int,input().split()) cab = [list(map(int,input().split())) for _ in range(k)] map_ = [[0]*m for _ in range(n)] for i,j in cab : map_[j][i..

[๋ฐฑ์ค€ 2178] [dfs/bfs] ๋ฏธ๋กœํƒ์ƒ‰

๐Ÿ’ป ๋ฐฑ์ค€ 2178๋ฒˆ [๋ฏธ๋กœํƒ์ƒ‰] ํ’€์ด) # ์ตœ์ข…์ œ์ถœ from collections import deque n,m = map(int,input().split()) miro = [list(map(int,input())) for _ in range(n)] visited = [[0]*m for _ in range(n)] dis = [[0]*m for _ in range(n)] q = deque([[0,0]]) visited[0][0] = 1 dis[0][0] = 1 while q : x,y = q.popleft() for [i,j] in [[x+1,y],[x,y+1],[x-1,y],[x,y-1]] : if 0

[๋ฐฑ์ค€ 2331] [dfs] ๋ฐ˜๋ณต์ˆ˜์—ด

๐Ÿ’ป ๋ฐฑ์ค€ 2331๋ฒˆ [๋ฐ˜๋ณต์ˆ˜์—ด] 2331๋ฒˆ: ๋ฐ˜๋ณต์ˆ˜์—ด ์ฒซ์งธ ์ค„์— ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ ์ œ์™ธํ–ˆ์„ ๋•Œ, ์ˆ˜์—ด์— ๋‚จ๊ฒŒ ๋˜๋Š” ์ˆ˜๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ํ’€์ด) ์ฒซ๋ฒˆ์งธ ์ œ์ถœ : ๋Ÿฐํƒ€์ž„์—๋Ÿฌ(ModuleNotFoundError) ๋œธ ์•Œ๊ณ ๋ณด๋‹ˆ ๋ฐฑ์ค€์—์„œ๋Š” numpy๋ฅผ ๋ชป ์“ฐ๊ฒŒ ํ•จ...;;^^ # ์ฒซ๋ฒˆ์งธ import numpy as np a,p = map(int,input().split()) visited = {} stack = [] def cal(a,p) : if a in visited : return stack.pop() else : visited[a] = 1 stack.append(a) return cal(np.sum([int(i)**p for i in str(a)]),p) dup_n = cal..

728x90
๋ฐ˜์‘ํ˜•