結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー naskyanaskya
提出日時 2022-05-01 23:39:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,376 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 9,280 KB
最終ジャッジ日時 2023-09-13 16:31:16
合計ジャッジ時間 2,746 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 30 ms
8,812 KB
testcase_08 AC 40 ms
8,868 KB
testcase_09 AC 42 ms
8,812 KB
testcase_10 AC 40 ms
9,280 KB
testcase_11 AC 41 ms
8,876 KB
testcase_12 AC 34 ms
8,776 KB
testcase_13 AC 34 ms
9,188 KB
testcase_14 AC 35 ms
9,188 KB
testcase_15 AC 34 ms
8,800 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 35 ms
8,800 KB
testcase_21 AC 35 ms
8,884 KB
testcase_22 AC 31 ms
8,884 KB
testcase_23 AC 30 ms
8,900 KB
testcase_24 AC 32 ms
8,872 KB
testcase_25 AC 39 ms
8,868 KB
testcase_26 AC 41 ms
9,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int H, W;
  std::cin >> H >> W;

  std::vector map(H, std::vector<long long>(W)), dp_l(H, std::vector<long long>(W)), dp_f(H, std::vector<long long>(W));
  for (int i = 0; i < H; ++i) {
    std::copy_n(std::istream_iterator<int>(std::cin), W, map[i].begin());
  }

  dp_l[0][0] = map[0][0];

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

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      for (int k = 0; k < 4; ++k) {
        const int ni = i + dy[k], nj = j + dx[k];
        if (ni < 0 || H <= ni || nj < 0 || W <= nj) {
          continue;
        }
        if (dp_l[i][j] > map[ni][nj]) {
          if (ni == H - 1 && nj == W - 1) {
            std::cout << "Yes\n";
            return 0;
          }
          dp_l[ni][nj] = std::max(dp_l[ni][nj], dp_l[i][j] + map[ni][nj]);
        } else if (ni != H - 1 || nj != W - 1) {
          dp_f[ni][nj] = std::max(dp_f[ni][nj], dp_l[i][j]);
        }

        if (dp_f[i][j] > map[ni][nj]) {
          if (ni == H - 1 && nj == W - 1) {
            std::cout << "Yes\n";
            return 0;
          }
          dp_f[ni][nj] = std::max(dp_f[ni][nj], dp_f[i][j] + map[ni][nj]);
        }
      }
    }
  }

  std::cout << "No\n";
}
0