結果

問題 No.2731 Two Colors
ユーザー tnakao0123
提出日時 2024-04-30 16:49:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 244 ms / 3,000 ms
コード長 1,364 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 56,704 KB
最終ジャッジ日時 2025-02-21 09:54:04
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:44:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |   scanf("%d%d", &h, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:46:37: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |   for (int i = 0; i < hw; i++) scanf("%d", as + i);
      |                                ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2731.cc:  No.2731 Two Colors - yukicoder
 */

#include<cstdio>
#include<set>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_HW = MAX_H * MAX_W;
const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 };

/* typedef */

typedef pair<int,int> pii;
typedef set<pii> spii;

/* global variables */

int as[MAX_HW], cs[MAX_HW];

/* subroutines */

void nbrs(int u, int h, int w, spii &b) {
  int uy = u / w, ux = u % w;
  for (int di = 0; di < 4; di++) {
    int vy = uy + dys[di], vx = ux + dxs[di], v = vy * w + vx;
    if (vy >= 0 && vy < h && vx >= 0 && vx < w && cs[v] < 0)
      b.insert({as[v], v});
  }
}

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int hw = h * w;
  for (int i = 0; i < hw; i++) scanf("%d", as + i);

  fill(cs, cs + hw, -1);
  spii bs[2];
  cs[0] = 0, nbrs(0, h, w, bs[0]);
  cs[hw - 1] = 1, nbrs(hw - 1, h, w, bs[1]);

  int i = 0;
  for (;;) {
    int p = (i & 1), q = p ^ 1;
    spii &bp = bs[p], &bq = bs[q];
    if (bp.empty()) break;
    i++;
    
    auto sit = bp.begin();
    int u = sit->second;
    cs[u] = p;
    //printf(" i=%d: %d,%d\n", i, u / w, u % w);

    if (bq.find(*sit) != bq.end()) break;
    bp.erase(sit);
    nbrs(u, h, w, bp);
  }

  printf("%d\n", i);
  
  return 0;
}
0