티스토리 뷰

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

 

15662번: 톱니바퀴 (2)

총 8개의 톱니를 가지고 있는 톱니바퀴 T개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n")
const N = input.shift();

const gears = [];
for (let i = 0; i < N; i++) {
  const gear = input.shift().trim().split('')
  gears.push(gear)
}
const K = input.shift();
const rotation = input.map(v => v.trim().split(' ')).map(v => {
  const clockwise = v[1] == '1' ? true : false
  return { index: v[0] - 1, clockwise: clockwise }
})


const goClock = (i) => {
  gears[i].unshift(gears[i].pop())
}

const goCountClock = (i) => {
  gears[i].push(gears[i].shift())
}




for (let i = 0; i < K; i++) {
  const { index, clockwise } = rotation.shift();
  let nowLeft = gears[index][6];
  let nowRight = gears[index][2];
  let left = index - 1;
  let right = index + 1;
  let leftClock = !clockwise
  let rightClock = !clockwise
  if (clockwise) {
    goClock(index)
  } else {
    goCountClock(index)
  }

  while (left > -1) {
    const prevRight = gears[left][2];
    if (prevRight == nowLeft) break;
    nowLeft = gears[left][6];
    if (leftClock) {
      goClock(left)
    } else {
      goCountClock(left)
    }
    leftClock = !leftClock
    left--;
  }

  while (right < N) {
    const nextLeft = gears[right][6];
    if (nextLeft == nowRight) break;
    nowRight = gears[right][2];
    if (rightClock) {
      goClock(right)
    } else {
      goCountClock(right)
    }
    rightClock = !rightClock
    right++;
  }
}

let answer = 0;
gears.forEach(v => {
  if (v[0] == '1') answer++;
})
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
글 보관함