๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2024.02.20 ์ฝ”๋”ฉ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2024๋…„ 2์›” 20์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
32/34
post-thumbnail

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฐ”ํƒ•ํ™”๋ฉด ์ •๋ฆฌํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…

์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•˜๋Š” ๋จธ์“ฑ์ด๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‚˜์ค‘์— ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•˜๋ ค๊ณ  ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์ปดํ“จํ„ฐ ๋ฐ”ํƒ•ํ™”๋ฉด์— ์•„๋ฌด ์œ„์น˜์—๋‚˜ ์ €์žฅํ•ด ๋‘ก๋‹ˆ๋‹ค. ์ €์žฅํ•œ ์ฝ”๋“œ๊ฐ€ ๋งŽ์•„์ง€๋ฉด์„œ ๋จธ์“ฑ์ด๋Š” ๋ณธ์ธ์˜ ์ปดํ“จํ„ฐ ๋ฐ”ํƒ•ํ™”๋ฉด์ด ๋„ˆ๋ฌด ์ง€์ €๋ถ„ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋Š” ๊ทธ ๋ฌธ์ œ์— ๊ฐ€์„œ ๋‹ค์‹œ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ €์žฅํ•ด ๋‘” ํŒŒ์ผ๋“ค์„ ์ „๋ถ€ ์‚ญ์ œํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ปดํ“จํ„ฐ ๋ฐ”ํƒ•ํ™”๋ฉด์€ ๊ฐ ์นธ์ด ์ •์‚ฌ๊ฐํ˜•์ธ ๊ฒฉ์žํŒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์ปดํ“จํ„ฐ ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด ๋ฐฐ์—ด wallpaper๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ํŒŒ์ผ๋“ค์€ ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ๊ฒฉ์ž์นธ์— ์œ„์น˜ํ•˜๊ณ  ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ๊ฒฉ์ž์ ๋“ค์€ ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„๋ฅผ (0, 0)์œผ๋กœ ์‹œ์ž‘ํ•ด (์„ธ๋กœ ์ขŒํ‘œ, ๊ฐ€๋กœ ์ขŒํ‘œ)๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๋นˆ์นธ์€ ".", ํŒŒ์ผ์ด ์žˆ๋Š” ์นธ์€ "#"์˜ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋“œ๋ž˜๊ทธ๋ฅผ ํ•˜๋ฉด ํŒŒ์ผ๋“ค์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ , ์„ ํƒ๋œ ํŒŒ์ผ๋“ค์„ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จธ์“ฑ์ด๋Š” ์ตœ์†Œํ•œ์˜ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ–๋Š” ํ•œ ๋ฒˆ์˜ ๋“œ๋ž˜๊ทธ๋กœ ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•ด์„œ ํ•œ ๋ฒˆ์— ์ง€์šฐ๋ ค๊ณ  ํ•˜๋ฉฐ ๋“œ๋ž˜๊ทธ๋กœ ํŒŒ์ผ๋“ค์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋“œ๋ž˜๊ทธ๋Š” ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ๊ฒฉ์ž์  S(lux, luy)๋ฅผ ๋งˆ์šฐ์Šค ์™ผ์ชฝ ๋ฒ„ํŠผ์œผ๋กœ ํด๋ฆญํ•œ ์ƒํƒœ๋กœ ๊ฒฉ์ž์  E(rdx, rdy)๋กœ ์ด๋™ํ•œ ๋’ค ๋งˆ์šฐ์Šค ์™ผ์ชฝ ๋ฒ„ํŠผ์„ ๋–ผ๋Š” ํ–‰๋™์ž…๋‹ˆ๋‹ค. ์ด๋•Œ, "์  S์—์„œ ์  E๋กœ ๋“œ๋ž˜๊ทธํ•œ๋‹ค"๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์  S์™€ ์  E๋ฅผ ๊ฐ๊ฐ ๋“œ๋ž˜๊ทธ์˜ ์‹œ์ž‘์ , ๋์ ์ด๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

  • ์  S(lux, luy)์—์„œ ์  E(rdx, rdy)๋กœ ๋“œ๋ž˜๊ทธ๋ฅผ ํ•  ๋•Œ, "๋“œ๋ž˜๊ทธ ํ•œ ๊ฑฐ๋ฆฌ"๋Š” |rdx - lux| + |rdy - luy|๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

  • ์  S์—์„œ ์  E๋กœ ๋“œ๋ž˜๊ทธ๋ฅผ ํ•˜๋ฉด ๋ฐ”ํƒ•ํ™”๋ฉด์—์„œ ๋‘ ๊ฒฉ์ž์ ์„ ๊ฐ๊ฐ ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๋กœ ํ•˜๋Š” ์ง์‚ฌ๊ฐํ˜• ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ํŒŒ์ผ์ด ์„ ํƒ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด wallpaper = [".#...", "..#..", "...#."]์ธ ๋ฐ”ํƒ•ํ™”๋ฉด์„ ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
