์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€]2468๋ฒˆ '์•ˆ์ „ ์˜์—ญ' ๋ฌธ์ œ ํ’€์ด - JavaScript

์šฉ๋‡ฝ 2023. 7. 12. 18:09
๋ฐ˜์‘ํ˜•

[๋ฐฑ์ค€]2468๋ฒˆ '์•ˆ์ „ ์˜์—ญ' ๋ฌธ์ œ ํ’€์ด - JavaScript

๋ฌธ์ œ ๐Ÿ“–

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

 

2468๋ฒˆ: ์•ˆ์ „ ์˜์—ญ

์žฌ๋‚œ๋ฐฉ์žฌ์ฒญ์—์„œ๋Š” ๋งŽ์€ ๋น„๊ฐ€ ๋‚ด๋ฆฌ๋Š” ์žฅ๋งˆ์ฒ ์— ๋Œ€๋น„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์„ ๊ณ„ํšํ•˜๊ณ  ์žˆ๋‹ค. ๋จผ์ € ์–ด๋–ค ์ง€์—ญ์˜ ๋†’์ด ์ •๋ณด๋ฅผ ํŒŒ์•…ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ์— ๊ทธ ์ง€์—ญ์— ๋งŽ์€ ๋น„๊ฐ€ ๋‚ด๋ ธ์„ ๋•Œ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š”

www.acmicpc.net

ํ’€์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜: DFS

๋‚œ์ด๋„: ์‹ค๋ฒ„ 1

์ ‘๊ทผ ๋ฐฉ๋ฒ• โ“

๋‚˜๋Š” DFS๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์ดํ–ˆ๋‹ค.

๋ฌธ์ œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์€ ์กฐ๊ฑด์— ๋งž๋Š” ์—ฐ๊ฒฐ ์š”์†Œ๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค.

๋น—๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š” ๋†’์ด์˜ ์—ฐ๊ฒฐ์š”์†Œ๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ฐ€ ๋‚ด๋ ธ์„ ๋•Œ ์Œ“์ด๋Š” ๋†’์ด๋ณด๋‹ค ๋†’์€ ์š”์†Œ๋“ค์„ ์ฐพ์•„์ฃผ๋ฉด ๋œ๋‹ค.

์ฃผ์˜ ์‚ฌํ•ญ์œผ๋กœ "์•„๋ฌด ์ง€์—ญ๋„ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค."๋ผ๋Š” ๋ง์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ฐ€ ์•ˆ ๋‚ด๋ ค์„œ ๋ชจ๋‘ ์•ˆ ์ž ๊ธฐ๋Š” ์ƒํ™ฉ๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

ํ’€์ด ์ฝ”๋“œ ๐Ÿ’ก

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs.readFileSync(file).toString().trim().split('\n');

const [n, ...arr] = input;
const graph = arr.map((item) => item.split(' ').map(Number));
const dx = [0, 0, 1, -1];
const dy = [1, -1, 0, 0];
let maxHeight = 0;
for (const x of graph) {
  maxHeight = Math.max(maxHeight, ...x);
}

function dfs(graph, x, y, target) {
  if (x < 0 || x >= n || y < 0 || y >= n) {
    return false;
  }

  if (graph[x][y] <= target) {
    return false;
  }

  graph[x][y] = 0;
  for (let i = 0; i < 4; i++) {
    const nx = x + dx[i];
    const ny = y + dy[i];
    dfs(graph, nx, ny, target);
  }

  return true;
}

let answer = 0;
let count = 0;
while (maxHeight > -1) {
  const newGraph = graph.map((item) => [...item]);

  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      if (dfs(newGraph, i, j, maxHeight)) {
        count++;
      }
    }
  }

  answer = Math.max(answer, count);
  count = 0;

  maxHeight--;
}

console.log(answer);

ํ’€์ด ํ•ด์„ค ๐Ÿ’ก

