結果
問題 | No.2731 Two Colors |
ユーザー |
![]() |
提出日時 | 2024-04-19 22:53:42 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 285 ms / 3,000 ms |
コード長 | 2,184 bytes |
コンパイル時間 | 4,553 ms |
コンパイル使用メモリ | 259,988 KB |
最終ジャッジ日時 | 2025-02-21 05:08:11 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
#include<bits/stdc++.h>using namespace std;//* ATCODER#include<atcoder/all>using namespace atcoder;typedef modint998244353 mint;//*//* BOOST MULTIPRECISION#include<boost/multiprecision/cpp_int.hpp>using namespace boost::multiprecision;//*/typedef long long ll;#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)template <typename T> bool chmin(T &a, const T &b) {if (a <= b) return false;a = b;return true;}template <typename T> bool chmax(T &a, const T &b) {if (a >= b) return false;a = b;return true;}template <typename T> T max(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);return ret;}template <typename T> T min(vector<T> &a){assert(!a.empty());T ret = a[0];for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);return ret;}template <typename T> T sum(vector<T> &a){T ret = 0;for (int i=0; i<(int)a.size(); i++) ret += a[i];return ret;}int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);int h,w; cin >> h >> w;vector a(h,vector<int>(w));rep(i,0,h)rep(j,0,w)cin>>a[i][j];vector<priority_queue<tuple<int,int,int>>> pq(2);vector c(h,vector<int>(w,-1));auto add=[&](int i, int j){int col = c[i][j];for (auto[x,y]:{pair(i+1,j),pair(i-1,j),pair(i,j+1),pair(i,j-1)}){if(!(0<=x&&x<h))continue;if(!(0<=y&&y<w))continue;if(c[x][y]!=-1)continue;pq[col].push(make_tuple(-a[x][y],x,y));}};auto owari_hantei=[&](int i,int j){int col = c[i][j];for (auto[x,y]:{pair(i+1,j),pair(i-1,j),pair(i,j+1),pair(i,j-1)}){if(!(0<=x&&x<h))continue;if(!(0<=y&&y<w))continue;if(c[x][y]==(col^1))return true;}return false;};c[0][0]=0;c[h-1][w-1]=1;add(0,0);add(h-1,w-1);int cnt = 0;while(true){bool mode = 0;bool tt = 0;while(!pq[cnt%2].empty()){auto [val,x,y] = pq[cnt%2].top();pq[cnt%2].pop();if (c[x][y]==-1){c[x][y] = cnt%2;add(x,y);tt = owari_hantei(x,y);mode = 1;break;}}if(!mode)break;cnt++;if(tt)break;}cout << cnt << '\n';}