eg1.png
์ด๋Ÿฌํ•œ ๋ฐ”ํƒ•ํ™”๋ฉด์—์„œ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด S(0, 1)์—์„œ E(3, 4)๋กœ ๋“œ๋ž˜๊ทธํ•˜๋ฉด ์„ธ ๊ฐœ์˜ ํŒŒ์ผ์ด ๋ชจ๋‘ ์„ ํƒ๋˜๋ฏ€๋กœ ๋“œ๋ž˜๊ทธ ํ•œ ๊ฑฐ๋ฆฌ (3 - 0) + (4 - 1) = 6์„ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
eg1-2.png
(0, 0)์—์„œ (3, 5)๋กœ ๋“œ๋ž˜๊ทธํ•ด๋„ ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋•Œ ๋“œ๋ž˜๊ทธ ํ•œ ๊ฑฐ๋ฆฌ๋Š” (3 - 0) + (5 - 0) = 8์ด๊ณ  ์ด์ „์˜ ๋ฐฉ๋ฒ•๋ณด๋‹ค ๊ฑฐ๋ฆฌ๊ฐ€ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค.

๋จธ์“ฑ์ด์˜ ์ปดํ“จํ„ฐ ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด wallpaper๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ํŒŒ์ผ๋“ค์„ ํ•œ ๋ฒˆ์— ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์†Œํ•œ์˜ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ–๋Š” ๋“œ๋ž˜๊ทธ์˜ ์‹œ์ž‘์ ๊ณผ ๋์ ์„ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด์„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”. ๋“œ๋ž˜๊ทธ์˜ ์‹œ์ž‘์ ์ด (lux, luy), ๋์ ์ด (rdx, rdy)๋ผ๋ฉด ์ •์ˆ˜ ๋ฐฐ์—ด [lux, luy, rdx, rdy]๋ฅผ returnํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค wallpaper์˜ ๊ธธ์ด โ‰ค 50
  • 1 โ‰ค wallpaper[i]์˜ ๊ธธ์ด โ‰ค 50
    • wallpaper์˜ ๋ชจ๋“  ์›์†Œ์˜ ๊ธธ์ด๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • wallpaper[i][j]๋Š” ๋ฐ”ํƒ•ํ™”๋ฉด์—์„œ i + 1ํ–‰ j + 1์—ด์— ํ•ด๋‹นํ•˜๋Š” ์นธ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • wallpaper[i][j]๋Š” "#" ๋˜๋Š” "."์˜ ๊ฐ’๋งŒ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
  • ๋ฐ”ํƒ•ํ™”๋ฉด์—๋Š” ์ ์–ด๋„ ํ•˜๋‚˜์˜ ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋“œ๋ž˜๊ทธ ์‹œ์ž‘์  (lux, luy)์™€ ๋์  (rdx, rdy)๋Š” lux < rdx, luy < rdy๋ฅผ ๋งŒ์กฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

