結果

問題 No.157 2つの空洞
ユーザー tassi_2012tassi_2012
提出日時 2015-02-27 02:08:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 3,009 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 106,864 KB
実行使用メモリ 22,976 KB
最終ジャッジ日時 2023-09-06 03:23:17
合計ジャッジ時間 4,014 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
18,792 KB
testcase_01 AC 56 ms
20,852 KB
testcase_02 AC 56 ms
22,976 KB
testcase_03 AC 56 ms
20,932 KB
testcase_04 AC 55 ms
20,868 KB
testcase_05 AC 56 ms
20,852 KB
testcase_06 AC 55 ms
20,740 KB
testcase_07 AC 57 ms
22,788 KB
testcase_08 AC 55 ms
20,820 KB
testcase_09 AC 56 ms
18,664 KB
testcase_10 AC 56 ms
20,912 KB
testcase_11 AC 55 ms
20,936 KB
testcase_12 AC 58 ms
22,868 KB
testcase_13 AC 56 ms
22,804 KB
testcase_14 AC 56 ms
20,752 KB
testcase_15 AC 57 ms
20,876 KB
testcase_16 AC 56 ms
20,936 KB
testcase_17 AC 58 ms
20,860 KB
testcase_18 AC 58 ms
21,016 KB
testcase_19 AC 56 ms
20,816 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;

class program
{
    public static void Main()
    {
        var str = Console.ReadLine().Split(' ');
        var W = int.Parse(str[0]);
        var H = int.Parse(str[1]);

        var room = new String[H, W];

        for (int i = 0; i < H; i++)
        {
            var buf = Console.ReadLine();
            for (int j = 0; j < W; j++)
            {
                room[i, j] = buf.ToCharArray()[j].ToString();
            }
        }

        var room1 = new bool[H, W];
        var flag = false;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (room[i, j] == ".")
                {
                    room1[i, j] = true;
                    check(room, room1, j, i);
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }

        var room2 = new bool[H, W];
        flag = false;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (room[i, j] == "." && !room1[i, j])
                {
                    room2[i, j] = true;
                    check(room, room2, j, i);
                    flag = true;
                    break;
                }
            }
            if (flag) break;
        }

        int minWight = 1000;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                var point1 = room1[i, j];
                for (int k = 0; k < H; k++)
                {
                    for (int l = 0; l < W; l++)
                    {
                        if (point1 && room2[k, l])
                        {
                            var man = System.Math.Abs(i - k) + System.Math.Abs(j - l);
                            man--;
                            minWight = minWight > man ? man : minWight;
                        }
                    }
                }
            }
        }

        Console.WriteLine(minWight);
    }

    public static void check(String[,] roombase, bool[,] room, int X, int Y)
    {
        if (roombase[Y, X + 1] == ".")
        {
            if (!room[Y, X + 1])
            {
                room[Y, X + 1] = true;
                check(roombase, room, X + 1, Y);
            }
        }

        if (roombase[Y - 1, X] == ".")
        {
            if (!room[Y - 1, X])
            {
                room[Y - 1, X] = true;
                check(roombase, room, X, Y - 1);
            }

        }

        if (roombase[Y, X - 1] == ".")
        {
            if (!room[Y, X - 1])
            {
                room[Y, X - 1] = true;
                check(roombase, room, X - 1, Y);
            }
        }

        if (roombase[Y + 1, X] == ".")
        {
            if (!room[Y + 1, X])
            {
                room[Y + 1, X] = true;
                check(roombase, room, X, Y + 1);
            }
        }
    }
}
0