結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
bluemegane
|
| 提出日時 | 2021-05-11 15:08:57 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 2,511 ms / 6,000 ms |
| コード長 | 1,764 bytes |
| コンパイル時間 | 946 ms |
| コンパイル使用メモリ | 106,496 KB |
| 実行使用メモリ | 48,756 KB |
| 最終ジャッジ日時 | 2024-11-08 08:58:08 |
| 合計ジャッジ時間 | 18,940 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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.
ソースコード
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);
}
}
bluemegane