wallpaper result
[".#...", "..#..", "...#."] [0, 1, 3, 4]
["..........", ".....#....", "......##..", "...##.....", "....#....."] [1, 3, 5, 8]
[".##...##.", "#..#.#..#", "#...#...#", ".#.....#.", "..#...#..", "...#.#...", "....#...."] [0, 0, 7, 9]
["..", "#."] [1, 0, 2, 1]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์„ค๋ช…์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. (0, 1)์—์„œ (3, 4)๋กœ ๋“œ๋ž˜๊ทธ ํ•˜๋ฉด ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ  ๋“œ๋ž˜๊ทธ ํ•œ ๊ฑฐ๋ฆฌ๋Š” 6์ด์—ˆ๊ณ , 6๋ณด๋‹ค ์ ์€ ๊ฑฐ๋ฆฌ๋กœ ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [0, 1, 3, 4]๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ์˜ˆ์ œ 2๋ฒˆ์˜ ๋ฐ”ํƒ•ํ™”๋ฉด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    eg2.png

    (1, 3)์—์„œ (5, 8)๋กœ ๋“œ๋ž˜๊ทธํ•˜๋ฉด ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ณ  ์ด๋ณด๋‹ค ์ ์€ ์ด๋™๊ฑฐ๋ฆฌ๋กœ ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ์ ์€ ์ด๋™์˜ ๋“œ๋ž˜๊ทธ๋กœ ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ [1, 3, 5, 8]์„ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ์˜ˆ์ œ 3๋ฒˆ์˜ ๋ฐ”ํƒ•ํ™”๋ฉด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    eg3.png

    ๋ชจ๋“  ํŒŒ์ผ์„ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด์„  ๋ฐ”ํƒ•ํ™”๋ฉด์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„ (0, 0)์—์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ (7, 9)๋กœ ๋“œ๋ž˜๊ทธ ํ•ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [0, 0, 7, 9]๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #4

  • ์˜ˆ์ œ 4๋ฒˆ์˜ ๋ฐ”ํƒ•ํ™”๋ฉด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด 2ํ–‰ 1์—ด์—๋งŒ ์•„์ด์ฝ˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.

    eg4.png

    ์ด๋ฅผ ๋“œ๋ž˜๊ทธ๋กœ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ทธ ์นธ์˜ ์™ผ์ชฝ ์œ„ (1, 0)์—์„œ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ (2, 1)๋กœ ๋“œ๋ž˜๊ทธ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. (1, 0)์—์„œ (2, 2)๋กœ ๋“œ๋ž˜๊ทธ ํ•ด๋„ ์•„์ด์ฝ˜์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด์ „๋ณด๋‹ค ์ด๋™๊ฑฐ๋ฆฌ๊ฐ€ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ [1, 0, 2, 1]์„ returnํ•ฉ๋‹ˆ๋‹ค.


์†Œ๋งˆ ํ…Œ์ŠคํŠธ๊ฐ€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์ง„ํ–‰๋œ๋‹ค๊ธธ๋ž˜ ๋ง› ์ข€ ๋ณด๋ ค๊ณ  ๋†€๋Ÿฌ์™”์Šต๋‹ˆ๋‹ค.
์–ด๋ ค์šธ ๊ฒƒ ์—†๋Š” ๊ฐ„๋‹จํ•œ ๋ฆฌ์ŠคํŠธ ํ™œ์šฉ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

wallpaper์—์„œ #์ด ๊ฐ€์žฅ ๋นจ๋ฆฌ ๋‚˜์˜ค๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์ฒซ๋ฒˆ์งธ x, ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” ์ธ๋ฑ์Šค๊ฐ€ ๋‘๋ฒˆ์งธ x, wallpaper์˜ ์›์†Œ๋“ค ์ค‘์—์„œ #์ด ๊ฐ€์žฅ ๋น ๋ฅธ ์œ„์น˜์— ์žˆ๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์ฒซ๋ฒˆ์งธ y, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” ์ธ๋ฑ์Šค๊ฐ€ ๋‘๋ฒˆ์งธ y๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

from collections import deque

def solution(wallpaper):
    x = []
    y = []
    sum = 0

deque ํ•˜๋‚˜ import ํ•˜๊ณ  ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    wallpaper = deque(wallpaper)
    while wallpaper:
        tmp = wallpaper.popleft()
        if '#' in tmp:
            x.append(sum)

popleft๋ฅผ ํ†ตํ•ด ๊ฐœ๋ณ„ ์›์†Œ๋งˆ๋‹ค ์ ‘๊ทผํ•˜๊ณ 
์œ„์— ์–ธ๊ธ‰ํ•œ๋Œ€๋กœ wallpaper์˜ ์š”์†Œ๋“ค์ค‘์— #์ด ์žˆ๋‹ค๋ฉด sum๊ฐ’์„ ํ™œ์šฉํ•ด์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ x ๋ฆฌ์ŠคํŠธ์— ์‹น ๋‹ค ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.


        for values in tmp:
            if values == '#':
                y.append(sum2)
            sum2 += 1
        sum += 1

