[programmers] ์•ˆ์ „์ง€๋Œ€ - Java

์ง€๋ขฐ ์ฐพ๊ธฐ ๋ฌธ์ œ์—์„œ ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ๊ณผ ์ธ์ ‘ํ•œ ์ง€์—ญ์„ ๋ชจ๋‘ ์œ„ํ—˜์ง€์—ญ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ , ์•ˆ์ „ํ•œ ์ง€์—ญ์˜ ์นธ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•ด 2์ฐจ์› ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ, ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์œ„์น˜๋ฅผ ์ฐพ์•„ ์ƒ,ํ•˜,์ขŒ,์šฐ,๋Œ€๊ฐ์„ ์˜ ๊ฐ’์„ -1๋กœ ์„ค์ •ํ•œ๋‹ค. ๋ฐฐ์—ด์˜ ์œ„์น˜๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ฃผ์š” ๊ณ ๋ ค์‚ฌํ•ญ์ด๋‹ค.
Feb 02, 2024
[programmers] ์•ˆ์ „์ง€๋Œ€ - Java

๋ฌธ์ œ ์„ค๋ช…

๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ๊ณผ ์ง€๋ขฐ์— ์ธ์ ‘ํ•œ ์œ„, ์•„๋ž˜, ์ขŒ, ์šฐ ๋Œ€๊ฐ์„  ์นธ์„ ๋ชจ๋‘ ์œ„ํ—˜์ง€์—ญ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค.
notion image
์ง€๋ขฐ๋Š” 2์ฐจ์› ๋ฐฐ์—ด board์— 1๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๊ณ  board์—๋Š” ์ง€๋ขฐ๊ฐ€ ๋งค์„ค ๋œ ์ง€์—ญ 1๊ณผ, ์ง€๋ขฐ๊ฐ€ ์—†๋Š” ์ง€์—ญ 0๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
์ง€๋ขฐ๊ฐ€ ๋งค์„ค๋œ ์ง€์—ญ์˜ ์ง€๋„ board๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์•ˆ์ „ํ•œ ์ง€์—ญ์˜ ์นธ ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • board๋Š” n * n ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • 1 โ‰ค n โ‰ค 100
  • ์ง€๋ขฐ๋Š” 1๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • board์—๋Š” ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ 1๊ณผ ์ง€๋ขฐ๊ฐ€ ์—†๋Š” ์ง€์—ญ 0๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

board
result
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]
16
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0]]
13
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
0

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

์ž…์ถœ๋ ฅ ์˜ˆ #1
  • (3, 2)์— ์ง€๋ขฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ๊ณผ ์ง€๋ขฐ์™€ ์ธ์ ‘ํ•œ ์œ„, ์•„๋ž˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„  ์ด 8์นธ์€ ์œ„ํ—˜์ง€์—ญ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 16์„ returnํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #2
  • (3, 2), (3, 3)์— ์ง€๋ขฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ง€๋ขฐ๊ฐ€ ์žˆ๋Š” ์ง€์—ญ๊ณผ ์ง€๋ขฐ์™€ ์ธ์ ‘ํ•œ ์œ„, ์•„๋ž˜, ์ขŒ, ์šฐ, ๋Œ€๊ฐ์„ ์€ ์œ„ํ—˜์ง€์—ญ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„ํ—˜์ง€์—ญ์„ ์ œ์™ธํ•œ ์นธ ์ˆ˜ 13์„ returnํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ #3
  • ๋ชจ๋“  ์ง€์—ญ์— ์ง€๋ขฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์•ˆ์ „์ง€์—ญ์€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 0์„ returnํ•ฉ๋‹ˆ๋‹ค.

solution.java

class Solution { public int solution(int[][] board) { int n = board.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (board[i][j] == 1) { markAdjacentCells(board, i, j); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (board[i][j] == 0) { count++; } } } return count; } private void markAdjacentCells(int[][] board, int row, int col) { int n = board.length; int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; for (int[] dir : directions) { int newRow = row + dir[0]; int newCol = col + dir[1]; if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < n && board[newRow][newCol] == 0) { board[newRow][newCol] = -1; } } } }
 

ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ

  • ์ด์ค‘ for๋ฌธ์„ ํ†ตํ•ด 2์ฐจ์› ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์ฝ์–ด์˜จ ๊ฐ’์ด 1์ด๋ผ๋ฉด ์ƒ,ํ•˜,์ขŒ,์šฐ,๋Œ€๊ฐ์„ ์˜ ๊ฐ’์„ -1๋กœ ์„ค์ •ํ•œ๋‹ค.
  • ๋งŒ์•ฝ ์ƒ,ํ•˜,์ขŒ,์šฐ,๋Œ€๊ฐ์„ ์˜ ์œ„์น˜๊ฐ€ ๋ฐฐ์—ด์˜ ์œ„์น˜๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ, ํ•ด๋‹น ์œ„์น˜๋Š” ์ ‘๊ทผํ•˜์ง€ ์•Š๋Š”๋‹ค.
 

๊ฒฐ๋ก !

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ค๊ฐ€ ๋ฐฐ์—ด์˜ ์œ„์น˜๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์—์„œ ์–ด๋ ค์›€์„ ๊ฒช์—ˆ๋‹ค.
 
Share article

More articles

See more posts

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ปDriedPollack's Blog