結果

問題 No.157 2つの空洞
ユーザー syoken_desukasyoken_desuka
提出日時 2015-02-27 00:03:55
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 5,102 bytes
コンパイル時間 3,781 ms
コンパイル使用メモリ 108,688 KB
実行使用メモリ 24,712 KB
最終ジャッジ日時 2023-09-06 02:57:54
合計ジャッジ時間 4,882 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
24,632 KB
testcase_01 AC 62 ms
22,520 KB
testcase_02 AC 62 ms
22,456 KB
testcase_03 AC 63 ms
22,548 KB
testcase_04 AC 60 ms
22,600 KB
testcase_05 AC 63 ms
22,444 KB
testcase_06 AC 62 ms
22,516 KB
testcase_07 AC 63 ms
24,448 KB
testcase_08 AC 63 ms
22,656 KB
testcase_09 AC 65 ms
24,508 KB
testcase_10 AC 63 ms
24,680 KB
testcase_11 AC 64 ms
24,712 KB
testcase_12 AC 62 ms
22,528 KB
testcase_13 AC 63 ms
22,464 KB
testcase_14 AC 61 ms
24,520 KB
testcase_15 AC 63 ms
24,536 KB
testcase_16 AC 64 ms
22,548 KB
testcase_17 AC 64 ms
22,620 KB
testcase_18 AC 64 ms
22,564 KB
testcase_19 AC 65 ms
22,360 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;
using System.Collections;
using System.Linq;
using System.Text;
using sc = Scanner;

class Program
{
    static void Main(string[] args)
    {
        Solve();

#if DEBUG
        System.Console.WriteLine("続行するには何かキーを押してください...");
        System.Console.ReadKey();
#endif

    }
    
    static int[,] Field;
    static int W;
    static int H;
    static int[] vx = { 1, 0, -1, 0 };
    static int[] vy = { 0, 1, 0, -1 };

    /// <summary>
    /// ここでとく
    /// </summary>
    static void Solve()
    {
        int w = sc.NextInt();
        int h = sc.NextInt();
        W = w;
        H = h;
        Field = new int[h, w];
        int min_displace = int.MaxValue;
        HashSet<Tuple<int,int>> group1 = new HashSet<Tuple<int,int>>();
        HashSet<Tuple<int,int>> group2 = new HashSet<Tuple<int,int>>();

        for (int i = 0; i < h; i++)
        {
            string line = Console.ReadLine();
            for (int j = 0; j < w; j++)
            {
                Field[i, j] = line[j] == '#' ? 1 : 0; 
            }
        }

        // group wakeru
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (Field[i,j] == 0)
                {
                    dfs(i, j, group1);
#if DEBUG
                    Console.WriteLine(1230000);
#endif 
                    i = h;
                    break;
                }
            }
        }
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (Field[i, j] == 0)
                {
                    dfs(i, j, group2);
                    break;
                }
            }
        }
        //man hatten kyori wo zenkeisan
        foreach (var item in group1)
        {
            foreach (var itemm in group2)
            {
                min_displace = Math.Min(min_displace, Math.Abs(item.Item1 - itemm.Item1) + Math.Abs(item.Item2 - itemm.Item2) - 1);
            }
        }

        Console.WriteLine(min_displace);
#if DEBUG
        Console.WriteLine("local check");
#endif 
    }

    static void dfs(int x, int y, HashSet<Tuple<int,int>> group)
    {
#if DEBUG
        Console.WriteLine(x+"  "+y);
#endif 
        Field[x , y] = 1;
        group.Add(new Tuple<int, int>(x , y ));
        for (int i = 0; i < 4; i++)
        {
            if ( x+vx[i] <0 || H <= x+vx[i] )
            {
                continue;
            }
            if (y + vy[i] < 0 || W <= y + vy[i])
            {
                continue;
            }

            if (Field[x+vx[i],y+vy[i]] == 0)
            {
                dfs(x + vx[i], y + vy[i], group);
                Field[x + vx[i], y + vy[i]] = 1;
            }                

        }
    }
}

/// <summary>
/// Java風のスキャナー,自作(某最速の人を参考)
/// </summary>
public static class Scanner
{
    public static string NextString()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
            tmp += readData;
        }
        return tmp;
    }

    public static string[] NextStrArray()
    {
        return Console.ReadLine().Split(' ');
    }

    public static long[] NextLongArray()
    {
        string[] s = NextStrArray();
        long[] a = new long[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = long.Parse(s[i]);
        }
        return a;
    }
    public static int[] NextIntArray()
    {

        string[] s = NextStrArray();
        int[] a = new int[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = int.Parse(s[i]);
        }
        return a;
    }
    public static int NextInt()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
        
            tmp += readData;
        }
        return int.Parse(tmp);
    }
    public static double NextDouble()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
            tmp += readData;
        }
        return double.Parse(tmp);
    }
    public static long NextLong()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
            tmp += readData;
        }
        return long.Parse(tmp);
    }
    public static double[] NextDoubleArray()
    {
        string[] s = NextStrArray();
        double[] a = new double[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = double.Parse(s[i]);
        }
        return a;
    }
}
0