結果

問題 No.2456 Stamp Art
ユーザー tnakao0123
提出日時 2024-06-22 02:10:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 214 ms / 5,000 ms
コード長 1,617 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 42,748 KB
最終ジャッジ日時 2025-02-22 00:05:59
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:63:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   63 |   scanf("%d%d", &h, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:64:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |   for (int y = 0; y < h; y++) scanf("%s", fs[y]);
      |                               ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2456.cc:  No.2456 Stamp Art - yukicoder
 */

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

/* constant */

const int MAX_H = 2000;
const int MAX_W = 2000;

/* typedef */

/* global variables */

char fs[MAX_H][MAX_W + 4];
int ass[MAX_H + 1][MAX_W + 1];
int bs[MAX_H + 1][MAX_W + 1], bss[MAX_H + 1][MAX_W + 1];

/* subroutines */

inline int rect(int y0, int x0, int y1, int x1) {
  return
    ass[y1][x1] - ass[y1][x0] - ass[y0][x1] + ass[y0][x0];
}

bool check(int h, int w, int k) {
  for (int y = 0; y <= h; y++)
    fill(bs[y], bs[y] + w + 1, 0);
  
  int kk = k * k;
  for (int y = 0; y + k <= h; y++)
    for (int x = 0; x + k <= w; x++)
      if (rect(y, x, y + k, x + k) == kk) {
	bs[y][x]++;
	bs[y + k][x]--;
	bs[y][x + k]--;
	bs[y + k][x + k]++;
      }

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      bss[y + 1][x + 1] =
	bs[y][x] + bss[y + 1][x] + bss[y][x + 1] - bss[y][x];

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
      int c = bss[y + 1][x + 1];
      if ((fs[y][x] == '#' && c == 0) ||
	  (fs[y][x] == '.' && c > 0)) return false;
    }
  return true;
}

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int y = 0; y < h; y++) scanf("%s", fs[y]);

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      ass[y + 1][x + 1] =
	(fs[y][x] == '#' ? 1 : 0)
	+ ass[y + 1][x] + ass[y][x + 1] - ass[y][x];
  
  int k0 = 1, k1 = min(h, w) + 1;
  while (k0 + 1 < k1) {
    int k = (k0 + k1) / 2;
    if (check(h, w, k)) k0 = k;
    else k1 = k;
  }

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