結果

問題 No.124 門松列(3)
ユーザー simansiman
提出日時 2022-07-04 16:41:57
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 1,984 bytes
コンパイル時間 6,325 ms
コンパイル使用メモリ 108,524 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 00:25:54
合計ジャッジ時間 8,131 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Node {
  int y;
  int x;
  vector<int> path;

  Node(int y = -1, int x = -1) {
    this->y = y;
    this->x = x;
    this->path.clear();
  }

  bool operator>(const Node &n) const {
    return path.size() > n.path.size();
  }
};

const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};

bool is_kadomatsu(int a1, int a2, int a3) {
  if (a1 == a2) return false;
  if (a1 == a3) return false;
  if (a2 == a3) return false;
  if (a1 < a2 && a2 < a3) return false;
  if (a1 > a2 && a2 > a3) return false;

  return true;
}

int main() {
  int W, H;
  cin >> W >> H;

  int M[H][W];
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      cin >> M[y][x];
    }
  }

  priority_queue <Node, vector<Node>, greater<Node>> pque;
  Node root(0, 0);
  root.path.push_back(M[0][0]);
  pque.push(root);
  bool visited[H][W][10];
  memset(visited, false, sizeof(visited));

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();
    int l = node.path.size();

    if (l >= 2) {
      if (visited[node.y][node.x][node.path[l - 2]]) continue;
      visited[node.y][node.x][node.path[l - 2]] = true;
    }

    if (node.y == H - 1 && node.x == W - 1) {
      cout << node.path.size() - 1 << endl;
      return 0;
    }

    for (int dir = 0; dir < 4; ++dir) {
      int ny = node.y + DY[dir];
      int nx = node.x + DX[dir];
      if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue;

      Node next(ny, nx);
      next.path = node.path;
      next.path.push_back(M[ny][nx]);
      int l = next.path.size();

      if (l >= 3) {
        if (not is_kadomatsu(next.path[l - 1], next.path[l - 2], next.path[l - 3])) continue;
      }

      pque.push(next);
    }
  }

  cout << -1 << endl;

  return 0;
}
0