개발자 톡
연습문제 톡
GINI야 도와줘
GINI야 도와줘 반례 부탁드립니다. (javascript)
- 등록일
- 2023-08-02 15:14:31
- 조회수
- 405
- 작성자
- rkdud4889
태케 no 3, 5에서 계속 틀리는데, 잘못된 부분을 못찾겠습니다ㅠ
const fs = require('fs');
let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
let [rc, ...rest] = input;
const [r, c] = rc.split(' ').map(Number);
const map = rest.map(v => v.split(''));
function solution(r, c, map){
let home;
let now = [];
const rain = [];
let visited = Array.from(Array(r), () => Array(c).fill(false));
let dist = Array.from(Array(r), () => Array(c).fill(-1));
for(let i = 0; i < r; i++){
for(let j = 0; j < c; j++){
if(map[i][j] === 'H') home = [i, j];
else if(map[i][j] === 'W'){
now.push([i, j]);
dist[i][j] = 0;
}
else if(map[i][j] === '*'){
rain.push([i, j]);
visited[i][j] = true;
}
}
}
const dx = [-1, 0, 1, 0];
const dy = [0, -1, 0, 1];
while(rain.length !== 0 && now.length !== 0){
const [rx, ry] = rain.shift();
for(let i = 0; i < 4; i++){
const nx = rx + dx[i];
const ny = ry + dy[i];
if(nx < 0 || nx >= r || ny < 0 || ny >= c)
continue;
if(!visited[nx][ny] && map[nx][ny] === '.'){
map[nx][ny] = '*';
rain.push([nx, ny]);
visited[nx][ny] = true;
}
}
const [nwx, nwy] = now.shift();
for(let i = 0; i < 4; i++){
const nx = nwx + dx[i];
const ny = nwy + dy[i];
if(nx < 0 || nx >= r || ny < 0 || ny >= c)
continue;
if(dist[nx][ny] === -1 && map[nx][ny] !== '*' && map[nx][ny] !== 'X'){
dist[nx][ny] = dist[nwx][nwy] + 1;
now.push([nx, ny]);
}
}
}
//소나기는 이동하지 못하지만, 태범이는 이동 가능한 경우
while(now.length !== 0){
const [nwx, nwy] = now.shift();
for(let i = 0; i < 4; i++){
const nx = nwx + dx[i];
const ny = nwy + dy[i];
if(nx < 0 || nx >= r || ny < 0 || ny >= c)
continue;
if(dist[nx][ny] === -1 && map[nx][ny] !== '*' && map[nx][ny] !== 'X'){
dist[nx][ny] = dist[nwx][nwy] + 1;
now.push([nx, ny]);
}
}
}
return dist[home[0]][home[1]] === -1 ? 'FAIL' : dist[home[0]][home[1]];
}
console.log(solution(r, c, map));
#gini야_도와줘
#gini
#javascript