Algorithm/Baekjoon

[JavaScript/node.js] ๋ฐฑ์ค€ 24513๋ฒˆ - ์ข€๋น„ ๋ฐ”์ด๋Ÿฌ์Šค

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 9. 8. 19:46

[๋ฌธ์ œ]

https://www.acmicpc.net/problem/24513

 

24513๋ฒˆ: ์ข€๋น„ ๋ฐ”์ด๋Ÿฌ์Šค

์—ฌ๊ธฐ $N$ x $M$ ๊ฒฉ์ž ๋ชจ์–‘์˜ ๋งˆ์„์ด ์žˆ๋‹ค. ์–ด๋Š ๋‚  ์„ธ์ƒ์— ์ข€๋น„ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์ฐฝ๊ถํ•˜์—ฌ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋น ๋ฅด๊ฒŒ ํผ์ ธ๋‚˜๊ฐ€๋ฒ„๋ฆฐ๋‹ค. ๋ฐ”์ด๋Ÿฌ์Šค์— ๋Œ€ํ•ด ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ ์„ธ ์ข…๋ฅ˜์˜ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ–ˆ์œผ๋ฉฐ ๊ฐ๊ฐ $1$

www.acmicpc.net

 

[ํ’€์ด]

BFS๋ฅผ ํ†ตํ•ด ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฝ๋‹ˆ๋‹ค.

 

์ดˆ๊ธฐ ์„ธํŒ…

  • village : ๋งˆ์„ ์ •๋ณด ์ €์žฅ
  • virus: [x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ฐ”์ด๋Ÿฌ์Šค ํƒ€์ž…] ํ˜•ํƒœ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค ์ •๋ณด ์ €์žฅ
  • checkOut(x, y) : ๋งˆ์„์„ ๋ฒ—์–ด๋‚œ ์œ„์น˜๋ฉด true, ๋งˆ์„ ์•ˆ์— ์žˆ๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

ํ’€์ด ๊ณผ์ •

bfs๋ฅผ ํ†ตํ•ด ๊ฐฑ์‹ ๋˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค ์ •๋ณด๋ฅผ virus์— ์ €์žฅํ•˜๋ฉฐ ๋ชจ๋‘ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ ๋™์‹œ๊ฐ„๋Œ€์— 1๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค์™€ 2๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋งŒ๋‚˜๋ฉด 3๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ด์ „ ๋ฐ”์ด๋Ÿฌ์Šค ํƒ€์ž…์—์„œ +2์”ฉ ์ˆซ์ž๋ฅผ ๋”ํ•ด์ฃผ์–ด ์‹œ๊ฐ„์„ ๊ตฌ๋ถ„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  • ์ฒซ๋ฒˆ์งธ ์‹œ๊ฐ„์— ํผ์ง„ ๋ฐ”์ด๋Ÿฌ์Šค : 1, 2
  • ๋‘๋ฒˆ์งธ ์‹œ๊ฐ„์— ํผ์ง„ ๋ฐ”์ด๋Ÿฌ์Šค : 3, 4
  • ์„ธ๋ฒˆ์งธ ์‹œ๊ฐ„์— ํผ์ง„ ๋ฐ”์ด๋Ÿฌ์Šค : 5, 6

(ํŽธ์˜์„ฑ์„ ์œ„ํ•ด 3๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค๋กœ ๋ฒˆ์ง„ ๋งˆ์„์€ Infinity๋ฅผ ์ €์žฅํ•˜์—ฌ ๊ตฌ๋ถ„)

 

bfs์˜ ๋™์ž‘๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. ๋งŒ์•ฝ village๊ฐ€ -1์ด๋‚˜ Infinity๋ผ๋ฉด ๋”์ด์ƒ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ continueํ•ฉ๋‹ˆ๋‹ค.

 

