結果

問題 No.2731 Two Colors
ユーザー butsurizukibutsurizuki
提出日時 2024-04-19 21:32:23
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 180 ms / 3,000 ms
コード長 1,337 bytes
コンパイル時間 3,547 ms
コンパイル使用メモリ 287,276 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-04-19 21:32:32
合計ジャッジ時間 7,392 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
15,104 KB
testcase_01 AC 174 ms
14,976 KB
testcase_02 AC 180 ms
15,104 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 8 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 21 ms
5,504 KB
testcase_07 AC 26 ms
5,804 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 91 ms
11,500 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 92 ms
10,136 KB
testcase_12 AC 89 ms
11,136 KB
testcase_13 AC 79 ms
9,092 KB
testcase_14 AC 42 ms
7,040 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 39 ms
6,400 KB
testcase_17 AC 51 ms
7,552 KB
testcase_18 AC 10 ms
5,376 KB
testcase_19 AC 39 ms
6,208 KB
testcase_20 AC 57 ms
8,488 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 93 ms
11,396 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 74 ms
9,980 KB
testcase_27 AC 91 ms
11,708 KB
testcase_28 AC 58 ms
8,528 KB
testcase_29 AC 19 ms
5,376 KB
testcase_30 AC 33 ms
6,252 KB
testcase_31 AC 22 ms
5,376 KB
testcase_32 AC 116 ms
12,584 KB
testcase_33 AC 2 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>

using namespace std;
using pi=pair<int,int>;
using pip=pair<int,pi>;

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

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int h,w;
  cin >> h >> w;
  vector<vector<int>> a(h,vector<int>(w));
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> a[i][j];
    }
  }

  vector<vector<int>> col(h,vector<int>(w,-1));
  vector<vector<int>> fl(h,vector<int>(w,0));
  col[0][0]=1;
  fl[0][0]=3;
  col[h-1][w-1]=0;
  fl[h-1][w-1]=3;

  vector<priority_queue<pip,vector<pip>,greater<pip>>> pq(2);

  pq[1].push({a[0][1],{0,1}});
  fl[0][1]|=2;
  pq[1].push({a[1][0],{1,0}});
  fl[1][0]|=2;

  pq[0].push({a[h-1][w-2],{h-1,w-2}});
  fl[h-1][w-2]|=1;
  pq[0].push({a[h-2][w-1],{h-2,w-1}});
  fl[h-2][w-1]|=1;

  for(int att=1;;att++){
    pip od=pq[att%2].top();
    pq[att%2].pop();
    int x=od.second.first;
    int y=od.second.second;
    col[x][y]=(att%2);

    for(int k=0;k<4;k++){
      int nx=x+dx4[k];
      int ny=y+dy4[k];
      if(!(0<=nx && nx<h)){continue;}
      if(!(0<=ny && ny<w)){continue;}
      if(col[nx][ny]==((att%2)^1)){
        cout << att << "\n";
        return 0;
      }
      if((fl[nx][ny]&(1<<(att%2)))==0){
        fl[nx][ny]|=(1<<(att%2));
        pq[att%2].push({a[nx][ny],{nx,ny}});
      }
    }
  }
  return 0;
}
0