結果
問題 | No.402 最も海から遠い場所 |
ユーザー |
![]() |
提出日時 | 2016-07-26 21:58:02 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 2,154 ms / 3,000 ms |
コード長 | 3,891 bytes |
コンパイル時間 | 4,199 ms |
コンパイル使用メモリ | 115,880 KB |
実行使用メモリ | 282,476 KB |
最終ジャッジ日時 | 2024-11-06 17:09:26 |
合計ジャッジ時間 | 13,275 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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;}}