[JavaScript] 백준 - 특별상이라도 받고 싶어

2023. 5. 3. 21:21Language/JavaScript

문제출처: https://www.acmicpc.net/problem/24460

 

24460번: 특별상이라도 받고 싶어

첫 번째 줄에는 정수 $N$이 주어진다. (단, $N = 2^m$, $0 \le m \le 10$, $m$은 정수) 두 번째 줄부터 $N$개 줄의 $i$번째 줄에는 $i$번째 줄에 있는 의자에 적힌 추첨번호가 주어진다. 각 줄에는 $N$개의 추첨

www.acmicpc.net


문제풀이

const fs = require("fs");

const input = fs.readFileSync("/dev/stdin").toString().split("\n");
const N = parseInt(input[0]);
const graph = [];
for (let i = 1; i <= N; i++) {
  graph.push(input[i].split(" ").map(Number));
}

console.log(Split(0, 0, N, graph));

function Split(x, y, n, graph) {
    if (n === 1) {
      return graph[x][y];
    } else {
      const list = [
        Split(x, y, n / 2, graph),
        Split(x, y + (n / 2), n / 2, graph),
        Split(x + (n / 2), y, n / 2, graph),
        Split(x + (n / 2), y + (n / 2), n / 2, graph)
      ];
      list.sort((a, b) => a - b);
      return list[1];
    }
  }