結果

問題 No.2731 Two Colors
ユーザー tobbietobbie
提出日時 2024-04-22 21:17:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 443 ms / 3,000 ms
コード長 1,628 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 176,088 KB
実行使用メモリ 16,128 KB
最終ジャッジ日時 2024-04-22 21:18:01
合計ジャッジ時間 10,630 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 419 ms
11,520 KB
testcase_01 AC 443 ms
11,520 KB
testcase_02 AC 422 ms
11,648 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 23 ms
6,940 KB
testcase_05 AC 15 ms
6,944 KB
testcase_06 AC 63 ms
6,940 KB
testcase_07 AC 73 ms
6,944 KB
testcase_08 AC 11 ms
6,944 KB
testcase_09 AC 271 ms
11,648 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 294 ms
14,464 KB
testcase_12 AC 265 ms
11,520 KB
testcase_13 AC 225 ms
12,800 KB
testcase_14 AC 123 ms
7,552 KB
testcase_15 AC 9 ms
6,940 KB
testcase_16 AC 107 ms
7,936 KB
testcase_17 AC 153 ms
9,216 KB
testcase_18 AC 26 ms
6,944 KB
testcase_19 AC 110 ms
8,192 KB
testcase_20 AC 187 ms
8,448 KB
testcase_21 AC 29 ms
6,940 KB
testcase_22 AC 279 ms
13,184 KB
testcase_23 AC 67 ms
6,940 KB
testcase_24 AC 34 ms
6,944 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 222 ms
10,752 KB
testcase_27 AC 278 ms
12,672 KB
testcase_28 AC 202 ms
10,368 KB
testcase_29 AC 59 ms
6,940 KB
testcase_30 AC 101 ms
7,168 KB
testcase_31 AC 63 ms
6,944 KB
testcase_32 AC 345 ms
16,128 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
using ll = long long int;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define INF 1000000001

bool comp(int a, int b) {
  return a > b;
}

int main() {
  int H, W;
  cin >> H >> W;
  vvi A(H, vi (W));
  vvi color(H, vi (W, -1));
  rep(i, H) rep(j, W)
    cin >> A[i][j];
  color[0][0] = 1;
  color[H-1][W-1] = 0;
  pii s = {0, 0};
  pii t = {H-1, W-1};
  vector<int> dx = {0, 1, 0, -1};
  vector<int> dy = {1, 0, -1, 0};
  auto f = [&](int x, int y, int i) {
   return (x + dx[i] >= 0 && x + dx[i] < H &&
	   y + dy[i] >= 0 && y + dy[i] < W);
  };
  auto f2 = [&](int x, int y, set<pair<int, pii>> &v) {
    rep(i, 4) {
      if (!f(x, y, i))
	continue;
      int x1 = x + dx[i];
      int y1 = y + dy[i];
      if (color[x1][y1] != -1)
	continue;
      v.insert({A[x1][y1], {x1, y1}});
    }
  };
  auto f3 = [&](int x, int y, int n) {
    rep(i, 4) {
      if (!f(x, y, i)) continue;
      if (color[x + dx[i]][y + dy[i]] == n)
	return true;
    }
    return false;
  };
  set<pair<int, pii>> sv, tv;
  int i = 0;
  while (1) {
    if (i % 2 == 0) {
      f2(s.first, s.second, sv);
      auto it = sv.begin();
      s = it->second;
      sv.erase(it);
      color[s.first][s.second] = 1;
      i++;
      if (f3(s.first, s.second, 0))
	break;
    } else {
      f2(t.first, t.second, tv);
      auto it = tv.begin();
      t = it->second;
      tv.erase(it);
      color[t.first][t.second] = 0;
      i++;
      if (f3(t.first, t.second, 1))
	break;
    }
  }
  cout << i << endl;
  return 0;
}
0