結果

問題 No.157 2つの空洞
ユーザー bluemeganebluemegane
提出日時 2018-10-05 20:06:05
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,267 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 113,324 KB
実行使用メモリ 19,200 KB
最終ジャッジ日時 2024-04-20 16:55:05
合計ジャッジ時間 2,184 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
19,200 KB
testcase_01 AC 28 ms
18,944 KB
testcase_02 AC 28 ms
18,944 KB
testcase_03 AC 28 ms
19,200 KB
testcase_04 AC 28 ms
19,200 KB
testcase_05 AC 28 ms
18,944 KB
testcase_06 AC 30 ms
18,816 KB
testcase_07 AC 28 ms
18,944 KB
testcase_08 AC 29 ms
19,072 KB
testcase_09 AC 28 ms
18,944 KB
testcase_10 AC 29 ms
18,944 KB
testcase_11 AC 29 ms
18,816 KB
testcase_12 AC 28 ms
19,200 KB
testcase_13 AC 28 ms
19,072 KB
testcase_14 AC 29 ms
18,944 KB
testcase_15 AC 28 ms
18,816 KB
testcase_16 AC 28 ms
19,072 KB
testcase_17 AC 28 ms
18,816 KB
testcase_18 AC 28 ms
19,072 KB
testcase_19 AC 28 ms
19,072 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 void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var w = int.Parse(line[0]);
        var h = int.Parse(line[1]);
        var map = new int[h, w];
        var fx = 0; var fy = 0;
        var find = false;
        for (int i = 0; i < h; i++)
        {
            var s = Console.ReadLine().Trim();
            for (int j = 0; j < w; j++)
                if (s[j] == '.')
                {
                    if (!find) { fx = i; fy = j; find = true; }
                    map[i, j] = 1;
                }
        }
        var a1 = goDfs(map, fx, fy);
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
                if (map[i, j] == 1) { fx = i; fy = j; goto next; }
        next:;
        var a2 = goDfs(map, fx, fy);
        var ans = getAns(a1, a2);
        Console.WriteLine(ans - 1);
    }
    public static int getAns(List<P> a, List<P> b)
    {
        var ans = int.MaxValue;
        foreach (var x in a)
            foreach (var y in b)
            {
                var w = Math.Abs(x.x - y.x) + Math.Abs(x.y - y.y);
                ans = Math.Min(ans, w);
            }
        return ans;
    }
    public static List<P> goDfs(int[,] map, int fx, int fy)
    {
        var ans = new List<P>();
        var h = map.GetLength(0);
        var w = map.GetLength(1);
        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 = fx, y = fy });
        while (q.Count() > 0)
        {
            var a = q.Dequeue();
            if (map[a.x, a.y] != 2)
            {
                ans.Add(new P { x = a.x, y = a.y });
                map[a.x, a.y] = 2;
                for (int i = 0; i < 4; i++)
                {
                    var nx = a.x + dx[i];
                    var ny = a.y + dy[i];
                    if (nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx, ny] == 1)
                        q.Enqueue(new P { x = nx, y = ny });
                }
            }
        }
        return ans;
    }
}
0