結果

問題 No.697 池の数はいくつか
ユーザー bluemegane
提出日時 2021-05-11 15:18:41
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,338 ms / 6,000 ms
コード長 1,680 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 107,008 KB
実行使用メモリ 48,212 KB
最終ジャッジ日時 2024-11-08 08:58:30
合計ジャッジ時間 17,854 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
    public static int[] dx, dy;
    static void Main()
    {
        dx = new int[] { 0, 1, 0, -1 };
        dy = new int[] { 1, 0, -1, 0 };
        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, int sx, int sy)
    {
        var q = new Queue<P>();
        q.Enqueue(new P { x = sx, y = sy });
        map[sx, sy] = false;
        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])
                {
                    q.Enqueue(new P { x = nx, y = ny });
                    map[nx, ny] = false;
                }
            }
        }
    }
    static void getAns(bool[,] map)
    {
        var count = 0;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
            {
                if (map[i, j])
                {
                    count++;
                    Bfs(map, i, j);
                }
            }
        Console.WriteLine(count);
    }
}
0