๐Ÿ“๋ฌธ์ œ ํ’€์ด

    [Python] ๋ฐฑ์ค€ 18405 ๊ฒฝ์Ÿ์  ์ „์—ผ | BFS

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/18405 18405๋ฒˆ: ๊ฒฝ์Ÿ์  ์ „์—ผ ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N, K๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 200, 1 ≤ K ≤ 1,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์‹œํ—˜๊ด€์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ–‰์€ N๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ํ•ด๋‹น ์œ„์น˜ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํŠน์ • ์œ„์น˜๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ๋ป—์–ด๊ฐ€๋ฏ€๋กœ BFS๊ฐ€ ์ ์ ˆ - ์ˆซ์ž๊ฐ€ ์ž‘์€ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์šฐ์„ ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„ ์ •๋ ฌ ํ›„ ํ๋กœ ์ „ํ™˜ํ•œ๋‹ค! - ํ์— ๊ฐ’์ด ์—†์–ด์งˆ ๋•Œ๊นŒ์ง€ ๊บผ๋‚ด ์ƒํ•˜์ขŒ์šฐ์˜ ๊ฐ ๊ฐ’์ด ๋‹ค์‹œ ํ์— ๋„ฃ๋Š” ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. - ํ์—์„œ ๊บผ๋‚ธ ๊ฐ’์˜ ์‹œ๊ฐ„์ด S์™€ ๊ฐ™๋‹ค๋ฉด ์ข…๋ฃŒ - X-1 ํ–‰ ..

    [Python] ๋ฐฑ์ค€ 14502 ์—ฐ๊ตฌ์†Œ | BFS

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/14502 14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ ์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - 0์ธ ๊ฐ’์˜ ๋ชจ๋“  ์œ„์น˜๋ฅผ ๋ฆฌ์ŠคํŠธ (blank)์— ๋„ฃ์–ด์„œ cominations ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด 3๊ฐœ ์œ„์น˜ ๋žœ๋ค ์ถ”์ถœ(nCr) - ๋ฒ ์ด์Šค๊ฐ€ ๋˜๋Š” ๊ธฐ์กด ์ •๋ณด์— ํ•ด๋‹น ํ•˜๋Š” 3๊ฐœ์˜ ์œ„์น˜๋ฅผ 1๋กœ ์„ค์ •ํ•˜๊ณ  check ํ•จ์ˆ˜ ํ˜ธ์ถœ - check ํ•จ์ˆ˜ ~ BFS๋ฅผ ์œ„ํ•œ ํ๋ฅผ ์„ ์–ธํ•˜์—ฌ ๊ฐ’์ด 2์ธ ์œ„์น˜๋ฅผ ๋„ฃ๋Š”๋‹ค. ~ ํ์— ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด ํ•ด๋‹น ํ•˜๋Š” ์œ„์น˜์—์„œ ์œ„, ..

    [Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ž์—ด ์••์ถ• | 2020 KAKAO BLIND RECRUITMENT

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/60057 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ [Python] 2, 4, 7, 17, 18, 20 ์‹คํŒจ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ ๋‹ค๋ฅธ ๋ถ„์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ์ฐธ๊ณ ํ•˜์—ฌ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค ๋ณธ์ธ์€ ๋ฐ˜๋ณต ๋ฐœ์ƒ์ด 1์ด๋ผ๋„ ์ „๋ถ€ ๋ฌธ์ž์—ด์— ํฌํ•จํ•˜์—ฌ ์ดํ›„ '1'์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” 2์ž๋ฆฌ ์ด์ƒ์˜ ์นด์šด๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. '10' -> '0' '11' -> '' '12' -> '2' ... ..

    [Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ | Heap(ํž™) | 2019 KAKAO BLIND RECRUITMENT

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/42891 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๊ฐ€์žฅ ์ž‘์€ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ์Œ์‹ ๋ถ€ํ„ฐ ์ฒ˜๋ฆฌํ•œ๋‹ค. (heapq ์‚ฌ์šฉ) - ๋งŒ์•ฝ ๋ชจ๋“  ์Œ์‹์˜ ๊ฐœ์ˆ˜ ๋ณด๋‹ค k๊ฐ€ ํฌ๋‹ค๋ฉด ๋ชจ๋“  ์Œ์‹์„ ๋จน๊ณ ๋„ ์‹œ๊ฐ„์ด ๋‚จ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ -1 ๋ฐ˜ํ™˜ - ํ•œ๋ฒˆ์˜ ๋กœํ…Œ์ด์…˜์„ ์œ„ํ•ด ํ•„์š”ํ•œ ์Œ์‹ ๊ฐœ์ˆ˜๋Š”, 0์ด ์•„๋‹Œ ์Œ์‹์˜ ์ˆ˜์ด๋‹ค.(length) - (๋‹ค์Œ ์ตœ์†Œ ์Œ์‹ ๊ฐœ์ˆ˜) - (์ด์ „์— ์ฒ˜๋ฆฌํ•œ ์Œ์‹ ๊ฐœ์ˆ˜) = ํ˜„์žฌ ์ฒ˜๋ฆฌํ•ด์•ผ..

    [Python] ๋ฐฑ์ค€ 4485 ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€? | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/4485 4485๋ฒˆ: ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€? ์ ค๋‹ค์˜ ์ „์„ค ๊ฒŒ์ž„์—์„œ ํ™”ํ์˜ ๋‹จ์œ„๋Š” ๋ฃจํ”ผ(rupee)๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ„ํ˜น '๋„๋‘‘๋ฃจํ”ผ'๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฒ€์ •์ƒ‰ ๋ฃจํ”ผ๋„ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๊ฑธ ํš๋“ํ•˜๋ฉด ์˜คํžˆ๋ ค ์†Œ์ง€ํ•œ ๋ฃจํ”ผ๊ฐ€ ๊ฐ์†Œํ•˜๊ฒŒ ๋œ๋‹ค! ์ ค๋‹ค์˜ ์ „์„ค ์‹œ๋ฆฌ์ฆˆ์˜ ์ฃผ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์œ„์น˜๋ณ„ (0, 0)์—์„œ ์ตœ์†Œ ๋น„์šฉ์„ ๊ณ„์‚ฐ - dr, dc : ๊ฐ ์œ„์น˜์—์„œ ์•„๋ž˜, ์œ„, ์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉํ–ฅํ‚ค - graph : ๊ฐ ์œ„์น˜ ๋ฐฉ๋ฌธ์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋น„์šฉ - distance : ๊ฐ ์œ„์น˜์—์„œ (0, 0)๊นŒ์ง€ ๊ฐ€๊ธฐ์œ„ํ•œ ์ตœ์†Œ ๋น„์šฉ, ์ดˆ๊ธฐ๋Š” ๋ฌดํ•œ์ˆ˜ ์ž‘์„ฑ ์ฝ”๋“œ imp..

    [Python] ๋ฐฑ์ค€ 1504 ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1504 1504๋ฒˆ: ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, a๋ฒˆ ์ •์ ์—์„œ b๋ฒˆ ์ •์ ๊นŒ์ง€ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ์กด www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - v1 -> v2 ์ˆœ์„œ ๋˜๋Š” v2 -> v1 ์ˆœ์„œ ๊ฐ๊ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์—ฌ ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. (2๋ฒˆ์˜ ๋‹ค์ต์ŠคํŠธ๋ผ) - ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ(๊ฒฐ๊ณผ ๊ฐ’์ด INF๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ) -1์„ ์ถœ๋ ฅ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ฝ”๋“œ ์ฃผ์„ ์ฐธ๊ณ  ์ž‘์„ฑ ์ฝ”๋“œ import sys import heapq input =..

    [Python] ๋ฐฑ์ค€ 13549 ์ˆจ๋ฐ”๊ผญ์งˆ 3 | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/13549 13549๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 3 ์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 ≤ N ≤ 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 ≤ K ≤ 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ www.acmicpc.net ์ž‘์„ฑ ์ฝ”๋“œ import heapq INF = int(1e9) N, K = map(int, input().split()) # ์‹œ์ž‘ ์œ„์น˜, ๋„์ฐฉ ์œ„์น˜ distance = [INF]*100001 # 100001๊ฐœ์˜ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ def dijkstra(start): # ๋‹ค์ต์ŠคํŠธ๋ผ distance[start] = 0 # ์‹œ์ž‘..

    [Python] ๋ฐฑ์ค€ 1916 ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ | ๋‹ค์ต์ŠคํŠธ๋ผ | ์‹œ๊ฐ„์ดˆ๊ณผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1916 1916๋ฒˆ: ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ ์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ M+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋ฐ›์„ ์ž…๋ ฅ์ด ๋งŽ์œผ๋ฏ€๋กœ sys.stdin.realine ์„ ์‚ฌ์šฉํ•œ๋‹ค. (์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐ) - ์ „ํ˜•์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ ์œ ํ˜•์ด๋‹ค - ์šฐ์„ ์ˆœ์œ„ ํ(์ตœ์†Œํž™)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋…ธ๋“œ๋ฅผ ์„ ํƒ - distance : start ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๊ฐ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ (INF ๋กœ ์ดˆ๊ธฐํ™”) - graph : ๊ฐ ..

    [Python] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฐ€์žฅ ๋จผ ๋…ธ๋“œ | BFS | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/49189 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - BFS์™€ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‘ ๊ฐ€์ง€ ํ’€์ด ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐ (DFS๋„ ๊ฐ€๋Šฅํ•˜๋‚˜ python์€ DFS ํšจ์œจ์ด ์ข‹์ง€ ๋ชป ํ•จ) ์ž์„ธํ•œ ์„ค๋ช…์€ ์ฃผ์„ ์ฐธ๊ณ  ์ž‘์„ฑ ์ฝ”๋“œ BFS ํ’€์ด from collections import deque def solution(n, edge): que = deque() # BFS๋ฅผ ์œ„ํ•œ ํ graph = [[]..