結果

問題 No.340 雪の足跡
ユーザー eitahoeitaho
提出日時 2016-03-23 14:58:49
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 869 ms / 1,000 ms
コード長 5,192 bytes
コンパイル時間 1,235 ms
コンパイル使用メモリ 117,632 KB
実行使用メモリ 66,540 KB
最終ジャッジ日時 2024-04-09 21:01:40
合計ジャッジ時間 13,057 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
25,428 KB
testcase_01 AC 33 ms
25,264 KB
testcase_02 AC 30 ms
25,344 KB
testcase_03 AC 32 ms
25,448 KB
testcase_04 AC 33 ms
25,516 KB
testcase_05 AC 30 ms
25,216 KB
testcase_06 AC 34 ms
25,572 KB
testcase_07 AC 33 ms
25,388 KB
testcase_08 AC 31 ms
25,652 KB
testcase_09 AC 31 ms
23,472 KB
testcase_10 AC 32 ms
25,812 KB
testcase_11 AC 40 ms
27,924 KB
testcase_12 AC 38 ms
25,904 KB
testcase_13 AC 40 ms
27,836 KB
testcase_14 AC 74 ms
30,268 KB
testcase_15 AC 84 ms
31,952 KB
testcase_16 AC 71 ms
31,972 KB
testcase_17 AC 97 ms
33,356 KB
testcase_18 AC 85 ms
30,404 KB
testcase_19 AC 98 ms
29,000 KB
testcase_20 AC 664 ms
61,416 KB
testcase_21 AC 654 ms
56,268 KB
testcase_22 AC 37 ms
30,628 KB
testcase_23 AC 703 ms
58,356 KB
testcase_24 AC 677 ms
61,260 KB
testcase_25 AC 626 ms
60,832 KB
testcase_26 AC 154 ms
42,812 KB
testcase_27 AC 59 ms
29,820 KB
testcase_28 AC 129 ms
39,664 KB
testcase_29 AC 736 ms
64,400 KB
testcase_30 AC 477 ms
53,144 KB
testcase_31 AC 739 ms
61,536 KB
testcase_32 AC 864 ms
66,540 KB
testcase_33 AC 634 ms
56,892 KB
testcase_34 AC 869 ms
63,932 KB
testcase_35 AC 700 ms
56,488 KB
testcase_36 AC 677 ms
56,648 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.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    static readonly int INF = (int)1e9;
    static readonly string Impossible = "Odekakedekinai..";
    static readonly int[] DR = { 0, 1, 0, -1 };
    static readonly int[] DC = { 1, 0, -1, 0 };

    public void Solve()
    {
        int W = Reader.Int(), H = Reader.Int(), N = Reader.Int();
        var table = new int[N * 2][];
        for (int i = 0; i < N * 2; i++)
            table[i] = Reader.IntLine();
        var row = Enu.Range(0, H).Select(i => new List<int>()).ToArray();
        var col = Enu.Range(0, W).Select(i => new List<int>()).ToArray();

        for (int ti = 1; ti < N * 2; ti += 2)
        {
            var t = table[ti];
            for (int i = 0; i + 1 < t.Length; i++)
            {
                int r = t[i] / W, c = t[i] % W;
                int nr = t[i + 1] / W, nc = t[i + 1] % W;
                if (r != nr)
                {
                    col[c].Add(Math.Min(r, nr) * 2 + 1);
                    col[c].Add(Math.Max(r, nr) * 2);
                }
                else
                {
                    row[r].Add(Math.Min(c, nc) * 2 + 1);
                    row[r].Add(Math.Max(c, nc) * 2);
                }
            }
        }
        bool[, ,] G = new bool[H, W, 4];
        for (int r = 0; r < H; r++)
        {
            int last = 0, balance = 0;
            row[r].Sort();
            foreach (var p in row[r])
            {
                int delta = p % 2 * 2 - 1;
                int pos = p / 2;
                if (balance > 0)
                    for (int c = last; c < pos; c++)
                        G[r, c, 0] = G[r, c + 1, 2] = true;
                balance += delta;
                last = pos;
            }
        }
        for (int c = 0; c < W; c++)
        {
            int last = 0, balance = 0;
            col[c].Sort();
            foreach (var p in col[c])
            {
                int delta = p % 2 * 2 - 1;
                int pos = p / 2;
                if (balance > 0)
                    for (int r = last; r < pos; r++)
                        G[r, c, 1] = G[r + 1, c, 3] = true;
                balance += delta;
                last = pos;
            }
        }

        int ans = BFS(G, H, W);
        Console.WriteLine(ans == INF ? Impossible : ans + "");
    }

    private int BFS(bool[, ,] G, int H, int W)
    {
        if (H == 1 && W == 1) return 0;
        var minCost = new int[H, W];
        for (int r = 0; r < H; r++)
            for (int c = 0; c < W; c++)
                minCost[r, c] = INF;
        minCost[0, 0] = 0;
        var que = new Queue<int>();
        que.Enqueue(0); que.Enqueue(0);

        while (que.Count > 0)
        {
            int r = que.Dequeue(), c = que.Dequeue();
            for (int d = 0; d < DR.Length; d++)
                if (G[r, c, d])
                {
                    int nr = r + DR[d], nc = c + DC[d];
                    int nextCost = minCost[r, c] + 1;
                    if (nextCost < minCost[nr, nc])
                    {
                        if (nr == H - 1 && nc == W - 1) return nextCost;
                        minCost[nr, nc] = nextCost;
                        que.Enqueue(nr); que.Enqueue(nc);
                    }
                }
        }

        return INF;
    }
}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private 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 Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private 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