結果

問題 No.13 囲みたい!
ユーザー AreTrashAreTrash
提出日時 2016-06-01 00:32:26
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 2,699 bytes
コンパイル時間 2,766 ms
コンパイル使用メモリ 107,904 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-04-21 12:26:49
合計ジャッジ時間 3,787 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
18,048 KB
testcase_01 AC 21 ms
18,176 KB
testcase_02 AC 21 ms
18,176 KB
testcase_03 AC 24 ms
19,200 KB
testcase_04 AC 24 ms
19,584 KB
testcase_05 AC 24 ms
19,328 KB
testcase_06 AC 23 ms
18,944 KB
testcase_07 AC 25 ms
18,944 KB
testcase_08 AC 27 ms
18,944 KB
testcase_09 AC 27 ms
18,944 KB
testcase_10 AC 26 ms
18,304 KB
testcase_11 AC 25 ms
18,560 KB
testcase_12 AC 23 ms
18,048 KB
testcase_13 AC 26 ms
17,920 KB
testcase_14 AC 23 ms
18,432 KB
testcase_15 AC 22 ms
18,304 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

namespace No13{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var W = sr.Next<int>();
            var H = sr.Next<int>();
            var M = sr.Next<int>(H, W);

            var dx = new[]{0, 1, 0, -1};
            var dy = new[]{1, 0, -1, 0};
            Func<int, int, bool> isInside = (x, y) => 0 <= x && x < W && 0 <= y && y < H;
            Func<int, int, int, int, int, bool> dfs = null;
            dfs = (v, x, y, bx, by) =>{
                if(M[y][x] == -v) return true;
                if(M[y][x] != v) return false;
                M[y][x] = -v;

                var result = false;
                for(var i = 0; i < 4; i++){
                    var nx = x + dx[i];
                    var ny = y + dy[i];
                    if(nx == bx && ny == by || !isInside(nx, ny)) continue;
                    result |= dfs(v, nx, ny, x, y);
                }
                return result;
            };

            for(var i = 0; i < H; i++){
                for(var j = 0; j < W; j++){
                    if(M[i][j] > 0 && dfs(M[i][j], j, i, j, i)){
                        Console.WriteLine("possible");
                        return;
                    }
                }
            }

            Console.WriteLine("impossible");
            //---------------------------------
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = Console.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var result = new T[x];
            for(var i = 0; i < x; ++i) result[i] = Next<T>();
            return result;
        }

        public T[][] Next<T>(int y, int x){
            var result = new T[y][];
            for(var i = 0; i < y; ++i) result[i] = Next<T>(x);
            return result;
        }
    }
}
0