結果

問題 No.2731 Two Colors
ユーザー IchigoYPIchigoYP
提出日時 2024-04-20 01:54:07
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 484 ms / 3,000 ms
コード長 1,724 bytes
コンパイル時間 3,452 ms
コンパイル使用メモリ 293,436 KB
実行使用メモリ 21,248 KB
最終ジャッジ日時 2024-04-20 01:54:24
合計ジャッジ時間 10,969 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 463 ms
19,200 KB
testcase_01 AC 484 ms
19,456 KB
testcase_02 AC 479 ms
19,456 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 23 ms
5,376 KB
testcase_05 AC 16 ms
5,376 KB
testcase_06 AC 65 ms
7,040 KB
testcase_07 AC 74 ms
7,040 KB
testcase_08 AC 12 ms
5,376 KB
testcase_09 AC 274 ms
16,384 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 315 ms
18,048 KB
testcase_12 AC 278 ms
16,512 KB
testcase_13 AC 239 ms
15,872 KB
testcase_14 AC 115 ms
9,600 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 120 ms
9,344 KB
testcase_17 AC 163 ms
11,520 KB
testcase_18 AC 27 ms
5,376 KB
testcase_19 AC 114 ms
9,856 KB
testcase_20 AC 201 ms
11,904 KB
testcase_21 AC 29 ms
5,376 KB
testcase_22 AC 311 ms
17,664 KB
testcase_23 AC 69 ms
7,296 KB
testcase_24 AC 35 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 222 ms
14,592 KB
testcase_27 AC 306 ms
17,280 KB
testcase_28 AC 187 ms
12,800 KB
testcase_29 AC 60 ms
6,656 KB
testcase_30 AC 118 ms
8,576 KB
testcase_31 AC 64 ms
7,040 KB
testcase_32 AC 381 ms
21,248 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using vp = vector<pair<ll, ll>>;
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define resort(v) sort(v.rbegin(),v.rend())
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define per(i, n) for (ll i = n - 1; i >= 0; --i)
#define rep2(i, a, n) for (ll i = a; i < n; ++i)
#define per2(i, a, n) for (ll i = n - 1; i >= a; --i)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
const ll inf = 1ll << 60;
ll dx[] = {1, 0, -1, 0, -1, 1, -1, 1};
ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

int main() {
    ll h,w;
    cin >> h >> w;
    vvll a(h,vll(w));
    rep(i,h){
        rep(j,w) cin >> a[i][j];
    }
    vvll dist(h,vll(w,-1));
    vector<set<tuple<ll,ll,ll>>> st(2);
    dist[0][0] = 0;
    dist[h-1][w-1] = 1;
    queue<P> q;
    q.push({0,0});
    q.push({h-1,w-1});
    ll i=0,ans=0;
    while (!q.empty()){
        ll x = q.front().first,y = q.front().second;
        q.pop();
        rep(k,4){
            if(x+dx[k]>=0&&y+dy[k]>=0&&x+dx[k]<h&&y+dy[k]<w&&dist[x+dx[k]][y+dy[k]]==-1){
                st[i].insert({a[x+dx[k]][y+dy[k]],x+dx[k],y+dy[k]});
            }
        }
        auto[m,mx,my] = *st[i].begin();
        q.push({mx,my});
        st[i].erase(*st[i].begin());
        dist[mx][my]=i;
        ans++;
        i = 1-i;
        rep(k,4){
            if(mx+dx[k]>=0&&my+dy[k]>=0&&mx+dx[k]<h&&my+dy[k]<w){
                if(dist[mx+dx[k]][my+dy[k]]==i){
                    cout << ans << endl;
                    return 0;
                } 
            }
        }
    }
}
0