結果

問題 No.2731 Two Colors
ユーザー ぷらぷら
提出日時 2024-04-19 22:07:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 3,000 ms
コード長 5,117 bytes
コンパイル時間 1,491 ms
コンパイル使用メモリ 145,696 KB
実行使用メモリ 11,656 KB
最終ジャッジ日時 2024-04-19 22:07:52
合計ジャッジ時間 5,327 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 243 ms
11,136 KB
testcase_01 AC 227 ms
11,136 KB
testcase_02 AC 228 ms
11,136 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 9 ms
6,940 KB
testcase_05 AC 7 ms
6,944 KB
testcase_06 AC 25 ms
6,940 KB
testcase_07 AC 26 ms
6,940 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 96 ms
9,520 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 106 ms
11,656 KB
testcase_12 AC 92 ms
9,324 KB
testcase_13 AC 86 ms
9,140 KB
testcase_14 AC 42 ms
6,940 KB
testcase_15 AC 6 ms
6,944 KB
testcase_16 AC 42 ms
6,940 KB
testcase_17 AC 57 ms
7,144 KB
testcase_18 AC 12 ms
6,944 KB
testcase_19 AC 44 ms
6,940 KB
testcase_20 AC 59 ms
7,484 KB
testcase_21 AC 14 ms
6,940 KB
testcase_22 AC 107 ms
10,148 KB
testcase_23 AC 27 ms
6,944 KB
testcase_24 AC 14 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 82 ms
8,700 KB
testcase_27 AC 99 ms
10,484 KB
testcase_28 AC 68 ms
8,192 KB
testcase_29 AC 22 ms
6,940 KB
testcase_30 AC 39 ms
6,940 KB
testcase_31 AC 26 ms
6,944 KB
testcase_32 AC 129 ms
11,504 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> using vvvc = vector<vector<vector<T>>>;
template<class T> using pq = priority_queue<T,vector<T>,greater<T>>;
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }

#define overload5(a, b, c, d, e, name, ...) name
#define overload4(a, b, c, d, name, ...) name
#define endl '\n'
#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)
#define REP1(i, n) for(ll i = 0; i < (n); ++i)
#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)
#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)
#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)
#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)
#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))
#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)
#define setbits(j, n) for(ll iiiii = (n), j = lowbit(iiiii); iiiii; iiiii ^= 1 << j, j = lowbit(iiiii))
#define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(all(v)));)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define drop(s) cout << #s << endl, exit(0)
#define si(c) (int)(c).size()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
#define rng(v, l, r) v.begin() + (l), v.begin() + (r)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define SORT(v) sort(all(v))
#define REV(v) reverse(all(v))
#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))

int dx4[4] = {0,1,0,-1};
int dy4[4] = {1,0,-1,0};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int h,w;
    cin >> h >> w;
    vvc<int> a(h,vc<int>(w));
    vvc<int> f(h,vc<int>(w,-1));
    rep(i,h) {
        rep(j,w) {
            cin >> a[i][j];
        }
    }
    pq<array<int,3>>pq1,pq0;
    pq1.push({a[0][1],0,1});
    pq1.push({a[1][0],1,0});
    pq0.push({a[h-1][w-2],h-1,w-2});
    pq0.push({a[h-2][w-1],h-2,w-1});
    f[0][0] = 1;
    f[h-1][w-1] = 0;
    int ans = 0;
    rep(i,h*w) {
        ans++;
        if(i%2 == 0) {
            while(!pq1.empty()) {
                auto x = pq1.top();
                pq1.pop();
                if(f[x[1]][x[2]] != -1) continue;
                f[x[1]][x[2]] = 1;
                rep(j,4) {
                    int nx = x[1]+dx4[j];
                    int ny = x[2]+dy4[j];
                    if(nx >= 0 && nx < h && ny >= 0 && ny < w) {
                        if(f[nx][ny] == -1) {
                            pq1.push({a[nx][ny],nx,ny});
                        }
                        if(f[nx][ny] == 0) {
                            cout << ans << "\n";
                            return 0;
                        }
                    }
                }
                break;
            }
        }
        else {
            while(!pq0.empty()) {
                auto x = pq0.top();
                pq0.pop();
                if(f[x[1]][x[2]] != -1) continue;
                f[x[1]][x[2]] = 0;
                rep(j,4) {
                    int nx = x[1]+dx4[j];
                    int ny = x[2]+dy4[j];
                    if(nx >= 0 && nx < h && ny >= 0 && ny < w) {
                        if(f[nx][ny] == -1) {
                            pq0.push({a[nx][ny],nx,ny});
                        }
                        if(f[nx][ny] == 1) {
                            cout << ans << "\n";
                            return 0;
                        }
                    }
                }
                break;
            }
        }
    }
}
0