Notice
Recent Posts
Recent Comments
Link
ยซ   2024/09   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[๋ฐฑ์ค€ 17140๋ฒˆ] ์ด์ฐจ์› ๋ฐฐ์—ด๊ณผ ์—ฐ์‚ฐ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 17140๋ฒˆ] ์ด์ฐจ์› ๋ฐฐ์—ด๊ณผ ์—ฐ์‚ฐ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 2. 17. 02:36
728x90
๋ฐ˜์‘ํ˜•

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

 

17140๋ฒˆ: ์ด์ฐจ์› ๋ฐฐ์—ด๊ณผ ์—ฐ์‚ฐ

์ฒซ์งธ ์ค„์— r, c, k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ r, c, k ≤ 100) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ 3๊ฐœ์˜ ์ค„์— ๋ฐฐ์—ด A์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฐฐ์—ด A์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋Š” 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

const fs = require('fs');
let [input, ...matrix] = fs.readFileSync('dev/stdin').toString().trim().split('\n');
input = input.split(' ');
const [num, location] = [+input.pop(), input.map(Number)];
matrix = matrix.map((v) => v.split(' ').map(Number));

console.log(solution(matrix));

function solution(arr) {
  if (verification(arr)) {
    return 0;
  }
  let count = 1;
  let answer = R_Calculate(arr);

  if (verification(answer)) {
    return count;
  }

  while (count < 100) {
    count++;
    if (answer.length >= answer[0].length) {
      answer = R_Calculate(answer);
    } else {
      answer = C_Calculate(answer);
    }

    if (verification(answer)) {
      return count;
    }
  }

  return -1;
}

function R_Calculate(arr) {
  const result = countNumber(arr);

  const maxLength = Math.max(...result.map((v) => v.length));

  return result.map((v) => {
    while (v.length !== maxLength) {
      v.push(0);
    }
    return v;
  });
}

function C_Calculate(arr) {
  const reverseMatrix = Array.from(Array(arr[0].length), () => Array(arr.length).fill(0));
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      reverseMatrix[j][i] = arr[i][j];
    }
  }

  const result = countNumber(reverseMatrix);

  const maxLength = Math.max(...result.map((v) => v.length));

  const reverseResultMatrix = Array.from(Array(maxLength), () => Array(result.length).fill(0));

  for (let i = 0; i < result.length; i++) {
    for (let j = 0; j < result[i].length; j++) {
      reverseResultMatrix[j][i] = result[i][j];
    }
  }

  return reverseResultMatrix;
}

function verification(input) {
  if (
    input.length >= location[0] &&
    input[0].length >= location[1] &&
    input[location[0] - 1][location[1] - 1] === num
  ) {
    return true;
  } else {
    return false;
  }
}

function countNumber(arr) {
  const result = arr.map((v) => {
    const obj = {};

    for (let j = 0; j < v.length; j++) {
      if (v[j] === 0) continue;
      !obj[v[j]] ? (obj[v[j]] = 1) : obj[v[j]]++;
    }

    const value = Object.entries(obj)
      .sort((a, b) => {
        if (a[1] !== b[1]) {
          return a[1] - b[1];
        } else {
          return +a[0] - +b[0];
        }
      })
      .flat()
      .map(Number);

    return value;
  });

  return result;
}

๊ณจ4 ๊ตฌํ˜„๋ฌธ์ œ๋ผ ๊ทธ๋Ÿฐ์ง€ ๋ฐฑ์ค€, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ตฌํ˜„ ๋ฌธ์ œ ํ‘ผ ๊ฒƒ ์ค‘์— ์ด ๋ฌธ์ œ๊ฐ€ ๊ฐ€์žฅ ๊ท€์ฐฎ์•˜๋‹ค. ์š”๊ฒƒ ์ €๊ฒƒ ์ˆ˜์ •ํ•˜๋ฉด ๊ณ„์† ์ผ€์ด์Šค๊ฐ€ ๋ง์ฝ์ด์—ˆ๋‹ค.

๊ฐ„๋‹จํ•œ ์ค„ ์•Œ์•˜์ง€๋งŒ ์ฒซ ์„ค๊ณ„์˜ ์ค‘์š”์„ฑ์„ ๊นจ๋‹ฌ์•˜๋‹ค.

 

์•„๋ž˜๋ถ€ํ„ฐ ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด

 

countNumber Func

ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™”์„ ๋•Œ ๊ฐ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์„œ ๊ธ€์— ์ ํžŒ ๋Œ€๋กœ ์ •๋ ฌํ•œ ํ›„ ๋‹ค์‹œ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

๋ฌด์˜๋ฏธํ•˜๊ฒŒ ๋“ค์–ด๊ฐ„ 0์€ ๋ฌด์‹œํ•œ๋‹ค.

ex) [[1,2,3,5]] => [1,1,2,1,3,1,5,1]]

 

R_Calculate Func

R ์—ฐ์‚ฐ์„ ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. countNumber Func์—ฐ์‚ฐ ํ›„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งˆ์ง€๋ง‰์— 0์„ ๋„ฃ์–ด์ค˜ ํ–‰๋ ฌ์˜ ํ˜•ํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

 

C_Calculate Func

C ์—ฐ์‚ฐ์„ ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋ฐฐ์—ด์˜ ํ–‰๊ณผ ์—ด์„ ๋ฐ”๊พผ ํ›„ countNumber Func์„ ํ•œ ํ›„ ๋‹ค์‹œ ํ–‰๊ณผ ์—ด์„ ๋ฐ”๊ฟ” ๋งˆ์ง€๋ง‰์— 0์„ ๋„ฃ์–ด์ค˜ ํ–‰๋ ฌ์˜ ํ˜•ํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

 

verification Func

A[r][c]์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์ด k์„ ๊ฒ€์ฆํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์ž…๋ ฅ ๋ฐฐ์—ด์˜ ํ–‰๋ ฌ์ด ์ฃผ์–ด์ง„ ๊ฐ’ (r, c)๊ฐ’ ๋ณด๋‹ค ์ž‘์•„์•ผ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐฐ์—ด์—์„œ ํ•ด๋‹น (r, c) ์˜ ๊ฐ’์ด k์ธ์ง€ ํ™•์ธํ•œ ํ›„ 

true, false๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

solution Func

 

1. ์—ฐ์‚ฐ ์‹œ๋„ ์ „์— verification Func์ด ์„ฑ๋ฆฝ๋˜๋ฉด ๋ฐ”๋กœ 0์„ return ํ•œ๋‹ค.

 

2. 3x3์ด ์ฒซ ์ž…๋ ฅ์ด๋ฏ€๋กœ r์—ฐ์‚ฐ์„ ํ•ด์ค€๋‹ค.

 

3. ์ฒซ ์—ฐ์‚ฐ์„ ๋๋ƒˆ์„ ๋•Œ verification Func์œผ๋กœ ๊ฒ€์ฆํ•œ๋‹ค.

 

4. while๋ฌธ์„ ๋Œ๋ฉฐ count๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๊ณ , ํ–‰์ด ์—ด๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด R์—ฐ์‚ฐ์„, ์ž‘์œผ๋ฉด C์—ฐ์‚ฐ์„ ํ•œ๋‹ค.

 

5. ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ verification Func๋กœ ๊ฒ€์ฆํ•œ๋‹ค. => true๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด count ๋ฆฌํ„ด ํ›„ ํ•จ์ˆ˜ ์ข…๋ฃŒ

728x90
๋ฐ˜์‘ํ˜•
Comments