結果

問題 No.124 門松列(3)
ユーザー kk
提出日時 2021-02-26 21:44:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,459 bytes
コンパイル時間 1,839 ms
コンパイル使用メモリ 206,056 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 12:21:01
合計ジャッジ時間 2,729 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1<<28;

int w, h;
int bd[100][100];
int dist[100][100][10];

int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};

bool kadomatsu(int x, int y, int z) {
  if (x == y || y == z || z == x) return false;
  if (max({x, y, z}) == y) return true;
  if (min({x, y, z}) == y) return true;
  return false;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> w >> h;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> bd[i][j];

  queue<tuple<int, int, int> > q;
  int s = bd[0][0];

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      for (int k = 0; k < 10; k++)
        dist[i][j][k] = INF;
  
  dist[0][1][s] = 1;
  dist[1][0][s] = 1;
  q.emplace(0, 1, s);
  q.emplace(1, 0, s);

  while (!q.empty()) {
    int y, x, pre, d;
    tie(y, x, pre) = q.front();
    q.pop();
    d = dist[y][x][pre];

    for (int k = 0; k < 4; k++) {
      int y2 = y + dy[k];
      int x2 = x + dx[k];
      if (x2 < 0 || x2 >= w) continue;
      if (y2 < 0 || y2 >= h) continue;
      if (kadomatsu(pre, bd[y][x], bd[y2][x2])) {
        if (dist[y2][x2][bd[y][x]] == INF) {
          dist[y2][x2][bd[y][x]] = d + 1;
          q.emplace(y2, x2, bd[y][x]);
        }
      }
    }
  }

  int ret = INF;
  for (int i = 0; i < 10; i++)
    ret = min(ret, dist[h-1][w-1][i]);

  if (ret == INF)
    cout << -1 << endl;
  else
    cout << ret << endl;

  return 0;
}
0