結果

問題 No.697 池の数はいくつか
ユーザー iwkjoseciwkjosec
提出日時 2018-06-16 17:04:15
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 1,086 ms
コンパイル使用メモリ 113,380 KB
実行使用メモリ 81,364 KB
最終ジャッジ日時 2024-05-04 05:56:25
合計ジャッジ時間 13,643 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
21,684 KB
testcase_01 WA -
testcase_02 AC 20 ms
21,940 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 22 ms
22,068 KB
testcase_09 AC 22 ms
23,856 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 21 ms
23,720 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 23 ms
23,704 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 23 ms
23,840 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1,201 ms
56,176 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

class Program
{
    static void Main()
    {
        var HW = ReadLine().Split();
        var H = int.Parse(HW[0]);
        var W = int.Parse(HW[1]);
        var map = new bool[H, W];
        var zero = 0;
        for (int i = 0; i < H; i++)
        {
            var line = ReadLine().Split();
            for (int j = 0; j < W; j++)
            {
                if (line[j] == "0") zero++;
                else map[i, j] = true;
            }
        }

        if (zero == 0)
        {
            WriteLine(1);
            return;
        }

        var uf = new UnionFind(H * W);
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (!map[i, j]) continue;
                var m = i * W + j;
                if (j != W - 1 && map[i, j + 1]) uf.Unite(m, m + 1);
                if (i != H - 1 && map[i + 1, j]) uf.Unite(m, m + W);
            }
        }
        WriteLine(uf.count - zero);
    }
}

class UnionFind
{
    int[] parent;
    public int count;

    public UnionFind(int size)
    {
        var b = new int[size];
        for (int i = 0; i < b.Length; i++)
        {
            b[i] = i;
        }
        parent = b;
        count = size;
    }

    int Root(int x) => parent[x] == x ? x : parent[x] = Root(parent[x]);

    public bool Same(int x, int y) => Root(x) == Root(y);

    public void Unite(int x, int y)
    {
        x = Root(x);
        y = Root(y);
        if (x == y) return;
        count++;
        parent[x] = y;
    }
}
0