結果

問題 No.697 池の数はいくつか
ユーザー chika0707chika0707
提出日時 2019-04-17 23:09:59
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 5,471 ms / 6,000 ms
コード長 2,972 bytes
コンパイル時間 1,029 ms
コンパイル使用メモリ 113,908 KB
実行使用メモリ 161,460 KB
最終ジャッジ日時 2024-04-25 20:26:10
合計ジャッジ時間 31,245 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
26,108 KB
testcase_01 AC 26 ms
26,028 KB
testcase_02 AC 26 ms
26,104 KB
testcase_03 AC 28 ms
28,076 KB
testcase_04 AC 26 ms
26,232 KB
testcase_05 AC 29 ms
28,204 KB
testcase_06 AC 27 ms
26,112 KB
testcase_07 AC 27 ms
27,952 KB
testcase_08 AC 25 ms
26,248 KB
testcase_09 AC 26 ms
26,288 KB
testcase_10 AC 26 ms
26,104 KB
testcase_11 AC 26 ms
26,100 KB
testcase_12 AC 25 ms
25,852 KB
testcase_13 AC 26 ms
23,860 KB
testcase_14 AC 26 ms
24,112 KB
testcase_15 AC 27 ms
26,168 KB
testcase_16 AC 26 ms
27,948 KB
testcase_17 AC 27 ms
26,108 KB
testcase_18 AC 26 ms
28,092 KB
testcase_19 AC 26 ms
26,160 KB
testcase_20 AC 26 ms
25,976 KB
testcase_21 AC 26 ms
28,080 KB
testcase_22 AC 26 ms
26,280 KB
testcase_23 AC 28 ms
27,892 KB
testcase_24 AC 417 ms
54,944 KB
testcase_25 AC 423 ms
53,024 KB
testcase_26 AC 429 ms
51,152 KB
testcase_27 AC 436 ms
56,992 KB
testcase_28 AC 431 ms
55,112 KB
testcase_29 AC 5,471 ms
161,460 KB
testcase_30 AC 3,630 ms
160,824 KB
testcase_31 AC 5,419 ms
161,272 KB
testcase_32 AC 3,602 ms
157,380 KB
testcase_33 AC 3,678 ms
161,352 KB
testcase_34 AC 3,590 ms
159,284 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 Input;

public class Prog
{
    private const int INF = 1000000007;
    private const long INFINITY = 9223372036854775807;
    private struct Pair { public int x, y; }
    private static int[] dx = { 1, 0, -1, 0 };
    private static int[] dy = { 0, -1, 0, 1 };
    public static void Main()
    {
        int H = NextInt();
        int W = NextInt();
        int[,] map = new int[H, W];
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++) map[i, j] = NextInt();
        UnionFind UF = new UnionFind(H * W);
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++)
            {
                if (map[i, j] != 1) continue;
                for (int d = 0; d < 4; d++)
                {
                    int xx = j + dx[d];
                    int yy = i + dy[d];
                    if (xx < 0 || W <= xx || yy < 0 || H <= yy) continue;
                    if (map[yy, xx] == 1) UF.Unite(i * W + j, yy * W + xx);
                }
            }
        HashSet<int> pos = new HashSet<int>();
        for (int i = 0; i < H; i++)
            for (int j = 0; j < W; j++)
            {
                if (map[i, j] == 0) { continue; }
                int k = i * W + j;
                int r = UF.Root(k);
                if (!pos.Contains(r)) pos.Add(r);
            }
        Console.WriteLine(pos.Count());
    }
}

public class UnionFind
{
    public List<int> parent = new List<int>();

    public UnionFind(int N)
    {
        for (int i = 0; i < N; i++) parent.Add(i);
    }

    public int Root(int a)
    {
        if (parent[a] == a) return a;
        else return parent[a] = Root(parent[a]);
    }

    public void Unite(int a, int b)
    {
        a = Root(a);
        b = Root(b);
        if (a == b) return;
        parent[a] = parent[b];
    }

    public bool Same(int a, int b) { return (Root(a) == Root(b)); }
}

public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}
0