結果

問題 No.2213 Neq Move
ユーザー tnakao0123
提出日時 2024-07-05 13:12:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,910 bytes
コンパイル時間 650 ms
コンパイル使用メモリ 67,936 KB
最終ジャッジ日時 2025-02-22 02:13:35
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 5
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |   scanf("%d", &tn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:42:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   42 |     scanf("%d%d%d%d", &a, &b, &c, &d); // a->c, b->d
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2213.cc:  No.2213 Neq Move - yukicoder
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_M = 3 + 4 * 2;
const int MAX_N = MAX_M * MAX_M;
const long long LINF = 1LL << 60;

/* typedef */

using ll = long long;
using pii = pair<int,int>;
using vpii = vector<pii>;
using pli = pair<ll,int>;

/* global variables */

vpii nbrs[MAX_N];
ll ds[MAX_N];

/* subroutines */

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d); // a->c, b->d

    int as[MAX_M] = {
      0, 1, 2, a, a + 1, b, b + 1, c, c + 1, d, d + 1
    };
    sort(as, as + MAX_M);
    int m = unique(as, as + MAX_M) - as;
    int n = m * m;

    int st = -1, gl = -1;
    for (int u = 0; u < n; u++) {
      nbrs[u].clear();

      int ux = u / m, uy = u % m;
      if (ux == uy) continue;
      if (as[ux] == a && as[uy] == b) st = u;
      if (as[ux] == c && as[uy] == d) gl = u;

      for (int vx = ux + 1; vx < m; vx++)
	if (uy < ux || vx < uy)
	  nbrs[u].push_back({as[vx] - as[ux], vx * m + uy});
      for (int vy = uy + 1; vy < m; vy++)
	if (ux < uy || vy < ux)
	  nbrs[u].push_back({as[vy] - as[uy], ux * m + vy});
      if (ux != 0 && uy != 0) {
	nbrs[u].push_back({1, ux * m + 0});
	nbrs[u].push_back({1, 0 * m + uy});
      }
    }
    //for (int i = 0; i < m; i++) printf(" %d", as[i]); putchar('\n');
    //printf(" st=%d, gl=%d\n", st, gl);

    fill(ds, ds + n, LINF);
    ds[st] = 0;
    priority_queue<pli> q;
    q.push({0, st});

    while (! q.empty()) {
      auto [ud, u] = q.top(); q.pop();
      ud = -ud;
      if (ds[u] != ud) continue;

      for (auto [e, v]: nbrs[u]) {
	ll vd = ud + e;
	if (ds[v] > vd) {
	  ds[v] = vd;
	  q.push({-vd, v});
	}
      }
    }

    printf("%lld\n", ds[gl]);
  }
  
  return 0;
}
0