金魚亭日常

読書,ガジェット,競技プログラミング

ARC#001 B. リモコン

chokudaiさんが毎日一問問題を選んでくれる #chokudai今日の一問

4日目は

ARC#001 B. リモコン

解答. 前回はRubyで解いていた.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>

using namespace std;

static int A, B;
typedef pair<int, int> P;

int bfs() {
  queue<P> que;
  que.push(P(A, 0));
  P p;
  while (!que.empty()) {
    p = que.front();
    que.pop();
    if (p.first == B) {
      break;
    }
    for (int d : {-1, 1, -5, 5, -10, 10}) {
      if (abs(p.first + d - B) < abs(p.first - B)) {
        que.push(P(p.first + d, p.second + 1));
      }
    }
  }
  return p.second;
}

void solve() { cout << bfs() << endl; }

int main() {
  cin >> A >> B;
  solve();
  return 0;
}

https://atcoder.jp/contests/arc001/submissions/7087376