2. ์ƒํ•˜์ขŒ์šฐ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์งˆ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

  • village๊ฐ€ 0์ด๋ฉด ์•„์ง ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ํผ์ง€์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์ด์ „ ๋ฐ”์ด๋Ÿฌ์Šค์˜ type + 2๋ฅผ ์ €์žฅํ›„  virus์— ํ•ด๋‹น ์ •๋ณด๋ฅผ push
  • village์™€ ์ƒˆ๋กœ ๋“ค์–ด์˜จ ๋ฐ”์ด๋Ÿฌ์Šค์˜ type์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ(์ง, ํ™€ ์Œ์ผ๋•Œ) ์—๋งŒ ๋ฐ”์ด๋Ÿฌ์Šค 3์ด ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
    • village๊ฐ€ ํ™€์ˆ˜๋ฉด (village์˜ ์ˆซ์ž - 1) === (type + 2) ์ธ ๊ฒฝ์šฐ์— ๋ฐ”์ด๋Ÿฌ์Šค 3์œผ๋กœ ๊ฐฑ์‹  
    • village๊ฐ€ ์ง์ˆ˜๋ฉด (village์˜ ์ˆซ์ž + 1) === (type + 2) ์ธ ๊ฒฝ์šฐ์— ๋ฐ”์ด๋Ÿฌ์Šค 3์œผ๋กœ ๊ฐฑ์‹ 

 

๋งˆ์ง€๋ง‰์œผ๋กœ ๋งˆ์„ ์ •๋ณด๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ฐ”์ด๋Ÿฌ์Šค 1, 2, 3์˜ ๊ฐœ์ˆ˜๋ฅผ ๋”ํ•ฉ๋‹ˆ๋‹ค.

  • -1์ด๋‚˜ 0์ด๋ฉด ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์—†์Œ
  • ํ™€์ˆ˜๋ฉด 1๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค
  • ์ง์ˆ˜๋ฉด 2๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค
  • Infinity๋ฉด 3๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค

 

 

[์ฝ”๋“œ]

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';

const input = fs.readFileSync(filePath).toString().split('\n');

const [N, M] = input[0].split(" ").map(Number);

// ๋งˆ์„ ์ •๋ณด
const village = [];
// ๋ฐ”์ด๋Ÿฌ์Šค ์œ„์น˜
let virus = [];

for (let i = 1; i <= N; i++) {
    village.push(input[i].split(" ").map(Number));
    for (let j = 0; j < M; j++) {
        if (village[i - 1][j] === 1 || village[i - 1][j] === 2) {
            // x, y, type
            virus.push([i - 1, j, village[i - 1][j]]);
        }
    }
}

const checkOut = (x, y) => {
    if (x < 0 || y < 0 || x >= N || y >= M) {
        return true;
    }
    return false;
}

const dx = [1, -1, 0, 0];
const dy = [0, 0, 1, -1];

let idx = 0;
let time = 2;

// ๋™์‹œ๊ฐ„๋Œ€๋ฅผ ํ™•์ธํ•ด์•ผํ•จ!!
// ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋ชจ๋‘ ํผ์ง
while (idx < virus.length) {
    const [x, y, type] = virus[idx];
    idx++;
    if(village[x][y] === -1 || village[x][y] === Infinity) {
        continue;
    }
    for (let i = 0; i < 4; i++) {
        const [nx, ny] = [x + dx[i], y + dy[i]];
        if (checkOut(nx, ny) || village[nx][ny] === -1 || village[nx][ny] === Infinity) {
            continue;
        }
        if(village[nx][ny] === 0) {
            // ์ฒซ ๋ฐฉ๋ฌธ
            village[nx][ny] = type + 2;
            virus.push([nx, ny, type + 2]);
        } else {
            // ๋™์‹œ๊ฐ„๋Œ€ ๋‹ค๋ฅธ ๋ฐ”์ด๋Ÿฌ์Šค ๋ฐฉ๋ฌธ
            if(type%2 !== village[nx][ny]%2) {
                // ๋ฐ”์ด๋Ÿฌ์Šค 1์ด ๋“ค์–ด์˜ด
                if(type%2 === 1 && village[nx][ny] - 1 === type + 2) {
                    village[nx][ny] = Infinity;
                } else if(type%2 === 0 && village[nx][ny] + 1 === type + 2) {
                    village[nx][ny] = Infinity;
                }
            }
        }
    }
}

let one = 0;
let two = 0;
let three = 0;

for(let i=0;i<N;i++) {
    for(let j=0;j<M;j++) {
        if(village[i][j] === -1 || village[i][j] === 0) {
            continue;
        }
        if(village[i][j] %2 === 1) one++;
        else if(village[i][j] % 2 === 0) two++;
        else if(village[i][j] === Infinity) three++;
    }
}

console.log(one, two, three);