[Node.js] 17822_원판돌리기

2024. 6. 27. 01:06· 코딩 테스트/백준
목차
  1. 문제
  2. 코드
728x90

문제

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

 

코드

const input = require('fs')
  .readFileSync(process.platform === 'linux' ? '/dev/stdin' : __dirname + '/example.txt')
  .toString().trim().split('\n').map(e => e.split(' ').map(Number));

const [n, m, t] = input.shift();
let target = input.splice(0, n);
const moves = input.splice(0, t);
const dir = [[-1, 0], [1, 0], [0, -1], [0, 1]];

for (let i = 0; i < t; i++){
  // console.log(target)
  const [num, d, moveCnt] = moves[i];
  const temp = Array.from(Array(n), () => Array(m).fill(null));
  let isSame = false;

  for (let j = 0; j < n; j++){
    if ((j + 1) % num !== 0) continue;

    if (d === 0) {
      const last = target[j].slice(m-moveCnt);
      const newArr = [...last, ...target[j].slice(0, m - moveCnt)];
      target[j] = newArr;
    }
    if (d === 1) {
      const last = target[j].slice(moveCnt);
      const newArr = [...last, ...target[j].slice(0, moveCnt)];
      target[j] = newArr;
    }
  }

  for (let y = 0; y < n; y++){
    for (let x = 0; x < m; x++){
      if (target[y][x] === 'x') {
        temp[y][x] = 'x';
        continue;
      }
      for (let c = 0; c < 4; c++){
        const nY = y + dir[c][0];
        const nX = (x + dir[c][1]) >= 0 ? x + dir[c][1] : m-1;

        if (nY >= 0 && nY < n && target[y][x] === target[nY][nX]) {
          if (!isSame) isSame = true;
          temp[y][x] = 'x';
          if (temp[nY][nX] !== 'x') temp[nY][nX] = 'x';
        }
      }
      if (temp[y][x] !== 'x') temp[y][x] = target[y][x];
    }
  }
  // console.log(isSame)
  if (!isSame) {
    // console.log('같은게 없음')
    const numCnt = target.reduce((acc1, cur) => acc1 + cur.reduce((acc2, el) => {
      if (el === 'x') return acc2;
      return acc2 + 1;
    }, 0), 0);
    // console.log(numCnt)
    const avg = target.reduce((acc1, cur) => acc1 + cur.reduce((acc2, el) => {
      if (el === 'x') return acc2;
      return acc2 + el;
    }, 0), 0) / numCnt;
    // console.log(target.reduce((acc1, cur) => acc1 + cur.reduce((acc2, el) => {
    //   if (el === 'x') return acc2;
    //   return acc2 + el;
    // }, 0), 0), avg)
    for (let y = 0; y < n; y++){
      for (let x = 0; x < m; x++){
        if(temp[y][x] !== 'x') temp[y][x] += temp[y][x] > avg ? -1 : temp[y][x] < avg ? 1 : 0;
      }
    }
  }

  // console.log(temp,'\n\n\n')
  target = temp;
}
console.log(target.reduce((acc1, cur) => acc1 + cur.reduce((acc2, el) => {
  if (el === 'x') return acc2;
  return acc2 + el;
}, 0), 0))
728x90

'코딩 테스트 > 백준' 카테고리의 다른 글

[Node.js] 16967_배열 복원하기  (0) 2024.07.03
[Node.js] 19236_청소년 상어  (1) 2024.07.01
[Node.js] 17837_새로운 게임 2  (0) 2024.06.26
[Node.js] 17143_낚시왕  (0) 2024.06.18
[Node.js] 17779_게리맨더링 2  (0) 2024.06.10
  1. 문제
  2. 코드
'코딩 테스트/백준' 카테고리의 다른 글
  • [Node.js] 16967_배열 복원하기
  • [Node.js] 19236_청소년 상어
  • [Node.js] 17837_새로운 게임 2
  • [Node.js] 17143_낚시왕
58청춘
58청춘
할 수 있었는데, 해야했나, 했어만 했는데 라는 말을 하며 슬픔과 좌절감을 느끼지 않도록
250x250
58청춘
Just 두 It
58청춘
전체
오늘
어제
  • 분류 전체보기 (542) N
    • JavaScript (106)
      • 모던 자바스크립트 Deep Dive (48)
      • You Don't know JS (43)
      • Javascript (15)
    • TypeScript (8)
      • 이펙티브 타입스크립트 (3)
    • React (49) N
      • 모던 리액트 Deep Dive (8) N
      • 리액트 기초 (7)
    • 코딩 테스트 (283) N
      • 프로그래머스 코딩 테스트 연습 (208)
      • 백준 (63) N
      • 구름 Goorm (12)
    • 프로젝트 (2)
      • 북극팽귄 프로젝트 (9)
      • TYLEGG (2)
      • 가말다 - 마일스톤 (11)
      • INMATE 인천 맛집 소개 (11)
    • FE 이모저모 공부 (22)
    • CS (21)
      • 네트워크 (4)
    • MySQL (5)
    • 회고록 (2)
      • React 프리온보딩 (3)
      • 원티드 프리온보딩 프론트엔드 회고록 (0)
      • 가말다 회고록 (0)
    • 파이썬 (2)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록
  • 글쓰기

공지사항

인기 글

태그

  • 구현
  • cs
  • 코테연습
  • 스택
  • 수학
  • FE개발자
  • 시뮬레이션
  • node.js
  • 취준생
  • 프로그래머스
  • dfs
  • 코테준비
  • 가말다
  • 취준
  • 프로젝트
  • REACT
  • Deep Dive
  • 알고리즘
  • typeScript
  • 백준
  • dp
  • JavaScript
  • 문자열
  • 정렬
  • 코딩테스트
  • 카카오
  • 배열
  • 스코프
  • 완전탐색
  • BFS

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.1
58청춘
[Node.js] 17822_원판돌리기
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.