結果

問題 No.2731 Two Colors
ユーザー rotti_coderrotti_coder
提出日時 2024-04-19 21:55:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 611 ms / 3,000 ms
コード長 1,818 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 89,588 KB
実行使用メモリ 24,876 KB
最終ジャッジ日時 2024-04-19 21:55:31
合計ジャッジ時間 8,875 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 610 ms
11,648 KB
testcase_01 AC 609 ms
11,520 KB
testcase_02 AC 611 ms
11,648 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 23 ms
6,944 KB
testcase_05 AC 17 ms
6,940 KB
testcase_06 AC 68 ms
7,344 KB
testcase_07 AC 77 ms
6,944 KB
testcase_08 AC 13 ms
6,944 KB
testcase_09 AC 289 ms
14,224 KB
testcase_10 AC 6 ms
6,944 KB
testcase_11 AC 299 ms
24,876 KB
testcase_12 AC 282 ms
14,052 KB
testcase_13 AC 252 ms
18,436 KB
testcase_14 AC 129 ms
8,808 KB
testcase_15 AC 11 ms
6,940 KB
testcase_16 AC 120 ms
10,564 KB
testcase_17 AC 165 ms
11,724 KB
testcase_18 AC 30 ms
6,944 KB
testcase_19 AC 126 ms
10,976 KB
testcase_20 AC 186 ms
9,780 KB
testcase_21 AC 33 ms
6,940 KB
testcase_22 AC 307 ms
17,452 KB
testcase_23 AC 76 ms
7,416 KB
testcase_24 AC 37 ms
6,944 KB
testcase_25 AC 4 ms
6,944 KB
testcase_26 AC 238 ms
13,196 KB
testcase_27 AC 304 ms
17,600 KB
testcase_28 AC 191 ms
15,864 KB
testcase_29 AC 60 ms
6,948 KB
testcase_30 AC 108 ms
9,248 KB
testcase_31 AC 70 ms
8,408 KB
testcase_32 AC 376 ms
22,564 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main()
{
  int h,w;
  cin >> h >> w;
  vector<vector<int>>num(h,vector<int>(w));
  for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin >> num[i][j];
  priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>prq1;
  priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>prq2;
  vector<vector<int>>color(h,vector<int>(w,-1));
  prq1.push({0,0,0});
  prq2.push({0,h-1,w-1});
  int cnt=1,ans=0;
  while(true){
    ans++;
    if(cnt==1){
      int a,b;
      while(true){
	vector<int>hako=prq1.top();
	prq1.pop();
	a=hako[1];
	b=hako[2];
	if(color[a][b]==-1)break;
      }
      color[a][b]=1;
      if(a-1>=0){
	if(color[a-1][b]==0)break;
	else if(color[a-1][b]==-1)prq1.push({num[a-1][b],a-1,b});
      }
      if(b+1<w){
	if(color[a][b+1]==0)break;
	else if(color[a][b+1]==-1)prq1.push({num[a][b+1],a,b+1});
      }
      if(a+1<h){
	if(color[a+1][b]==0)break;
	else if(color[a+1][b]==-1)prq1.push({num[a+1][b],a+1,b});
      }
      if(b-1>=0){
	if(color[a][b-1]==0)break;
	else if(color[a][b-1]==-1)prq1.push({num[a][b-1],a,b-1});
      }
      cnt=0;
    }
    else{
      int a,b;
      while(true){
	vector<int>hako=prq2.top();
	prq2.pop();
	a=hako[1];
	b=hako[2];
	if(color[a][b]==-1)break;
      }
      color[a][b]=0;
      if(a-1>=0){
	if(color[a-1][b]==1)break;
	else if(color[a-1][b]==-1)prq2.push({num[a-1][b],a-1,b});
      }
      if(b+1<w){
	if(color[a][b+1]==1)break;
	else if(color[a][b+1]==-1)prq2.push({num[a][b+1],a,b+1});
      }
      if(a+1<h){
	if(color[a+1][b]==1)break;
	else if(color[a+1][b]==-1)prq2.push({num[a+1][b],a+1,b});
      }
      if(b-1>=0){
	if(color[a][b-1]==1)break;
	else if(color[a][b-1]==-1)prq2.push({num[a][b-1],a,b-1});
      }
      cnt=1;
    }
  }
  cout << ans-2 << endl;
}
0