結果

問題 No.2731 Two Colors
ユーザー maeshunmaeshun
提出日時 2024-04-19 22:21:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 472 ms / 3,000 ms
コード長 2,614 bytes
コンパイル時間 5,135 ms
コンパイル使用メモリ 261,476 KB
実行使用メモリ 16,144 KB
最終ジャッジ日時 2024-04-19 22:21:28
合計ジャッジ時間 11,928 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 449 ms
11,520 KB
testcase_01 AC 447 ms
11,648 KB
testcase_02 AC 472 ms
11,392 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 23 ms
5,376 KB
testcase_05 AC 16 ms
5,376 KB
testcase_06 AC 56 ms
5,880 KB
testcase_07 AC 68 ms
5,828 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 256 ms
11,116 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 229 ms
16,144 KB
testcase_12 AC 253 ms
10,972 KB
testcase_13 AC 194 ms
11,868 KB
testcase_14 AC 113 ms
7,444 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 105 ms
7,788 KB
testcase_17 AC 146 ms
8,656 KB
testcase_18 AC 27 ms
5,376 KB
testcase_19 AC 99 ms
7,852 KB
testcase_20 AC 178 ms
8,364 KB
testcase_21 AC 28 ms
5,376 KB
testcase_22 AC 276 ms
12,676 KB
testcase_23 AC 63 ms
5,992 KB
testcase_24 AC 32 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 222 ms
10,416 KB
testcase_27 AC 259 ms
12,752 KB
testcase_28 AC 157 ms
10,592 KB
testcase_29 AC 49 ms
5,572 KB
testcase_30 AC 93 ms
7,336 KB
testcase_31 AC 56 ms
6,844 KB
testcase_32 AC 312 ms
14,824 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for(int i=0;i<(n);++i)
#define rep1(i, n) for(int i=1;i<=(n);i++)
#define ll long long
using mint = modint998244353;
using P = pair<ll,ll>;
using lb = long double;
using T = tuple<ll, ll, ll>;
#ifdef LOCAL
#  include <debug_print.hpp>
#  define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define dbg(...) (static_cast<void>(0))
#endif

int main()
{
    int h, w;
    cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    rep(i,h)rep(j,w) cin >> a[i][j];
    vector<vector<int>> used(h, vector<int>(w, -1));
    priority_queue<T, vector<T>, greater<T>> pq1, pq0;
    vector<int> di = {0, 1, 0, -1};
    vector<int> dj = {1, 0, -1, 0};
    auto add = [&](int x, int y, int t) {
        rep(d,4) {
            int ni = x + di[d];
            int nj = y + dj[d];
            if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
            if(used[ni][nj]!=-1) continue;
            if(t==0) pq0.emplace(a[ni][nj], ni, nj);
            else pq1.emplace(a[ni][nj], ni, nj);
        }
    };
    add(0, 0, 1);
    add(h-1, w-1, 0);
    used[0][0] = 1;
    used[h-1][w-1] = 0;
    int ans = 0;
    while(!pq0.empty() && !pq1.empty()) {
        if(ans%2==0) {
            while(!pq1.empty()) {
                auto [val, x,y] = pq1.top();pq1.pop();
                if(used[x][y]!=-1) continue;
                used[x][y] = 1;
                add(x, y, 1);
                ans++;  
                dbg(x,y,val);
                rep(d,4) {
                    int ni = x + di[d];
                    int nj = y + dj[d];
                    if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
                    if(used[ni][nj]==0) {
                        cout << ans << endl;
                        exit(0);
                    }
                }
                break;
            }
        }
        else{
            while(!pq0.empty()) {
                auto [val, x,y] = pq0.top();pq0.pop();
                if(used[x][y]!=-1) continue;
                used[x][y] = 0;
                add(x, y, 0);
                ans++;
                dbg(x,y,val);
                rep(d,4) {
                    int ni = x + di[d];
                    int nj = y + dj[d];
                    if(ni<0 || ni>=h || nj<0 || nj>=w) continue;
                    if(used[ni][nj]==1) {
                        cout << ans << endl;
                        exit(0);
                    }
                }  
                break;
            }
        }

    }
    cout << ans << endl;
    return 0;
}
0