좀더 발전된 문제인거같은데 방식은 선생님께서 알려주신 방법으로 문제를 풀어볼려고 했습니다.
허나 이상하게도 독립적으로 thawing() 함수를 실행했을땐 정상적으로 리턴 원하는 값을 하는데...
solution() 함수 내에서 호출을하면 리턴값이 0으로 나와버립니다. 문제가 생긴 곳은 주석으로 처리해서 여쭈어 봅니다.
문제의 출처 :// https://www.acmicpc.net/problem/2573
let input = `5 7
0 0 0 0 0 0 0
0 2 4 5 3 0 0
0 3 0 2 5 2 0
0 7 6 2 4 0 0
0 0 0 0 0 0 0`
let arr = input.split("\n").map((e) =>{
e.split(" ").map((x) => x = parseInt(x, 10))
});
function solution(ice) {
let answer = 0;
let mn = ice.shift();
let n = mn[0] //세로
let m = mn[1] // 가로
let dx = [-1,0,1,0];
let dy = [0,1,0,-1];
for (let y = 1; y <= 10; y++) {
for (let i = 1; i < n - 1; i++) {
for (let j = 1; j < m - 1; j++) {
let count = 0;
if (ice[i][j] > 0) {
for (let k = 0; k < 4; k++) {
let nx = i + dx[k];
let ny = j + dy[k];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && ice[nx][ny] == 0) {
count++
}
}
if(ice[i][j] - count == 0) ice[i][j] = -1
else ice[i][j] -= count;
}
}
}
for (let i = 1; i < n - 1; i++) {
for (let j = 1; j < m - 1; j++) {
if(ice[i][j] < 0) ice[i][j] = 0;
}
}
// if (thawing(ice) > 1) {
// console.log("aa")
// answer = y;
// break
// }
}
return answer;
}
function thawing(ice) {
let count2 = 0;
let n = ice.length;
let m = ice[0].length;
let dx = [-1,0,1,0];
let dy = [0,1,0,-1];
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < m - 1; j++) {
if (ice[i][j] > 0) {
DFS(i, j);
count2++;
}
}
}
function DFS(x, y) {
ice[x][y] = 0;
for(let i = 0; i < 4; i++) {
let nx = x + dx[i];
let ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && ice[nx][ny] > 0) {
DFS(nx,ny);
}
}
}
return count2;
}
console.log(solution(arr));
네 알겠습니다!!