結果

問題 No.697 池の数はいくつか
ユーザー tnakao0123tnakao0123
提出日時 2018-06-18 17:04:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,039 ms / 6,000 ms
コード長 1,437 bytes
コンパイル時間 889 ms
コンパイル使用メモリ 90,152 KB
実行使用メモリ 21,324 KB
最終ジャッジ日時 2024-04-25 20:09:01
合計ジャッジ時間 9,311 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 117 ms
10,700 KB
testcase_25 AC 119 ms
10,700 KB
testcase_26 AC 116 ms
10,568 KB
testcase_27 AC 118 ms
10,572 KB
testcase_28 AC 116 ms
10,696 KB
testcase_29 AC 928 ms
21,192 KB
testcase_30 AC 1,016 ms
21,324 KB
testcase_31 AC 953 ms
21,068 KB
testcase_32 AC 1,037 ms
21,188 KB
testcase_33 AC 1,039 ms
21,188 KB
testcase_34 AC 1,034 ms
21,188 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:48:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |   scanf("%d%d", &h, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:53:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   53 |       scanf("%d", &a);
      |       ~~~~~^~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 697.cc: No.697 池の数はいくつか - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 3000;
const int MAX_W = 3000;

const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 };

/* typedef */

typedef pair<int,int> pii;

/* global variables */

bool flds[MAX_H][MAX_W], used[MAX_H][MAX_W];

/* subroutines */

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) {
      int a;
      scanf("%d", &a);
      flds[y][x] = (a > 0);
    }

  int cnt = 0;
  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      if (flds[y][x] && ! used[y][x]) {
	cnt++;
	used[y][x] = true;
	queue<pii> q;
	q.push(pii(x, y));

	while (! q.empty()) {
	  pii u = q.front(); q.pop();
	  int &ux = u.first, &uy = u.second;

	  for (int di = 0; di < 4; di++) {
	    int vx = ux + dxs[di], vy = uy + dys[di];
	    if (vx >= 0 && vx < w && vy >= 0 && vy < h &&
		flds[vy][vx] && ! used[vy][vx]) {
	      used[vy][vx] = true;
	      q.push(pii(vx, vy));
	    }
	  }
	}
      }

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