オフラインリアルタイムどう書く E05 C# で解く


問題はこちら

- http://qiita.com/Nabetani/items/c516875b13a4d282affe

素直にそれぞれの道を進んで最後まで到達できるかを調べています。


```csharp
using System;
using System.Collections.Generic;
using System.Linq;

// どきどきトロッコ 横へな2016.7.2 問題
// http://nabetani.sakura.ne.jp/hena/orde05dokitruck/

class Program {
    static int[] Next(int n) {
        // -1 => 分岐なし
        // -2 => x 印
        switch (n) {
        case 1: return new[] { 1, 2, -1 };
        case 2: return new[] { 2, -1, 1 };
        case 3: return new[] { 2, 0, -1 };
        case 4: return new[] { -1, 0, 1 };
        case 5: return new[] { -1, 2, 0 };
        case 6: return new[] { 1, -1, 0 };
        case 7: return new[] { -1, -2, -1 };
        case 8: return new[] { -2, -1, -1 };
        case 9: return new[] { -1, -1, -2 };
        }
        throw new ArgumentException();
    }

    static bool Calc_1(int x, int p, int[] xs) {
        if (p == xs.Length) return true;

        var next = Next(xs[p]);
        if (next[x] == -2) return false; // x 印

        bool goal = Calc_1(x, p + 1, xs); // 同じ道を進む
        if (!goal && next[x] >= 0) { // 分かれ道を進む
            goal = Calc_1(next[x], p + 1, xs);
        }
        return goal;
    }

    static string Calc(string input) {
        var xs = input.Select(c => c - '0').ToArray();
        string ans = "";
        for (int i = 0; i < 3; i++) {
            if (Calc_1(i, 0, xs)) {
                ans += (char)('a' + i);
            }
        }
        return ans.Length > 0 ? ans : "-";
    }

    static void test(string input, string output) {
        var got = Calc(input);
        if (got == output) {
            Console.WriteLine("ok");
        }
        else {
            Console.WriteLine("ng {0} {1} got:{2}", input, output, got);
            throw new Exception();
        }
    }

    static void Main() {
        /*0*/ test( "1728398", "bc" );
        /*1*/ test( "789", "-" );
        /*2*/ test( "274", "ac" );
        /*3*/ test( "185", "abc" );
        /*4*/ test( "396", "ab" );
        /*5*/ test( "1278", "abc" );
        /*6*/ test( "7659832", "a" );
        /*7*/ test( "178", "bc" );
        /*8*/ test( "189", "ab" );
        /*9*/ test( "197", "a" );
        /*10*/ test( "278", "ac" );
        /*11*/ test( "289", "bc" );
        /*12*/ test( "297", "a" );
        /*13*/ test( "378", "ac" );
        /*14*/ test( "389", "b" );
        /*15*/ test( "397", "ab" );
        /*16*/ test( "478", "c" );
        /*17*/ test( "489", "bc" );
        /*18*/ test( "497", "ab" );
        /*19*/ test( "578", "bc" );
        /*20*/ test( "589", "b" );
        /*21*/ test( "597", "ac" );
        /*22*/ test( "678", "c" );
        /*23*/ test( "689", "ab" );
        /*24*/ test( "697", "ac" );
        /*25*/ test( "899", "b" );
        /*26*/ test( "7172", "ac" );
        /*27*/ test( "54787", "bc" );
        /*28*/ test( "83713", "bc" );
        /*29*/ test( "149978", "-" );
        /*30*/ test( "159735", "abc" );
        /*31*/ test( "1449467", "abc" );
        /*32*/ test( "9862916", "b" );
        /*33*/ test( "96112873", "ab" );
        /*34*/ test( "311536789", "-" );
        /*35*/ test( "281787212994", "abc" );
        /*36*/ test( "697535114542", "ac" );
    }
}
```