문제

https://programmers.co.kr/learn/courses/30/lessons/12899?language=javascript#

풀이

  1. 3진수를 만든다.
  2. 3진수의 0이 있으면 윗자리 수에서 3을 빌려와서 3을 더해준다.

코드

function solution(n) {
  var answer = '';
  let tempans = Array();
  while(n > 0) {
    tempans.push(n % 3);
    n = Math.floor(n / 3);
  }
  for(let i=0;i<tempans.length-1;++i) {
    if(tempans[i] <= 0) {
    tempans[i] += 3;
    tempans[i+1]--;
    } 
  }

  tempans.reverse()
    .forEach((el) => {
      if(el === 0) {
        return;
      }
      if(el === 3) {
        answer += '4';
        return;
      } 
      answer += el;
    })
  return answer;
}

'알고리즘' 카테고리의 다른 글

[HackerRank] New Year Chaos  (0) 2021.01.08
[프로그래머스] 뉴스클러스터링  (0) 2021.01.07
백준 20055 C++ 컨베이어 벨트 위의 로봇  (0) 2020.12.31
Codility - MinAbsSumOfTwo  (0) 2020.12.30
백준 4902 삼각형의 값 c++  (0) 2020.09.17

문제

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

풀이

동전이 2개 있는데 하나만 table 밖으로 밀어내야 하는 문제.
하나만 밖으로 보내기 위해선

  • 두 동전이 겹치면 안됨(겹치면 둘 다 떨어지거나 둘 다 table위에 남거나 밖에 없기 때문에)

bfs로 모든 경우를 검사하고 위 예외만 처리하면 시간초과없이 해결

코드

#include <iostream>
#include <queue>

using namespace std;

int N, M;
char table[21][21];
struct qData {
  pair<int, int> coin1, coin2;
  int depth;
};

pair<int, int> c1, c2;
bool bC1= false;

bool isOutside(int x, int y) {
  if(0 > x || x >= M) {
    return true;
  }
  if(0 > y || y >= N) {
    return true;
  }
  return false;
}

int main(void) {
  cin >> N >> M;
  for(int y=0;y<N;++y) {
    for(int x=0;x<M;++x) {
      cin >> table[y][x];
      if(table[y][x] == 'o') {
        if(bC1) {
          c2 = make_pair(x, y);
        } else {
          c1 = make_pair(x, y);
          bC1 = true;
        }
      }
    }
  }

  queue<qData> q;
  qData initQData;
  initQData.coin1 = c1;
  initQData.coin2 = c2;
  initQData.depth = 0;
  q.push(initQData);
  int dx[4] = {1,-1,0,0};
  int dy[4] = {0,0,1,-1};
  while(!q.empty()) {
    pair<int, int> coin1 = q.front().coin1;
    pair<int, int> coin2 = q.front().coin2;
    int depth = q.front().depth;
    q.pop();
    for(int k=0;k<4;++k) {
      int nextC1X = coin1.first + dx[k];
      int nextC1Y = coin1.second+ dy[k];
      int nextC2X = coin2.first + dx[k];
      int nextC2Y = coin2.second+ dy[k];
      int outsideCnt = 0;
      if(isOutside(nextC1X, nextC1Y)) {
        outsideCnt++;
      }
      if(isOutside(nextC2X, nextC2Y)) {
        outsideCnt++;
      }
      if(outsideCnt == 1) {
        cout << depth+1;
        return 0;
      }
      if(outsideCnt == 0) {
        pair<int, int> nextC1 = make_pair(nextC1X, nextC1Y);
        pair<int, int> nextC2 = make_pair(nextC2X, nextC2Y);
        if(table[nextC1Y][nextC1X] == '#') {
          nextC1 = coin1;
        } 
        if(table[nextC2Y][nextC2X] == '#') {
          nextC2 = coin2;
        } 
        if(nextC1X == nextC2X && nextC1Y == nextC2Y) {
          continue;
        }
        if(depth != 9) {
          qData nextQData;
          nextQData.coin1 = nextC1;
          nextQData.coin2 = nextC2;
          nextQData.depth= depth+1;
          q.push(nextQData);
        }
      }
    }
  }
  cout << -1 << endl;
  return 0;
}

문제

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

풀이

조건대로 차례대로 구현하면 됐다.

  1. 컨베이어 벨트를 회전시킨다.
    1-1 N위치에 로봇이 있다면 로봇을 내린다.
  2. 로봇을 이동시킨다.
    2-1 N위치에 로봇이 있다면 로봇을 내린다.
    2-2 로봇이 이동할 때 내구도를 1 감소시킨다.
  3. 1위치에 로봇을 올린다.
    3-1 1위치에 로봇을 올릴 때 내구도를 1 감소시킨다.
  4. 종료조건을 확인한다.

코드

#include <iostream>

using namespace std;

struct can {
  bool onRobot;
  int left;
};

int N, K;
can A[1001];

void rotateA() {
  can temp = A[N * 2 - 1];
  for(int i=N * 2-1;i>=0;--i) {
    A[i] = A[i-1];
  } 
  A[0] = temp;
  if(A[N-1].onRobot) {
    A[N-1].onRobot = false;
  }
}

void move() {
  for(int i=N - 2;i>=0;--i) {
    if(A[i].onRobot) {
      int nextIdx = i + 1;
      if(!A[nextIdx].onRobot && A[nextIdx].left != 0) {
        if(nextIdx == N-1) {
          A[i].onRobot = false;
          A[nextIdx].left--;
          continue;
        }
        A[i].onRobot = false;
        A[nextIdx].onRobot = true;
        A[nextIdx].left--;
      }
    }
  }
}

void pushRobot() {
  if(!A[0].onRobot && A[0].left != 0) {
    A[0].onRobot = true;
    A[0].left--;
  }
}

int cntEmptyCan() {
  int ret = 0;
  for(int i=0;i<N * 2;++i) {
    if(A[i].left == 0) {
      ret++;
    } 
  }
  return ret;
}

void printLeft() {
  cout << "PL" << endl;
  for(int i=0;i<N * 2;++i) {
    cout << A[i].left << ' ';
  }
  cout << endl;
  for(int i=0;i<N * 2;++i) {
    cout << A[i].onRobot<< ' ';
  }
  cout << endl;
}

int main(void) {
  cin >> N >> K;
  for(int i=0;i<N * 2;++i) {
    cin >> A[i].left;
    A[i].onRobot = false;
  }

  int ans = 1;
  while(true) {
    rotateA();
    move();
    pushRobot();
    if(cntEmptyCan() >= K) {
      cout << ans;
      return 0;
    } 
    ans++;
  }
  return 0;
}

+ Recent posts