๊ฐœ๋ณ„ ์›์†Œ ์•ˆ์— #์ด ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ์œ„์น˜๋ฅผ y๋ฆฌ์ŠคํŠธ์— ์‹น ๋‹ค ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

    return [min(x),min(y),max(x)+1,max(y)+1]

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ’ return ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋‹จ์–ด ๋ณ€ํ™˜
๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์„ ์ด์šฉํ•˜์—ฌ begin์—์„œ target์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฐ€์žฅ ์งง์€ ๋ณ€ํ™˜ ๊ณผ์ •์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

1. ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ๋งŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
2. words์— ์žˆ๋Š” ๋‹จ์–ด๋กœ๋งŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด begin์ด "hit", target๊ฐ€ "cog", words๊ฐ€ ["hot","dot","dog","lot","log","cog"]๋ผ๋ฉด "hit" -> "hot" -> "dot" -> "dog" -> "cog"์™€ ๊ฐ™์ด 4๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๊ฐœ์˜ ๋‹จ์–ด begin, target๊ณผ ๋‹จ์–ด์˜ ์ง‘ํ•ฉ words๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ ๋ช‡ ๋‹จ๊ณ„์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ begin์„ target์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ
  • ๊ฐ ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 3 ์ด์ƒ 10 ์ดํ•˜์ด๋ฉฐ ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • words์—๋Š” 3๊ฐœ ์ด์ƒ 50๊ฐœ ์ดํ•˜์˜ ๋‹จ์–ด๊ฐ€ ์žˆ์œผ๋ฉฐ ์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • begin๊ณผ target์€ ๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” 0๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ
begin target words return
"hit" "cog" ["hot", "dot", "dog", "lot", "log", "cog"] 4
"hit" "cog" ["hot", "dot", "dog", "lot", "log"] 0
์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ #1
๋ฌธ์ œ์— ๋‚˜์˜จ ์˜ˆ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
target์ธ "cog"๋Š” words ์•ˆ์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.


def solution(begin, target, words):
global ans
words = list(sorted(words))
  

์ผ๋‹จ ์ •๋ ฌํ•œ ๊ฐ’๊ณผ ์ •๋ ฌํ•˜์ง€ ์•Š์€ ๊ฐ’์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ๋ถ€ํ„ฐ ํ•ด์ค์‹œ๋‹ค.

begin: hit target: cog words: [hot, dot, dog, lot, log, cog] return : 4 << ์˜ˆ์ œ 1๋ฒˆ
begin: hit target: cog words: [cog, log, lot, dog, dot, hot] return : 4

์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—์š”.

    def dfs(begin):
     global ans
     if begin in words:
         visited[words.index(begin)] = 1 

DFS ์ž‘์„ฑ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๋‹จ์–ด๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•ด์ค์‹œ๋‹ค.

        sum = 0
        for k in range(len(begin)):
            if begin[k] != target[k]:
                sum += 1
        if sum == 1:
            return ans

ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๋‹จ์–ด์™€ ํƒ€๊ฒŸ ๋‹จ์–ด๊ฐ€ ๋ฌธ์ž 1๊ฐœ ์ฐจ์ด๋กœ ํ‹€๋ฆฌ๋‹ค๋ฉด ํƒ€๊ฒŸ ๋‹จ์–ด๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ ์žฌ๊ท€๋ฅผ ๋๋‚ด์ค์‹œ๋‹ค.

        for i in range(len(words)):
            sum2 = 0
            for j in range(len(words[i])):
                if words[i][j] != begin[j]:
                    sum2 += 1
            if sum2 == 1 and not visited[i]:
                print(words[i])
                ans += 1
                dfs(words[i])
                return ans

words ๋ฆฌ์ŠคํŠธ์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ๋งŒ์•ฝ ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ๋‹จ์–ดํ•˜๊ณ  ๋ฌธ์ž 1๊ฐœ ์ฐจ์ด๋กœ ํ‹€๋ฆฐ ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ํƒ์ƒ‰์— ๋„ฃ์–ด ์žฌ๊ท€๋ฅผ ๋Œ๋ ค์ค์‹œ๋‹ค. ์ƒ๋‹จ ์ฝ”๋“œ์—์„œ ํƒ€์ผ“ ๋‹จ์–ด๋กœ ๊ฐ€๋Š” ๊ฒƒ์ด ์ฆ๋ช…๋˜์—ˆ๋‹ค๋ฉด(dfs(words[i]) ์•„๋ž˜ ๋ถ€๋ถ„) ans๋ฅผ return ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ๋๋‚ด์ฃผ๋„๋ก ํ•ฉ์‹œ๋‹ค.

    ans = 1
    visited = [0] * len(words)
    if target not in words:
        return 0
    else:
        return dfs(begin)

