結果

問題 No.697 池の数はいくつか
ユーザー bluemeganebluemegane
提出日時 2021-05-11 15:08:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,444 ms / 6,000 ms
コード長 1,764 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 113,104 KB
実行使用メモリ 56,964 KB
最終ジャッジ日時 2024-04-25 21:04:48
合計ジャッジ時間 18,695 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
27,020 KB
testcase_01 AC 29 ms
25,052 KB
testcase_02 AC 28 ms
24,920 KB
testcase_03 AC 28 ms
26,840 KB
testcase_04 AC 27 ms
27,024 KB
testcase_05 AC 26 ms
25,056 KB
testcase_06 AC 27 ms
25,012 KB
testcase_07 AC 28 ms
26,832 KB
testcase_08 AC 25 ms
24,028 KB
testcase_09 AC 27 ms
24,880 KB
testcase_10 AC 27 ms
24,924 KB
testcase_11 AC 29 ms
24,792 KB
testcase_12 AC 25 ms
24,364 KB
testcase_13 AC 27 ms
27,284 KB
testcase_14 AC 27 ms
25,140 KB
testcase_15 AC 27 ms
24,876 KB
testcase_16 AC 28 ms
24,668 KB
testcase_17 AC 28 ms
25,052 KB
testcase_18 AC 27 ms
25,048 KB
testcase_19 AC 28 ms
27,028 KB
testcase_20 AC 27 ms
24,980 KB
testcase_21 AC 27 ms
24,928 KB
testcase_22 AC 28 ms
24,792 KB
testcase_23 AC 28 ms
25,108 KB
testcase_24 AC 279 ms
49,776 KB
testcase_25 AC 277 ms
47,604 KB
testcase_26 AC 275 ms
49,144 KB
testcase_27 AC 280 ms
47,472 KB
testcase_28 AC 275 ms
47,220 KB
testcase_29 AC 2,444 ms
56,404 KB
testcase_30 AC 2,228 ms
54,660 KB
testcase_31 AC 2,423 ms
56,248 KB
testcase_32 AC 2,213 ms
56,768 KB
testcase_33 AC 2,217 ms
56,940 KB
testcase_34 AC 2,221 ms
56,964 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Linq;
using System.Collections.Generic;
using System;

public class P
{
    public int x { get; set; }
    public int y { get; set; }
}

public class Hello
{
    public static int h, w;
    static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        h = int.Parse(line[0]);
        w = int.Parse(line[1]);
        var map = new bool[h, w];
        for (int i = 0; i < h; i++)
        {
            line = Console.ReadLine().Trim().Split(' ');
            for (int j = 0; j < w; j++)
                if (line[j] == "1") map[i, j] = true;
        }
        getAns(map);
    }
    static void Bfs(bool[,] map, bool[,] visited, int sx, int sy)
    {
        var dx = new int[] { 0, 1, 0, -1 };
        var dy = new int[] { 1, 0, -1, 0 };
        var q = new Queue<P>();
        q.Enqueue(new P { x = sx, y = sy });
        visited[sx, sy] = true;
        while (q.Count() > 0)
        {
            var t = q.Dequeue();
            for (int i = 0; i < 4; i++)
            {
                var nx = t.x + dx[i];
                var ny = t.y + dy[i];
                if (nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx, ny] && !visited[nx, ny])
                {
                    q.Enqueue(new P { x = nx, y = ny });
                    visited[nx, ny] = true;
                }
            }
        }
    }
    static void getAns(bool[,] map)
    {
        var visited = new bool[h, w];
        var count = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
            {
                if (map[i, j] && !visited[i, j])
                {
                    count++;
                    Bfs(map, visited, i, j);
                }
            }
        Console.WriteLine(count);
    }
}
0