結果

問題 No.157 2つの空洞
ユーザー mbanmban
提出日時 2016-12-25 15:00:26
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 4,267 bytes
コンパイル時間 2,498 ms
コンパイル使用メモリ 105,724 KB
実行使用メモリ 22,908 KB
最終ジャッジ日時 2023-08-21 08:59:31
合計ジャッジ時間 4,887 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
20,792 KB
testcase_01 AC 58 ms
20,820 KB
testcase_02 AC 59 ms
22,884 KB
testcase_03 AC 59 ms
20,896 KB
testcase_04 AC 59 ms
20,864 KB
testcase_05 AC 59 ms
20,760 KB
testcase_06 AC 59 ms
20,880 KB
testcase_07 AC 59 ms
22,856 KB
testcase_08 AC 59 ms
20,840 KB
testcase_09 AC 60 ms
20,896 KB
testcase_10 AC 59 ms
22,908 KB
testcase_11 AC 59 ms
20,840 KB
testcase_12 AC 58 ms
18,916 KB
testcase_13 AC 59 ms
20,896 KB
testcase_14 AC 59 ms
22,756 KB
testcase_15 AC 59 ms
20,856 KB
testcase_16 AC 59 ms
22,824 KB
testcase_17 AC 59 ms
20,904 KB
testcase_18 AC 59 ms
20,828 KB
testcase_19 AC 60 ms
20,808 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

class Magatro
{
    static int W, H;
    static string[] C;
    static int[] Xp = new int[] { 1, -1, 0, 0 };
    static int[] Yp = new int[] { 0, 0, 1, -1 };

    static void Main()
    {
        Read();
        List<XY> L1 = new List<XY>();
        bool[,] Looked = new bool[H, W];
        bool q = false;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (C[i][j] == '.')
                {
                    Stack<XY> ST = new Stack<XY>();
                    ST.Push(new XY(i, j));
                    Looked[i, j] = true;
                    while (ST.Count > 0)
                    {
                        XY Po = ST.Peek();
                        bool a = true;
                        for(int k = 0; k < 4; k++)
                        {
                            XY N = new XY(Po.Y+Yp[k],Po.X+Xp[k]);
                            if (C[N.Y][N.X] == '.')
                            {
                                if (!Looked[N.Y, N.X])
                                {
                                //    Console.WriteLine("a");
                                    Looked[N.Y, N.X] = true;
                                    a = false;
                                    ST.Push(N);
                                    break;
                                }
                            }
                        }
                        if (a)
                        {
                        L1.Add(ST.Pop());
                        }
                    }
                    q = true;
                    break;
                }

            }
            if (q)
            {
                break;
            }
        }
        q = false;
        List<XY> L2 = new List<XY>();
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (C[i][j] == '.'&&!Looked[i,j])
                {
                    Stack<XY> ST = new Stack<XY>();
                    ST.Push(new XY(i, j));
                    Looked[i, j] = true;
                    while (ST.Count > 0)
                    {
                        XY Po = ST.Peek();
                        bool a = true;
                        for (int k = 0; k < 4; k++)
                        {
                            XY N = new XY(Po.Y + Yp[k], Po.X + Xp[k]);
                            if (C[N.Y][N.X] == '.')
                            {
                                if (!Looked[N.Y, N.X])
                                {
                                 //   Console.WriteLine("{0} {1}", N.Y, N.X);
                                    Looked[N.Y, N.X] = true;
                                    a = false;
                                    ST.Push(N);
                                    break;
                                }
                            }
                        }
                        if (a)
                        {
                            L2.Add(ST.Pop());
                        }
                    }
                    q = true;
                    break;
                }

            }
            if (q)
            {
                break;
            }
        }
        int min = int.MaxValue;
        for(int i = 0; i < L1.Count; i++)
        {
            for(int j = 0; j < L2.Count; j++)
            {
                min = Math.Min(min, Dist(L1[i], L2[j]));
            }
        }
        Console.WriteLine(min);
    }
    static int Dist(XY a,XY b)
    {
        return Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y)-1;
    }
    static void Read()
    {
        string[] s = Console.ReadLine().Split(' ');
        W = int.Parse(s[0]);
        H = int.Parse(s[1]);
        C = new string[H];
        for(int i = 0; i < H; i++)
        {
            C[i] = Console.ReadLine();
        }
    }

}

struct XY
{
    public int Y, X;
    public XY(int y,int x)
    {
        Y = y;
        X = x;
    }
    public static XY operator +(XY a,XY b)
    {
        return new XY(a.Y + b.Y, a.X + b.X);
    }
}
0