結果

問題 No.1948 足し算するだけのパズルゲーム(1)
ユーザー nawawannawawan
提出日時 2022-05-20 23:03:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 1,692 bytes
コンパイル時間 1,175 ms
コンパイル使用メモリ 103,068 KB
実行使用メモリ 10,228 KB
最終ジャッジ日時 2023-10-20 13:57:13
合計ジャッジ時間 4,010 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 66 ms
10,228 KB
testcase_08 AC 115 ms
10,228 KB
testcase_09 AC 123 ms
10,228 KB
testcase_10 AC 124 ms
10,228 KB
testcase_11 AC 115 ms
10,228 KB
testcase_12 AC 86 ms
10,228 KB
testcase_13 AC 86 ms
10,228 KB
testcase_14 AC 86 ms
10,228 KB
testcase_15 AC 85 ms
10,228 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 104 ms
10,228 KB
testcase_21 AC 103 ms
10,228 KB
testcase_22 AC 70 ms
10,228 KB
testcase_23 AC 68 ms
10,228 KB
testcase_24 AC 84 ms
10,228 KB
testcase_25 AC 123 ms
9,964 KB
testcase_26 AC 126 ms
10,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <tuple>
using namespace std;
int main()
{
    int H, W;
    cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W));
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
            cin >> A[i][j];
    }
    int dx[2] = {0, 1}, dy[2] = {1, 0};
    vector<vector<vector<long long>>> f(2, vector<vector<long long>>(H, vector<long long>(W, -1)));
    queue<tuple<int, int, int, long long>> q;
    q.push({0, 0, 0, A[0][0]});
    f[0][0][0] = A[0][0];
    while (!q.empty())
    {
        auto [id, x, y, d] = q.front();
        q.pop();
        if (f[id][x][y] > d)
            continue;
        for (int i = 0; i < 2; i++)
        {
            int nx = x + dx[i], ny = y + dy[i];
            if (0 <= nx && nx < H && 0 <= ny && ny < W)
            {
                if (d <= A[nx][ny] && id == 0 && (nx != H - 1 || ny != W - 1))
                {
                    if (d > f[1][nx][ny])
                    {
                        f[1][nx][ny] = max(f[1][nx][ny], d);
                        q.push({1, nx, ny, d});
                    }
                }
                else if (d > A[nx][ny])
                {
                    if (d + A[nx][ny] > f[id][nx][ny])
                    {
                        f[id][nx][ny] = max(f[id][nx][ny], A[nx][ny] + d);
                        q.push({id, nx, ny, A[nx][ny] + d});
                    }
                }
            }
        }
    }
    if (f[0][H - 1][W - 1] != -1 || f[1][H - 1][W - 1] != -1)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
0