티스토리 뷰

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

 

17822번: 원판 돌리기

반지름이 1, 2, ..., N인 원판이 크기가 작아지는 순으로 바닥에 놓여있고, 원판의 중심은 모두 같다. 원판의 반지름이 i이면, 그 원판을 i번째 원판이라고 한다. 각각의 원판에는 M개의 정수가 적혀

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v => v.split(' ').map(Number));
const [N, M, T] = input.shift();
let circle = input.splice(0, N);
const rotate = input;

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

rotate.forEach(v => {
  const [x, d, k] = v;

  //원판 회전
  for (let i = x; i <= N; i += x) {
    const target = circle[i - 1];
    if (d == 0) { //시계
      for (let j = 0; j < k; j++) {
        target.unshift(target.pop())
      }
    } else { // 반시계
      for (let j = 0; j < k; j++) {
        target.push(target.shift())
      }
    }
  }


  // console.log(circle.map(v => v.join(' ')).join('\n'))

  //같은 거 지우기
  let erase = false;
  let newCircle = Array.from(Array(N), () => Array(M).fill(null));

  for (let i = 0; i < N; i++) {
    for (let j = 0; j < M; j++) {
      let findSame = false;
      for (let k = 0; k < 4; k++) {
        const nx = i + dx[k]
        let ny = j + dy[k]
        if (j + dy[k] == -1) {
          ny = M - 1;
        } else if (j + dy[k] == M) {
          ny = 0;
        }

        if (nx >= 0 && ny >= 0 && nx < N && ny < M && circle[i][j] > 0 && circle[nx][ny] == circle[i][j]) {
          findSame = true;
          erase = true;
          break;
        }
      }

      if (findSame) {
        newCircle[i][j] = 0;
      } else {
        newCircle[i][j] = circle[i][j];
      }
    }
  }

  circle = newCircle

  if (!erase) {
    let sum = 0;
    let cnt = 0;
    let avg;
    for (let i = 0; i < N; i++) {
      for (let j = 0; j < M; j++) {
        if (circle[i][j] > 0) {
          sum += circle[i][j]
          cnt++;
        }
      }
    }
    if (cnt == 0) {
      process.exit(console.log(0))
    } else {
      avg = sum / cnt;
      for (let i = 0; i < N; i++) {
        for (let j = 0; j < M; j++) {
          if (circle[i][j] == 0) continue
          if (circle[i][j] > avg) {
            circle[i][j]--
          } else if (circle[i][j] < avg) {
            circle[i][j]++;
          }
        }
      }
    }
  }
  // console.log('xxxxxxxxxxxxxxxxxxxxx')
  // console.log(circle.map(v => v.join(' ')).join('\n'))

})


let answer = 0;
for (let i = 0; i < N; i++) {
  for (let j = 0; j < M; j++) {
    answer += circle[i][j]
  }
}

console.log(answer)
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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 31
글 보관함