๐Ÿ“๋ฌธ์ œ ํ’€์ด/๐ŸงฉBaekjoon

    [Python] ๋ฐฑ์ค€ 11404 ํ”Œ๋กœ์ด๋“œ | ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/11404 11404๋ฒˆ: ํ”Œ๋กœ์ด๋“œ ์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ n์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ m์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ m+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค์˜ ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ๊ฐ€ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - graph : 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋…ธ๋“œ ๋ณ„ ๊ฐ ๋…ธ๋“œ๊นŒ์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•œ๋‹ค. graph[i][j] ๋Š” i์—์„œ j๋กœ ๊ฐ€๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์ด๋™ ๊ฑฐ๋ฆฌ๋ฅผ ์˜๋ฏธํ•จ - ๊ฐ ๊ฐ„์„  ์ •๋ณด๋ฅผ ๋”ฐ๋ผ ๋ฌดํ•œ์ˆ˜(INF)๋กœ ์ดˆ๊ธฐํ™” ํ•œ graph๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. ๋™์ผํ•œ ๋…ธ์„ ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ„์„ ์ด ์žˆ์Œ์œผ๋กœ ์ฃผ์˜ ํ•„์š” - a -> b ๋…ธ์„ ์— ๋Œ€..

    [Python] ๋ฐฑ์ค€ 18352 ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ | ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/18352 18352๋ฒˆ: ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ ์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N, ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M, ๊ฑฐ๋ฆฌ ์ •๋ณด K, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 300,000, 1 ≤ M ≤ 1,000,000, 1 ≤ K ≤ 300,000, 1 ≤ X ≤ N) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋‘ ๊ฐœ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜ - distance : X๋กœ๋ถ€ํ„ฐ ๊ฐ ๋…ธ๋“œ์˜ ๊ฐ€์žฅ ์งง์€ ๊ฒฝ๋กœ. ๊ฒฝ๋กœ๊ฐ€ ์—†๋‹ค๋ฉด ๋ฌดํ•œ์ˆ˜(INF) - graph : ๊ฐ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ •๋ณด (์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ) - q : ์ง€๊ธˆ๊นŒ์ง€ ๊ณ„์‚ฐ ๋œ ๊ฑฐ๋ฆฌ ์ค‘ ๊ฐ„์„ ์ด ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์„ ..

    [Python] ๋ฐฑ์ค€ 1753 ์ตœ๋‹จ ๊ฒฝ๋กœ | ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1753 1753๋ฒˆ: ์ตœ๋‹จ๊ฒฝ๋กœ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) ๋ชจ๋“  ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์‹œ์ž‘ ์ •์ ์˜ ๋ฒˆํ˜ธ K(1 ≤ K ≤ V)๊ฐ€ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋Œ€ํ‘œ์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ - ์‹œ์ž‘๋…ธ๋“œ๋ถ€ํ„ฐ ๊ฐ ๋…ธ๋“œ๊นŒ์ง€ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด(distance)์„ ๋ฌดํ•œ์ˆ˜(INF)๋กœ ์ดˆ๊ธฐํ™” - ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ graph๋ฅผ ์„ ์–ธํ•˜์—ฌ ๊ฐ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€์™€ ๋ฌด๊ฒŒ๋ฅผ ํŠœ๋ธ”ํ˜•ํƒœ๋กœ ์ €์žฅ : graph[i][0]์€ i์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ์˜๋ฏธํ•˜๊ณ  ..

    [Python] ๋ฐฑ์ค€ 1463 1๋กœ ๋งŒ๋“ค๊ธฐ | DP

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1463 1463๋ฒˆ: 1๋กœ ๋งŒ๋“ค๊ธฐ ์ฒซ์งธ ์ค„์— 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 106๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ์ „ํ˜•์ ์ธ ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ์ด๋‹ค. - ๊ฐ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ตœ์†Œ ์—ฐ์‚ฐ์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅ ๋ฐ›์€ N ํฌ๊ธฐ๋กœ ์ƒ์„ฑ. - ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐ”ํ…€์—…(BottomUp) : ๋ฐ˜๋ณต๋ฌธ ํƒ‘๋‹ค์šด(TopDown) : ์žฌ๊ท€ ๋ฐ”ํ…€์—… ๋ฐฉ์‹์ด ์Šคํƒ ํฌ๊ธฐ ์ œํ•œ์œผ๋กœ๋ถ€ํ„ฐ ์ž์œ ๋กœ์›€์œผ๋กœ, ์ฒ˜์Œ ์—ฐ์‚ฐ์„ ์ €์žฅํ•˜์—ฌ ์ดํ›„ ๋™์ผํ•œ ์—ฐ์‚ฐ ๋ฐœ์ƒ์‹œ ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ์—ฐ์‚ฐ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค. - ์ตœ์ข…์ ์œผ๋กœ 1์„ ๋งŒ๋“œ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋ฏ€๋กœ 2๋ถ€ํ„ฐ ๊ตฌํ•˜๋ ค๋Š” n๊นŒ์ง€ ๊ฐ ์ˆ˜๋ฅผ ..

    [Python] ๋ฐฑ์ค€ 9663 N-Queen | ์‹œ๊ฐ„์ดˆ๊ณผ

    ์ •๋‹ต์€ ์•Œ๋งž๊ฒŒ ๋‚˜์˜ค๋Š”๋ฐ ์ž๊พธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๋ถ„๋“ค์€ ์–ธ์–ด๋ฅผ Python3๊ฐ€ ์•„๋‹Œ Pypy3๋กœ ์„ค์ •ํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ ์ „ํ˜•์ ์ธ Backtracking ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค! https://www.acmicpc.net/problem/9663 9663๋ฒˆ: N-Queen N-Queen ๋ฌธ์ œ๋Š” ํฌ๊ธฐ๊ฐ€ N × N์ธ ์ฒด์ŠคํŒ ์œ„์— ํ€ธ N๊ฐœ๋ฅผ ์„œ๋กœ ๊ณต๊ฒฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋†“๋Š” ๋ฌธ์ œ์ด๋‹ค. N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ€ธ์„ ๋†“๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ 1. row[i] : i๋ฒˆ์งธ ํ–‰์—์„œ ํ€ธ์˜ ์—ด ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” 1์ฐจ์› ๋ฐฐ์—ด row๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„ ๋ฐฉ์ง€ 2. promising ํ•จ์ˆ˜ : ๋Œ€๊ฐ์„ ์— ์œ„์น˜ํ•˜๊ฑฐ๋‚˜ ๊ฐ™์€ ์—ด์— ์œ„์น˜ํ•˜๋Š”..

    [Python] ๋ฐฑ์ค€ 10819 ์ฐจ์ด๋ฅผ ์ตœ๋Œ€๋กœ | ์ˆœ์—ด/์กฐํ•ฉ | Permutations/Combinations

    ~์‹ค๋ฒ„ 2~ ๋ฐฑํŠธ๋ž˜ํ‚น(Backtracking) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•™์Šตํ•˜๋Š” ๊ณผ์ •์—์„œ ๋งŒ๋‚œ ๋ฐฑ์ค€ ๋ฌธ์ œ! https://www.acmicpc.net/problem/10819 10819๋ฒˆ: ์ฐจ์ด๋ฅผ ์ตœ๋Œ€๋กœ ์ฒซ์งธ ์ค„์— N (3 ≤ N ≤ 8)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋ฐฐ์—ด A์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๋Š” -100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. www.acmicpc.net [์ตœ์ข… ์ฝ”๋“œ] from itertools import permutations n = int(input()) alist = list(map(int, input().split())) maxN = 0 nPr = permutations(alist) for p in nPr: temp = 0 for i in range(n-1): ..

    [Python] ๋ฐฑ์ค€ 2089 -2์ง„์ˆ˜

    ~์‹ค๋ฒ„ 2~ [ํ•ด๊ฒฐ ํฌ์ธํŠธ] 1. 2์ง„์ˆ˜์ฒ˜๋Ÿผ -2์˜ ๋‚˜๋จธ์ง€๋ฅผ ์ €์žฅํ•˜์—ฌ ์ถœ๋ ฅ 2. ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ์•„๋‹ ๋•Œ +1 [์ตœ์ข… ์ฝ”๋“œ] n = int(input()) result = [] if n == 0: # ์ž…๋ ฅ ๊ฐ’์ด 0์ผ ๋•Œ print(0) else: while n != 0: temp = n % -2 result.append(-temp) n //= -2 if temp: # ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ๋ฉด n += 1 for r in reversed(result): print(r, end="") ์˜ˆ์ œ ์ž…๋ ฅ์ฒ˜๋Ÿผ -13์ด ๋“ค์–ด์™”๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž REFERENCE https://suri78.tistory.com/119 [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜] 2089๋ฒˆ: -2์ง„์ˆ˜ -Python [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜] 2089๋ฒˆ: -2์ง„์ˆ˜ -Python http..

    [Python] ๋ฐฑ์ค€ 1107 ๋ฆฌ๋ชจ์ปจ | EOFError | ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๊ณจ๋“œ5~ EOFError๊ฐ€ ๋– ์„œ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ–ˆ๋Š”๋ฐ, ๋‹ค์Œ ๋Œ“๊ธ€์„ ๋ณด๊ณ  ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค..! ๊ฐ™์€ ์˜ค๋ฅ˜๋กœ ๊ณ ์ƒํ•˜์‹œ๋Š” ๋ถ„์€ ์ฐธ๊ณ  ํ•˜์…”์š”~ [์ตœ์ข… ์ฝ”๋“œ] target = int(input()) brokenButtonCount = int(input()) if brokenButtonCount == 0: # ๊ณ ์žฅ๋‚œ ๋ฒ„ํŠผ์ด ์—†๋Š” ๊ฒฝ์šฐ print(min(abs(100 - target), len(str(target)))) # 100์—์„œ +/- vs target ๋ฒ„ํŠผ ๋ˆ„๋ฅด๊ธฐ else: brokenButton = list(map(int, input().split())) minN = abs(100 - target) # +/- ๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋™ํ•œ ๊ฒฝ์šฐ for num in range(1000001): # ์ฑ„๋„ ์ œํ•œ์€..

    [Python] ๋ฐฑ์ค€ 16924 ์‹ญ์ž๊ฐ€ ์ฐพ๊ธฐ | ์™„์ „ ํƒ์ƒ‰ | ๋ธŒ๋ฃจํŠธํฌ์Šค(Brute-force)

    ~์‹ค๋ฒ„ 2~ https://www.acmicpc.net/problem/16924 16924๋ฒˆ: ์‹ญ์ž๊ฐ€ ์ฐพ๊ธฐ ์‹ญ์ž๊ฐ€๋Š” ๊ฐ€์šด๋ฐ์— '*'๊ฐ€ ์žˆ๊ณ , ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ์œผ๋กœ ๋ชจ๋‘ ๊ฐ™์€ ๊ธธ์ด์˜ '*'๊ฐ€ ์žˆ๋Š” ๋ชจ์–‘์ด๋‹ค. ์‹ญ์ž๊ฐ€์˜ ํฌ๊ธฐ๋Š” ๊ฐ€์šด๋ฐ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ์œผ๋กœ ์žˆ๋Š” '*'์˜ ๊ฐœ์ˆ˜์ด๋‹ค. ์‹ญ์ž๊ฐ€์˜ ํฌ๊ธฐ๋Š” 1๋ณด๋‹ค ํฌ www.acmicpc.net [๊ตฌํ˜„ ํฌ์ธํŠธ] ๋”๋ณด๊ธฐ 1. ์ œ๊ณต๋œ ์ž…๋ ฅ ์™ธ ์‹ญ์ž๊ฐ€ ๋ฐœ๊ฒฌ์‹œ '*'์„ '.'์œผ๋กœ ๋ณ€๊ฒฝํ•  ๊ณต๊ฐ„ deepcopy 2. ์™„์ „ํƒ์ƒ‰ํ•˜๋ฉฐ '*' ๋ฐœ๊ฒฌ์‹œ ์ธ๋ฑ์Šค๋ฅผ ๋„“ํ˜€ ์‹ญ์ž ๋ชจ์–‘ ํ™•์ธ 3. 2๋ฅผ ์‹ญ์ž ๋ชจ์–‘์ผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต - ๋ฐ˜๋ณต๋งˆ๋‹ค ์‚ฌ์ด์ฆˆ += 1 - ๋ฆฌ์ŠคํŠธํ˜• ๋ณ€์ˆ˜์— (i, j, size) ์ถ”๊ฐ€ - ์‹ญ์ž ๋ชจ์–‘์— ๋Œ€ํ•ด '.' ์ฒ˜๋ฆฌ 4. ์™„์ „ํƒ์ƒ‰ ํ›„ ์‹ญ์ž๋ชจ์–‘์œผ๋กœ '.'์ฒ˜๋ฆฌํ•œ ๊ณต๊ฐ„์— '*'๊ฐ€ ์žˆ..