target์ด ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์—†์œผ๋ฉด ํƒ€๊ฒŸ๋‹จ์–ด๋ฅผ ์•„์˜ˆ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 0์„ return ํ•˜๊ณ  ์•„๋‹ˆ๋ผ๋ฉด dfs(begin)์—์„œ ๋‚˜์˜จ ans๊ฐ’์„ return ํ•˜๋ฉด ํƒ€๊ฒŸ๋‹จ์–ด๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ๋‹จ์–ด ์ˆ˜ ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.



ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋“ฑ๊ตฃ๊ธธ

10์–ต์ด ๋„˜๋Š” ์ˆ˜๋กœ ๋‚˜๋ˆ„๋ผ๋Š” ๊ฒƒ์„ ๋ณด์ž๋งˆ์ž ์•Œ์•˜์Šต๋‹ˆ๋‹ค.
๋„ค, DP ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๋ฌผ์›…๋ฉ์ด๋กœ๋Š” ํ†ตํ–‰์ด ๋ถˆ๊ฐ€ํ•˜๋ฉฐ ์ง‘์—์„œ ํ•™๊ต๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ์ ์˜ ๊ฐ’์„ ๋„์ถœํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.


๋ถ„์„

์ €๋Š” DP๋ฌธ์ œ๋Š” ๋ถ„์„์„ ์•ˆํ•˜๋ฉด ํ•ญ์ƒ ํ‘ธ๋Š” ๋ฐ ๋‚œ์ด๋„๊ฐ€ ์žˆ๋”๋ผ๊ณ ์š”.
ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์Ÿ์ ์ด ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ๋ฌผ์›…๋ฉ์ด๊ฐ€ 1์—ด/1ํ–‰์— ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

https://velog.io/@ghost_dog/%EA%B3%B5%EB%B6%80-2024.02.15-%EC%BD%94%EB%94%A9-%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4

์™œ 1ํ–‰/1์—ด์— ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€์‹œ๋ฉด ์ด ๊ฒŒ์‹œ๋ฌผ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๋ฉด ๋„์›€๋˜์‹ค ๊ฒ๋‹ˆ๋‹ค.

์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด 1ํ–‰๊ณผ 1์—ด์„ ์ „๋ถ€ 1๋กœ ์ดˆ๊ธฐํ™” ์‹œํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทผ๋ฐ ์–ด๋ผ? ๋ฌผ์›…๋ฉ์ด๊ฐ€ 1ํ–‰๊ณผ 1์—ด์— ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ด ์ดํ›„๋Š” ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ• ๊นŒ์š”?

์ •๋‹ต์€ ์žฅ์• ๋ฌผ์ด ์ƒ๊ธด ์ดํ›„์˜ 1ํ–‰ ๋˜๋Š” 1์—ด์€ ๋‹ค 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค ์ž…๋‹ˆ๋‹ค.
์™œ๋ƒํ•˜๋ฉด ์žฅ์• ๋ฌผ์ด ์ƒ๊ธด ์ดํ›„์˜ ๊ณต๊ฐ„์€ ์–ด์ฐจํ”ผ ์˜ค๋ฅธ์ชฝ ๋˜๋Š” ํ•˜๊ฐ•๋งŒ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋ฐฉ๋ฌธ์„ ํ•ด๋ด์•ผ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐ ์žˆ์–ด ์ „ํ˜€ ๋ฉ”๋ฆฌํŠธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (ํ•™๊ต๊ฐ€ ๋งจ ์•„๋ž˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ์œผ๋‹ˆ๊นŒ)


์ฝ”๋”ฉ

ํ•ด๋‹น ๋‚ด์šฉ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ฝ”๋”ฉ์„ ํ•ด๋ด…์‹œ๋‹ค.

def solution(m, n, puddles):
    graph = [[0]*m for _ in range(n)]
    
    for a in range(len(graph[0])):
        if [a+1,1] not in puddles:
            graph[0][a] = 1
        else:
            break
    for b in range(len(graph)):
        if [1,b+1] not in puddles: 
            graph[b][0] = 1
        else:
            break

