結果

問題 No.697 池の数はいくつか
ユーザー alexara1123alexara1123
提出日時 2019-09-26 12:26:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 947 ms / 6,000 ms
コード長 2,047 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 101,420 KB
実行使用メモリ 223,800 KB
最終ジャッジ日時 2024-04-25 20:35:36
合計ジャッジ時間 8,521 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 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,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 94 ms
36,356 KB
testcase_25 AC 94 ms
38,412 KB
testcase_26 AC 94 ms
38,440 KB
testcase_27 AC 96 ms
36,296 KB
testcase_28 AC 98 ms
36,240 KB
testcase_29 AC 947 ms
223,800 KB
testcase_30 AC 839 ms
149,296 KB
testcase_31 AC 933 ms
221,488 KB
testcase_32 AC 829 ms
147,796 KB
testcase_33 AC 826 ms
147,800 KB
testcase_34 AC 803 ms
148,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <functional>
#include <cmath>
#include <cassert>
#include <string>
#include <iostream>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll MOD = 1000000007;
ll INF = 1LL << 60;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val)
{
   std::fill((T *)array, (T *)(array + N), val);
}
ll a[3010][3010];
bool visited[3010][3010];
int main()
{
   ios::sync_with_stdio(false);
   cin.tie(0);

   ll h, w;
   cin >> h >> w;
   ll ans = 0;
   queue<P> one;
   for (ll i = 0; i < h; i++)
   {
      for (ll j = 0; j < w; j++)
      {
         cin >> a[i][j];
         if (a[i][j] == 1)
         {
            one.push(P(j, i));
            ans = 1;
         }
      }
   }
   if(one.empty()){
      cout << 0 << endl;
      return 0;
   }

   queue<P> q;
   q.push(P(one.front().first , one.front().second));
   
   one.pop();
   int xl[] = {0, 1, 0, -1};
   int yl[] = {-1, 0, 1, 0};
   while (!one.empty())
   {
      if (q.empty())
      {
         while(true){
            P pp = P(one.front().first, one.front().second);
            one.pop();
            //cout << pp.second << pp.first << endl;
            if(a[pp.second][pp.first]==1){
               
               q.push(pp);
               ans++;
               break;
            }else{
               if(one.empty()){
                  cout << ans << endl;
                  return 0;
               }
               continue;
            }
         }
      }
      ll x = q.front().first;
      ll y = q.front().second;
      q.pop();
      a[y][x] = 0;

      for (int i = 0; i < 4; i++)
      {
         ll nx = x + xl[i];
         ll ny = y + yl[i];
         if (nx < 0 || ny < 0 || nx >= w || ny >= h || a[ny][nx] == 0)
            continue;
         q.push(P(nx, ny));
         a[ny][nx] = 0;
      }

   }

   cout << ans << endl;
}
0