結果

問題 No.402 最も海から遠い場所
ユーザー eitahoeitaho
提出日時 2016-07-26 21:58:02
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,209 ms / 3,000 ms
コード長 3,891 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 110,016 KB
実行使用メモリ 282,040 KB
最終ジャッジ日時 2023-08-06 14:04:25
合計ジャッジ時間 14,383 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
20,732 KB
testcase_01 AC 60 ms
22,768 KB
testcase_02 AC 61 ms
18,664 KB
testcase_03 AC 59 ms
20,792 KB
testcase_04 AC 58 ms
20,668 KB
testcase_05 AC 60 ms
20,716 KB
testcase_06 AC 61 ms
20,836 KB
testcase_07 AC 59 ms
22,772 KB
testcase_08 AC 60 ms
20,716 KB
testcase_09 AC 59 ms
20,728 KB
testcase_10 AC 59 ms
20,796 KB
testcase_11 AC 61 ms
20,852 KB
testcase_12 AC 59 ms
22,780 KB
testcase_13 AC 69 ms
22,952 KB
testcase_14 AC 63 ms
20,788 KB
testcase_15 AC 113 ms
23,076 KB
testcase_16 AC 138 ms
27,668 KB
testcase_17 AC 1,049 ms
120,372 KB
testcase_18 AC 2,209 ms
105,560 KB
testcase_19 AC 2,044 ms
282,040 KB
testcase_20 AC 1,933 ms
107,736 KB
testcase_21 AC 1,948 ms
211,724 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.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int H = Reader.Int() + 2, W = Reader.Int() + 2;
        var grid = Reader.StringArray(H - 2);
        grid = Surround(grid, '.');
        var que = new Queue<int>();
        var minDist = new int[H, W];
        Fill(minDist, H + W + 10);
        Func<int, int, bool> IsInside = (r, c) => r >= 0 && r < H && c >= 0 && c < W;

        for (int r = 0; r < H; r++)
            for (int c = 0; c < W; c++)
                if (grid[r][c] == '.') { que.Enqueue(r); que.Enqueue(c); minDist[r, c] = 0; }
        while (que.Count > 0)
        {
            int r = que.Dequeue(), c = que.Dequeue();
            for (int dr = -1; dr <= 1; dr++)
                for (int dc = -1; dc <= 1; dc++)
                {
                    int nr = r + dr, nc = c + dc;
                    if (IsInside(nr, nc) && CheckMin(ref minDist[nr, nc], minDist[r, c] + 1))
                    {
                        que.Enqueue(nr); que.Enqueue(nc);
                    }
                }
        }
        int ans = 0;
        for (int r = 0; r < H; r++)
            for (int c = 0; c < W; c++)
                CheckMax(ref ans, minDist[r, c]);

        Console.WriteLine(ans);
    }

    bool CheckMin(ref int a, int b) { if (b < a) { a = b; return true; } return false; }
    bool CheckMax(ref int a, int b) { if (b > a) { a = b; return true; } return false; }

    string[] Surround(string[] grid, char c)
    {
        int H = grid.Length + 2, W = grid[0].Length + 2;
        var res = new string[H];
        for (int i = 0; i < H; i++)
            if (i == 0 || i == H - 1) res[i] = new string(c, W);
            else res[i] = c + grid[i - 1] + c;
        return res;
    }

    void Fill<T>(T[,] A, T val)
    {
        for (int a = 0; a < A.GetLength(0); a++)
            for (int b = 0; b < A.GetLength(1); b++)
                A[a, b] = val;
    }
}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0