ํ–‰,์—ด,๋ฌผ์›…๋ฉ์ด ์œ„์น˜๋ฅผ ๋ฐ›๊ณ  ํ–‰์—ด ํฌ๊ธฐ๋งŒํผ์˜ ์›์†Œ๊ฐ€ ์ „๋ถ€ 0์ธ ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•ด์ค์‹œ๋‹ค.
๋งŒ์•ฝ ์›…๋ฉ์ด๊ฐ€ ์—†๋Š” 1ํ–‰/1์—ด์ด๋ผ๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค๋Š” 1๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ 
ํ–‰์—ด ์ธ๋ฑ์Šค ๊ฐ’ 1๋กœ ์ดˆ๊ธฐํ™”์ค‘์— 1ํ–‰/1์—ด ์ž๋ฆฌ์— ์›…๋ฉ์ด๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค! ํ•˜๋ฉด break๋กœ ๋‚˜์™€์„œ ์›๋ž˜ ๊ฐ’์ธ 0์œผ๋กœ ๋ƒ…๋‘ก์‹œ๋‹ค.

    for i in range(len(graph)):
        for j in range(len(graph[0])):
            if i==0 or j==0:
                continue

์ด์ œ DP๋ฅผ ๋Œ๋ฆด ์ค€๋น„๋ฅผ ํ•ฉ์‹œ๋‹ค.
๊ฐ ์ธ๋ฑ์Šค๋งˆ๋‹ค ํƒ์ƒ‰์„ ํ•˜๋ฉด์„œ ๋งŒ์•ฝ 1ํ–‰ ๋˜๋Š” 1์—ด์— ์ ‘๊ทผํ–ˆ๋‹ค๋ฉด ์ ํ™”์‹์„ ์œ„ํ•œ ์ดˆ๊ธฐ๊ฐ’์ด๋ฏ€๋กœ ๊ฑด๋“ค์ง€ ๋ง๊ณ  continue๋ฅผ ํ•ด์ค์‹œ๋‹ค. (์–ด์ฐจํ”ผ 1ํ–‰ ๋˜๋Š” 1์—ด์€ ๊ฐ€๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ 1๊ฐ€์ง€๋ฐ–์— ์—†์œผ๋ฏ€๋กœ)

            elif [j+1,i+1] in puddles:
                continue

์›…๋ฉ์ด๋ผ๋ฉด ๊ฑด๋“ค์ง€ ๋ง๊ณ  continue ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋Ÿฌ๋ฉด ์ ํ™”์‹ ์ ์šฉ์ด ์•ˆ๋˜์–ด ์™ผ์ชฝ+์œ„์ชฝ ์ด์ „์œ„์น˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ์ ํ™”์‹์„ ์‚ฌ์šฉํ•  ๋•Œ ์ด์ „์œ„์น˜์˜ ์›…๋ฉ์ด๋Š” ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ’์ด ์•„์˜ˆ ์—†๋Š” ํšจ๊ณผ๋ฅผ ์ค๋‹ˆ๋‹ค.

            else:
                graph[i][j] = graph[i-1][j] + graph[i][j-1]

๋งˆ์ง€๋ง‰์œผ๋กœ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๋Š” ์ ํ™”์‹ ์ž๊ธฐ์ž์‹  ์ตœ๋‹จ๊ฑฐ๋ฆฌ = ์œ„์ธ๋ฑ์Šค ์ตœ๋‹จ๊ฑฐ๋ฆฌ + ์™ผ์ชฝ์ธ๋ฑ์Šค ์ตœ๋‹จ๊ฑฐ๋ฆฌ
๋ฅผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    return graph[n-1][m-1]%1000000007

์ถœ๋ ฅ์€ DP ์ดˆ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด 10์–ต์ธ๊ฐ€? ๋ฅผ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ๋ฌธ์ œ์— ๋ช…์‹œ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ ์ทจ์ง€๋Š” ์ข‹์€๋ฐ ์›…๋ฉ์ด ํ–‰ ์—ด์ด ๋’ค๋ฐ”๊ปด์„œ ๋“ค์–ด๊ฐ€๋Š” ๋“ฑ ์ข€ ๋ฏธํกํ•œ ๋ถ€๋ถ„์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.



profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€