結果

問題 No.2731 Two Colors
ユーザー Caiiiiiiii
提出日時 2024-04-19 21:46:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 266 ms / 3,000 ms
コード長 1,134 bytes
コンパイル時間 2,246 ms
コンパイル使用メモリ 201,640 KB
最終ジャッジ日時 2025-02-21 04:03:48
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:37:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:40:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |       scanf("%d", &a[i][j]);
      |       ~~~~~^~~~~~~~~~~~~~~~

ソースコード

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