結果

問題 No.2064 Smallest Sequence on Grid
ユーザー simansiman
提出日時 2022-09-06 11:06:58
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,472 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 144,368 KB
実行使用メモリ 76,400 KB
最終ジャッジ日時 2024-05-01 09:29:30
合計ジャッジ時間 18,809 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
  char ch;

  Node(int y = -1, int x = -1, char ch = -1) {
    this->y = y;
    this->x = x;
    this->ch = ch;
  }

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

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

  vector<string> S(H);
  for (int y = 0; y < H; ++y) {
    cin >> S[y];
  }

  priority_queue <Node, vector<Node>, greater<Node>> pque;
  pque.push(Node(0, 0, S[0][0]));

  int history[H][W];
  memset(history, -1, sizeof(history));

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (node.y + 1 < H && history[node.y + 1][node.x] == -1) {
      history[node.y + 1][node.x] = 2;
      pque.push(Node(node.y + 1, node.x, S[node.y + 1][node.x]));
    }

    if (node.x + 1 < W && history[node.y][node.x + 1] == -1) {
      history[node.y][node.x + 1] = 1;
      pque.push(Node(node.y, node.x + 1, S[node.y][node.x + 1]));
    }
  }

  string ans = "";
  int cy = H - 1;
  int cx = W - 1;
  while (cy != 0 || cx != 0) {
    int dir = history[cy][cx];
    ans += S[cy][cx];

    if (dir == 1) {
      cx -= 1;
    } else {
      cy -= 1;
    }
  }
  ans += S[0][0];

  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}
0