結果

問題 No.2731 Two Colors
ユーザー CaiiiiiiiiCaiiiiiiii
提出日時 2024-04-19 21:46:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 3,000 ms
コード長 1,134 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 202,536 KB
実行使用メモリ 16,500 KB
最終ジャッジ日時 2024-04-19 21:46:17
合計ジャッジ時間 6,179 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
11,724 KB
testcase_01 AC 240 ms
11,592 KB
testcase_02 AC 253 ms
11,720 KB
testcase_03 AC 4 ms
6,948 KB
testcase_04 AC 11 ms
7,116 KB
testcase_05 AC 9 ms
11,088 KB
testcase_06 AC 26 ms
11,200 KB
testcase_07 AC 29 ms
7,936 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 98 ms
12,464 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 114 ms
16,500 KB
testcase_12 AC 102 ms
12,436 KB
testcase_13 AC 94 ms
14,168 KB
testcase_14 AC 50 ms
11,672 KB
testcase_15 AC 6 ms
6,944 KB
testcase_16 AC 44 ms
11,308 KB
testcase_17 AC 63 ms
11,100 KB
testcase_18 AC 13 ms
6,940 KB
testcase_19 AC 52 ms
12,212 KB
testcase_20 AC 65 ms
11,428 KB
testcase_21 AC 16 ms
11,604 KB
testcase_22 AC 107 ms
12,568 KB
testcase_23 AC 33 ms
11,972 KB
testcase_24 AC 17 ms
10,052 KB
testcase_25 AC 4 ms
10,572 KB
testcase_26 AC 84 ms
11,624 KB
testcase_27 AC 104 ms
13,504 KB
testcase_28 AC 71 ms
12,880 KB
testcase_29 AC 24 ms
7,064 KB
testcase_30 AC 41 ms
11,720 KB
testcase_31 AC 27 ms
11,240 KB
testcase_32 AC 129 ms
13,340 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long LL;

const int N = 1e3 + 7;
const int MOD = 998244353;

const int X[] = {1, -1, 0, 0};
const int Y[] = {0, 0, 1, -1};

int n, m, ans;
int a[N][N], c[N][N];

template<typename T> void solve(T &q, int co) {
  auto [v, x, y] = q.top();
  q.pop();
  if(c[x][y] == co) {
    solve(q, co);
    return ;
  }
  c[x][y] = co;
  for(int i = 0; i < 4; ++i) {
    int u = x + X[i], v = y + Y[i];
    if(u >= 1 && u <= n && v >= 1 && v <= m) {
      if(!c[u][v])
	q.push({a[u][v], u, v});
      else if(c[u][v] != co) {
	printf("%d\n", ans);
	exit(0);
      }
    }
  }
}

int main() {

  scanf("%d%d", &n, &m);
  for(int i = 1; i <= n; ++i)
    for(int j = 1; j <= m; ++j)
      scanf("%d", &a[i][j]);

  c[1][1] = 1, c[n][m] = 2;
  std::priority_queue<std::tuple<int, int, int>,
		      std::vector<std::tuple<int, int, int>>,
		      std::greater<std::tuple<int, int, int>>> p, q;
  p.push({a[1][2], 1, 2});
  p.push({a[2][1], 2, 1});
  q.push({a[n - 1][m], n - 1, m});
  q.push({a[n][m - 1], n, m - 1});

  while(true) {
    ++ans;
    solve(p, 1);
    ++ans;
    solve(q, 2);
  }
  
  return 0;

}
0