๋น—๋ฌผ์ด ์Œ“์—ฌ์„œ ์ž ๊ธธ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€์˜ ๋†’์ด๋Š” ์ง€์—ญ ์ค‘ ์ตœ๋Œ€์˜ ๋†’์ด๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

๊ทธ ์ด์ƒ์œผ๋กœ๋Š” ๊ณ„์‚ฐํ•ด๋„ ์ตœ๋Œ€ ๋†’์ด์˜ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ–ˆ์„ ๋•Œ์™€ ๊ฐ™์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์˜๋ฏธ๊ฐ€ ์—†์„ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ maxHeight์„ ๊ตฌํ•ด์ฃผ๊ณ  maxHeight๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต๋ฌธ์ด ์‹คํ–‰๋˜๊ฒŒ ๋” ํ•ด์คฌ๋‹ค.

 

while๋ฌธ์—์„œ๋Š” ์ตœ๋Œ€ ๋†’์ด๋ถ€ํ„ฐ 0๊นŒ์ง€ ๋ฐ˜๋ณต์ด ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์คฌ๋‹ค.

์ตœ๋Œ€ ๋†’์ด๊ฐ€ ๋ณ€๊ฒฝ ๋  ๋•Œ๋งˆ๋‹ค graph๋ฅผ ์ดˆ๊ธฐ ์ƒํƒœ์—์„œ ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ์ƒํƒœ๋กœ ๋Œ๋ ค์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ ์ƒ๋‹จ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด์ค‘๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ณ  dfsํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

dfsํ•จ์ˆ˜๋Š” ์ดˆ๊ธฐํ™”ํ•œ graph์™€ ํ–‰(i), ์—ด(j), ๋น„๊ตํ•ด์„œ ๊ฒ€์‚ฌํ•  ์ตœ๋Œ€ ๋†’์ด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.

๊ทธ๋ฆฌ๊ณ  true๊ฐ’ ์ฆ‰ ์ตœ๋Œ€ ๋†’์ด๋ณด๋‹ค ๋†’์€ ์ง€์—ญ์„ ์ƒ,ํ•˜,์ขŒ,์šฐ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•˜๋ฉด์„œ ์ˆœํšŒํ•ด์„œ ์ž˜ ๋Œ์•„์™”๋‹ค๋ฉด count๋ฅผ ํ•ด์ค€๋‹ค.

์ตœ์ข…์ ์œผ๋กœ ์ตœ๋Œ€ํ•œ ๋ฌผ์ด ์•ˆ ์ž ๊ธฐ๋Š” ์ง€์—ญ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— answer์™€ count ๋ณ€์ˆ˜๋ฅผ ๋น„๊ตํ•ด์„œ ๋†’์ด๋งˆ๋‹ค ๊ณ„์‚ฐํ•œ ์ตœ๋Œ€ ์ง€์—ญ์„ answer์— ๋‹ด์•„์ฃผ๊ณ  ๋‹ค์Œ ๋†’์ด์— ๋Œ€ํ•œ count๋ฅผ ์œ„ํ•ด count๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

 

dfsํ•จ์ˆ˜์—์„œ๋Š”  ๊ทธ๋ž˜ํ”„์˜ ์˜์—ญ์—์„œ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ์žฌ๊ท€๋ฅผ ๋„๋Š” graph์˜ ์š”์†Œ๊ฐ€ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋†’์ด๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด false๋ฅผ return ํ•ด์คฌ๋‹ค.

์ฆ‰ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” ๋†’์ด(target) ๋ณด๋‹ค ๋†’์€ ์š”์†Œ๋งŒ ํ†ต๊ณผ(๊ฒ€์‚ฌ)ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์คฌ๋‹ค.

์œ„ ์กฐ๊ฑด๋“ค์ด ํ†ต๊ณผ๊ฐ€ ๋˜๋ฉด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋กœ 0์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์ƒ, ํ•˜, ์ขŒ, ์šฐ ๋ชจ๋‘ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คฌ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•