結果

問題 No.2855 Move on Grid
ユーザー tnakao0123
提出日時 2024-08-29 17:26:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 124 ms / 3,000 ms
コード長 1,341 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 61,552 KB
最終ジャッジ日時 2025-02-24 02:43:40
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:64:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |   scanf("%d%d%d", &n, &m, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:66:37: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   66 |   for (int i = 0; i < nm; i++) scanf("%d", as + i);
      |                                ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2855.cc:  No.2855 Move on Grid - yukicoder
 */

#include<cstdio>
#include<queue>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 300;
const int MAX_M = 300;
const int MAX_NM = MAX_N * MAX_M;
const int MAX_A = 1000000000;
const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

using qi = queue<int>;

/* global variables */

int as[MAX_NM], ds[MAX_NM];

/* subroutines */

int calc(int n, int m, int x) {
  int nm = n * m;
  fill(ds, ds + nm, -1);
  ds[0] = (as[0] < x) ? 1 : 0;

  qi q0, q1;
  q0.push(0);

  while (! q0.empty()) {
    int u = q0.front(); q0.pop();
    int uy = u / m, ux = u % m;
    for (int di = 0; di < 4; di++) {
      int vy = uy + dys[di], vx = ux + dxs[di], v = vy * m + vx;
      if (vy >= 0 && vy < n && vx >= 0 && vx < m && ds[v] < 0) {
	if (as[v] < x) {
	  ds[v] = ds[u] + 1;
	  q1.push(v);
	}
	else {
	  ds[v] = ds[u];
	  q0.push(v);
	}
      }
    }
    if (q0.empty()) swap(q0, q1);
  }

  return ds[nm - 1];
}

/* main */

int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int nm = n * m;
  for (int i = 0; i < nm; i++) scanf("%d", as + i);

  int x0 = 1, x1 = MAX_A + 1;
  while (x0 + 1 < x1) {
    int x = (x0 + x1) / 2;
    if (calc(n, m, x) <= k) x0 = x;
    else x1 = x;
  }

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