結果

問題 No.2064 Smallest Sequence on Grid
ユーザー simansiman
提出日時 2022-09-06 15:55:56
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 1,006 ms / 3,000 ms
コード長 1,507 bytes
コンパイル時間 4,048 ms
コンパイル使用メモリ 144,332 KB
実行使用メモリ 23,896 KB
最終ジャッジ日時 2024-05-01 12:41:29
合計ジャッジ時間 12,369 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 175 ms
23,536 KB
testcase_18 AC 175 ms
23,624 KB
testcase_19 AC 173 ms
23,472 KB
testcase_20 AC 734 ms
23,740 KB
testcase_21 AC 787 ms
23,792 KB
testcase_22 AC 558 ms
23,808 KB
testcase_23 AC 598 ms
23,808 KB
testcase_24 AC 591 ms
23,896 KB
testcase_25 AC 596 ms
23,836 KB
testcase_26 AC 1,006 ms
23,828 KB
testcase_27 AC 999 ms
23,868 KB
testcase_28 AC 174 ms
23,680 KB
testcase_29 AC 816 ms
22,392 KB
testcase_30 AC 159 ms
22,016 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;
  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];
  }

  queue<Node> que;
  que.push(Node(0, 0, S[0][0]));

  int history[H][W];
  memset(history, -1, sizeof(history));
  string ans = "";
  ans += S[0][0];
  bool visited[H][W];
  memset(visited, false, sizeof(visited));

  for (int i = 0; i < H + W - 2; ++i) {
    priority_queue <Node, vector<Node>, greater<Node>> pque;

    while (not que.empty()) {
      Node node = que.front();
      que.pop();

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

    char ch = pque.top().ch;
    ans += ch;

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

      if (node.ch > ch) break;
      if (visited[node.y][node.x]) continue;
      visited[node.y][node.x] = true;

      que.push(node);
    }
  }

  cout << ans << endl;

  return 0;
}
0