結果

問題 No.2731 Two Colors
ユーザー 矢澤式WINTER矢澤式WINTER
提出日時 2024-04-19 23:19:47
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,679 bytes
コンパイル時間 4,190 ms
コンパイル使用メモリ 264,048 KB
実行使用メモリ 66,176 KB
最終ジャッジ日時 2024-10-11 17:53:42
合計ジャッジ時間 13,299 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
/*/
#include <atcoder/all>
using namespace atcoder;
//*/
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define ALL(x) (x).begin(),(x).end()
#define dbgv(x); for(auto now : x) cout << now << " "; cout << endl;
//using P = pair<int,int>;
using ll = long long;
using ull = unsigned long long;
//*/
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; }
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vint;
random_device rnd;
mt19937 rng(rnd());
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0}; 
void solve(){
  int h,w; cin >> h >> w;
  vector<vll> a(h,vll(w)); rep(i,h)rep(j,w) cin >> a[i][j];
  vector<vll> c(h,vll(w,0));
  priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>> z,o;
  int var = 1;
  int ans = 0;
  set<pair<int,int>> zz,oo;
  oo.insert(make_pair(0,0));
  zz.insert(make_pair(h-1,w-1));
  c[0][0] = 1;
  c[h-1][w-1] = -1;
  rep(i,4){
    if(dx[i] >= 0 && dy[i] >= 0) o.push(make_pair(a[dx[i]][dy[i]],make_pair(dx[i],dy[i])));
  }
  rep(i,4){
    if(dx[i] <= 0 && dy[i] <= 0) z.push(make_pair(a[h-1+dx[i]][w-1+dy[i]],make_pair(h-1+dx[i],w-1+dy[i])));
  }
  while(1){
    bool bre = false;
    ans++;
    if(var == 1){
      auto pp = o.top(); o.pop();
      auto [_,p] = pp;
      rep(i,4){
        int ni = p.first+dx[i];
        int nj = p.second+dy[i];
        if(ni < 0 || nj < 0 || ni >= h || nj >= w || c[ni][nj] == var) continue;
        if(c[ni][nj] == -1) bre = true;
        else{
          if(oo.find(make_pair(ni,nj)) == oo.end()) o.push(make_pair(a[ni][nj],make_pair(ni,nj)));
          oo.insert(make_pair(ni,nj));
        }
      }
      c[p.first][p.second] = var;
    }else{
      auto pp = z.top(); z.pop();
      auto [_,p] = pp;
      rep(i,4){
        int ni = p.first+dx[i];
        int nj = p.second+dy[i];
        if(ni < 0 || nj < 0 || ni >= h || nj >= w || c[ni][nj] == var) continue;
        if(c[ni][nj] == 1) bre = true;
        else{
          if(zz.find(make_pair(ni,nj)) == zz.end()) z.push(make_pair(a[ni][nj],make_pair(ni,nj)));
          zz.insert(make_pair(ni,nj));
        }
      }
      c[p.first][p.second] = var;
    }
    var *= -1;
    if(bre){
      ans = 0;
      rep(i,h){
        rep(j,w){
          if(c[i][j] != 0) ans++;
        }
      }
      cout << ans-2 << endl;
      return;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1; //cin >> t;
  rep(dirc,t) solve();
}
0