結果

問題 No.157 2つの空洞
ユーザー akichiakichi
提出日時 2017-08-26 00:38:38
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 2,117 bytes
コンパイル時間 1,962 ms
コンパイル使用メモリ 107,904 KB
実行使用メモリ 21,888 KB
最終ジャッジ日時 2024-04-23 15:54:04
合計ジャッジ時間 2,196 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
21,760 KB
testcase_01 AC 33 ms
21,504 KB
testcase_02 AC 33 ms
21,376 KB
testcase_03 AC 34 ms
21,504 KB
testcase_04 AC 33 ms
21,376 KB
testcase_05 AC 34 ms
21,632 KB
testcase_06 AC 33 ms
21,760 KB
testcase_07 AC 32 ms
21,504 KB
testcase_08 AC 33 ms
21,248 KB
testcase_09 AC 34 ms
21,504 KB
testcase_10 AC 33 ms
21,888 KB
testcase_11 AC 32 ms
21,760 KB
testcase_12 AC 31 ms
21,760 KB
testcase_13 AC 30 ms
21,504 KB
testcase_14 AC 31 ms
21,376 KB
testcase_15 AC 32 ms
21,632 KB
testcase_16 AC 31 ms
21,504 KB
testcase_17 AC 33 ms
21,632 KB
testcase_18 AC 30 ms
21,504 KB
testcase_19 AC 28 ms
21,504 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.Drawing;
using System.Linq;
class Program {

    static void Main() {
        int[] wh = Console.ReadLine().Split().Select(int.Parse).ToArray();
        w = wh[0]; h = wh[1];
        chars = new char[w, h];

        Point start = new Point();
        bool dotFound = false;
        for (int y = 0; y < h; y++) {
            string cs = Console.ReadLine();
            for (int x = 0; x < w; x++) {
                if (cs[x] == '.') {
                    if (!dotFound) {
                        dotFound = true;
                        start = new Point(x, y);
                    }
                }
                chars[x, y] = cs[x];
            }
        }

        dfs(start);

        int ans = 1000;
        foreach (Point a in aa) {
            for (int dx = -1; dx <= 1; dx += 2) {
                for (int x = a.X; 0 <= x && x < w; x += dx) {
                    for (int dy = -1; dy <= 1; dy += 2) {
                        for (int y = a.Y; 0 <= y && y < h; y += dy) {
                            if (x == a.X && y == a.Y) continue;
                            if (chars[x, y] == 'A') break;
                            if (chars[x, y] == '.') {
                                int score = Math.Abs(a.X - x) + Math.Abs(a.Y - y) - 1;
                                ans = Math.Min(ans, score);
                                break;
                            }
                        }
                    }
                }
            }
        }
        Console.WriteLine(ans);
    }

    static char[,] chars;
    static HashSet<Point> aa = new HashSet<Point>();
    static int w, h;

    static void dfs(Point p) {
        chars[p.X, p.Y] = 'A';
        aa.Add(p);

        Size[] dirs = { new Size(1, 0), new Size(-1, 0), new Size(0, 1), new Size(0, -1) };
        foreach (Size dir in dirs) {
            Point np = p + dir;
            if (np.X < 0 || w <= np.X || np.Y < 0 || h <= np.Y) {
                continue;
            }

            if (chars[np.X, np.Y] == '.') {
                dfs(np);
            }
        }
    }
}
0