結果

問題 No.1949 足し算するだけのパズルゲーム(2)
ユーザー milanis48663220milanis48663220
提出日時 2022-05-20 22:22:54
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 3,000 ms
コード長 2,500 bytes
コンパイル時間 1,480 ms
コンパイル使用メモリ 135,332 KB
実行使用メモリ 7,384 KB
最終ジャッジ日時 2023-10-20 13:07:44
合計ジャッジ時間 3,429 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 52 ms
5,704 KB
testcase_08 AC 32 ms
5,688 KB
testcase_09 AC 64 ms
5,704 KB
testcase_10 AC 69 ms
5,720 KB
testcase_11 AC 70 ms
5,740 KB
testcase_12 AC 64 ms
5,752 KB
testcase_13 AC 54 ms
5,740 KB
testcase_14 AC 54 ms
7,384 KB
testcase_15 AC 53 ms
7,384 KB
testcase_16 AC 18 ms
5,688 KB
testcase_17 AC 83 ms
7,384 KB
testcase_18 AC 19 ms
5,688 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 64 ms
6,244 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 35 ms
5,720 KB
testcase_25 AC 49 ms
5,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using T = tuple<ll, int, int>;

vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w, x, y; cin >> h >> w >> x >> y; x--; y--;
    auto a = vec2d(h, w, 0ll);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> a[i][j];
        }
    }
    auto seen = vec2d(h, w, false);
    ll sum = a[x][y];
    priority_queue<T, vector<T>, greater<T>> que;
    seen[x][y] = true;
    auto push = [&](int i, int j){
        if(!seen[i][j]){
            seen[i][j] = true;
            que.push(T(a[i][j], i, j));
        }
    };
    auto infield = [&](int i, int j){
        return 0 <= i && i < h && 0 <= j && j < w;  
    };
    for(int k = 0; k < 4; k++){
        int ii = x+dx[k];
        int jj = y+dy[k];
        if(infield(ii, jj)){
            push(ii, jj);
        }
    }
    while(!que.empty()){
        auto [v, i, j] = que.top(); que.pop();
        if(v >= sum){
            cout << "No" << endl;
            return 0;
        }
        sum += v;
        for(int k = 0; k < 4; k++){
            int ii = i+dx[k];
            int jj = j+dy[k];
            if(infield(ii, jj)){
                push(ii, jj);
            }
        }
    }
    cout << "Yes" << endl;
}
0