結果

問題 No.585 工夫のないパズル
ユーザー くれちーくれちー
提出日時 2017-10-28 02:55:59
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 14,664 bytes
コンパイル時間 1,217 ms
コンパイル使用メモリ 125,448 KB
実行使用メモリ 27,364 KB
最終ジャッジ日時 2024-11-22 02:51:34
合計ジャッジ時間 2,497 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
25,224 KB
testcase_01 AC 39 ms
25,196 KB
testcase_02 AC 38 ms
25,396 KB
testcase_03 AC 39 ms
25,100 KB
testcase_04 AC 38 ms
25,080 KB
testcase_05 AC 37 ms
27,244 KB
testcase_06 AC 39 ms
25,316 KB
testcase_07 AC 39 ms
25,220 KB
testcase_08 AC 40 ms
27,364 KB
testcase_09 AC 40 ms
25,196 KB
testcase_10 AC 40 ms
25,196 KB
testcase_11 AC 39 ms
22,964 KB
testcase_12 AC 39 ms
26,984 KB
testcase_13 AC 39 ms
27,136 KB
testcase_14 AC 39 ms
25,092 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.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Math;
using static System.Linq.Enumerable;
using static Extentions;

public static class Program
{
    static int[][] _s;

    static Queue<(bool, int, int)> _ope = new Queue<(bool, int, int)>(100);

    public static void Solve(IO io)
    {
        _s = io.Read(4, t => t.Select(u => u - 'A').Take(4).ToArray());

        Print();
        MakeRow(0);
        MakeRow(1);
        MakeRow(2);
        MakeRow3();
        Print();
        Optimize();

        io.WriteLine(_ope.Count);

        foreach (var (d, i, n) in _ope)
        {
            io.WriteLine($"{(d ? 'R' : 'C')} {i} {n}");
        }
    }

    // for debug
    public static void Print()
    {
        for (var i = 0; i < 4; i++)
        {
            for (var j = 0; j < 4; j++) Console.Error.Write($"{_s[i][j]} ");
            Console.Error.WriteLine();
        }
    }

    public static int FindColumn(int v, int r)
    {
        for (var i = 0; i < 4; i++) if (_s[r][i] == v) return i;
        throw new UnreachableException();
    }

    public static (int, int) Find(int v)
    {
        for (var i = 0; i < 4; i++)
            for (var j = 0; j < 4; j++) if (_s[i][j] == v) return (i, j);
        throw new UnreachableException();
    }

    public static int Round(int i) => (i + 4) % 4;

    public static void Right(int r)
    {
        var t = _s[r][3];
        _s[r][3] = _s[r][2]; _s[r][2] = _s[r][1]; _s[r][1] = _s[r][0];
        _s[r][0] = t;
        _ope.Enqueue((true, r, 1));
    }

    public static void Right(int r, int n)
    {
        Repeat(() => Right(r), n);
        //_ope.Push((true, r, n));
    }

    public static void Left(int r)
    {
        var t = _s[r][0];
        _s[r][0] = _s[r][1];
        _s[r][1] = _s[r][2];
        _s[r][2] = _s[r][3];
        _s[r][3] = t;
        _ope.Enqueue((true, r, 3));
    }

    public static void Left(int r, int n)
    {
        Repeat(() => Left(r), n);
        //_ope.Push((true, r, Round(4 - n)));
    }

    public static void Down(int c)
    {
        var t = _s[3][c];
        _s[3][c] = _s[2][c]; _s[2][c] = _s[1][c]; _s[1][c] = _s[0][c];
        _s[0][c] = t;
        _ope.Enqueue((false, c, 1));
    }

    public static void Down(int c, int n)
    {
        Repeat(() => Down(c), n);
        //_ope.Push((false, c, n));
    }

    public static void Up(int c)
    {
        var t = _s[0][c];
        _s[0][c] = _s[1][c]; _s[1][c] = _s[2][c]; _s[2][c] = _s[3][c];
        _s[3][c] = t;
        _ope.Enqueue((false, c, 3));
    }

    public static void Up(int c, int n)
    {
        Repeat(() => Up(c), n);
        //_ope.Push((false, c, Round(4 - n)));
    }

    // _s[r][c] <-> _s[r][c + 1]
    public static void Swap(int r, int c)
    {
        c = Round(c - 1);
        Down(c);
        Left(r);
        Up(c);
        Right(r, 2);
        Down(c);
        Left(r);
        Up(c);
        Left(r);
    }

    // r != 3
    public static void MakeRow(int r)
    {
        var next = r * 4;

        for (var i = 0; i < 4; i++, next++)
        {
            var (rn, cn) = Find(next);
            if (rn == r && cn == i) continue;
 
            var upN = rn - r;
            var leftN = Round(cn - i);

            if (upN == 0)
            {
                Down(cn);
                Left(rn + 1, leftN == 1 ? 1 : leftN - 1);
                Up(cn);
                upN = 1;
                leftN = leftN == 1 ? 0 : 1;
                rn++;
            }

            if (leftN == 0)
            {
                Right(rn, 1);
                leftN++;
            }

            Down(i, upN);
            Left(rn, leftN);
            Up(i, upN);
        }
    }

    public static void MakeRow3()
    {
        for (var i = 0; i < 4; i++)
        {
            var c = FindColumn(12 + i, 3);
            while (c > i) { Swap(3, c - 1); c = FindColumn(12 + i, 3); }
        }
    }

    public static void Optimize()
    {
        var t = new Queue<(bool, int, int)>(_ope.Count);

        while (_ope.Any())
        {
            var (fd, fi, n) = _ope.Peek();
            var (d, i, cn) = (fd, fi, 0);

            while (d == fd && i == fi)
            {
                _ope.Dequeue();
                cn += n;
                if (!_ope.Any()) break;
                (d, i, n) = _ope.Peek();
            }

            cn = Round(cn);
            if (cn > 0) t.Enqueue((fd, fi, Round(cn)));
        }

        _ope = t;
    }

    public static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
        {
            NewLine = "\n",
#if DEBUG
            AutoFlush = true,
#else
            AutoFlush = false,
#endif
        });
        Solve(new IO(Console.In, Console.Out));
        Console.Out.Flush();
    }
}

public static partial class Extentions
{
}

#region Library

// fuckin' slow
public class IO
{
    public delegate T Parser<out T>(string str);
    public delegate string Serializer<in T>(T value);

    private TextReader _tr;
    private TextWriter _tw;

    public IO(TextReader tr, TextWriter tw)
    {
        _tr = tr;
        _tw = tw;
    }

    public string ReadLine() => _tr.ReadLine();

    private string ReadWord()
    {
        var sb = new StringBuilder();
        Repeat(_tr.Read).TakeWhile(c => c != ' ' && c != '\n').ForEach(c => sb.Append((char)c));
        return sb.ToString();
    }

    public T Read<T>(Parser<T> parser = null) => parser != null
        ? parser(ReadWord()) : (T)Convert.ChangeType(ReadWord(), typeof(T));

    public (T1, T2) Read<T1, T2>(
        Parser<T1> parser1 = null, Parser<T2> parser2 = null)
        => (Read(parser1), Read(parser2));

    public (T1, T2, T3) Read<T1, T2, T3>(
        Parser<T1> parser1 = null, Parser<T2> parser2 = null,
        Parser<T3> parser3 = null)
        => (Read(parser1), Read(parser2), Read(parser3));

    public (T1, T2, T3, T4) Read<T1, T2, T3, T4>(
        Parser<T1> parser1 = null, Parser<T2> parser2 = null,
        Parser<T3> parser3 = null, Parser<T4> parser4 = null)
        => (Read(parser1), Read(parser2), Read(parser3), Read(parser4));

    public T[] Read<T>(int count, Parser<T> parser = null)
        => Repeat(() => Read(parser), count).ToArray();

    public (T1[], T2[]) Read<T1, T2>(int count,
        Parser<T1> parser1 = null, Parser<T2> parser2 = null)
        => Repeat(() => Read(parser1, parser2), count).ToArray().Unzip();

    public (T1[], T2[], T3[]) Read<T1, T2, T3>(int count,
        Parser<T1> parser1 = null, Parser<T2> parser2 = null,
        Parser<T3> parser3 = null)
        => Repeat(() => Read(parser1, parser2, parser3), count).ToArray().Unzip();

    public (T1[], T2[], T3[], T4[]) Read<T1, T2, T3, T4>(int count,
        Parser<T1> parser1 = null, Parser<T2> parser2 = null,
        Parser<T3> parser3 = null, Parser<T4> parser4 = null)
        => Repeat(() => Read(parser1, parser2, parser3, parser4), count).ToArray().Unzip();

    public void Write<T>(T value, Serializer<T> serializer = null)
    {
        _tw.Write(serializer != null ? serializer(value) : value.ToString());
        _tw.Write(' ');
    }

    public void Write<T1, T2>(T1 value1, T2 value2,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null)
    {
        Write(value1, serializer1);
        Write(value2, serializer2);
    }

    public void Write<T1, T2, T3>(T1 value1, T2 value2, T3 value3,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null,
        Serializer<T3> serializer3 = null)
    {
        Write(value1, serializer1);
        Write(value2, serializer2);
        Write(value3, serializer3);
    }

    public void Write<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null,
        Serializer<T3> serializer3 = null, Serializer<T4> serializer4 = null)
    {
        Write(value1, serializer1);
        Write(value2, serializer2);
        Write(value3, serializer3);
        Write(value4, serializer4);
    }

    public void WriteLine<T>(T value, Serializer<T> serializer = null)
    {
        Write(value, serializer);
        _tw.WriteLine();
    }

    public void WriteLine<T1, T2>(T1 value1, T2 value2,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null)
    {
        Write(value1, value2, serializer1, serializer2);
        _tw.WriteLine();
    }

    public void WriteLine<T1, T2, T3>(T1 value1, T2 value2, T3 value3,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null,
        Serializer<T3> serializer3 = null)
    {
        Write(value1, value2, value3, serializer1, serializer2, serializer3);
        _tw.WriteLine();
    }

    public void WriteLine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null,
        Serializer<T3> serializer3 = null, Serializer<T4> serializer4 = null)
    {
        Write(value1, value2, value3, value4, serializer1, serializer2, serializer3, serializer4);
        _tw.WriteLine();
    }

    public void WriteLine<T>(IEnumerable<T> values, Serializer<T> serializer = null)
        => values.ForEach(v => WriteLine(v, serializer));

    public void WriteLine<T1, T2>(
        IEnumerable<T1> values1, IEnumerable<T2> values2,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null)
    {
        foreach (var (v1, v2) in values1.Zip(values2, ValueTuple.Create))
            WriteLine(v1, v2, serializer1, serializer2);
    }

    public void WriteLine<T1, T2, T3>(
        IEnumerable<T1> values1, IEnumerable<T2> values2,
        IEnumerable<T3> values3,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null,
        Serializer<T3> serializer3 = null)
    {
        foreach (var (v1, v2, v3) in values1.Zip(values2, values3, ValueTuple.Create))
            WriteLine(v1, v2, v3, serializer1, serializer2, serializer3);
    }

    public void WriteLine<T1, T2, T3, T4>(
        IEnumerable<T1> values1, IEnumerable<T2> values2,
        IEnumerable<T3> values3, IEnumerable<T4> values4,
        Serializer<T1> serializer1 = null, Serializer<T2> serializer2 = null,
        Serializer<T3> serializer3 = null, Serializer<T4> serializer4 = null)
    {
        foreach (var (v1, v2, v3, v4) in values1.Zip(values2, values3, values4, ValueTuple.Create))
            WriteLine(v1, v2, v3, v4, serializer1, serializer2, serializer3, serializer4);
    }
}

public class UnreachableException : Exception
{
}

public static partial class Extentions
{
    public static void Assert(bool condition)
    {
        if (!condition) Environment.Exit(1);
    }

    public static string AsString(this IEnumerable<char> source) => new string(source.ToArray());

    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var item in source) action(item);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, _> func)
    {
        foreach (var item in source) func(item);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
    {
        var i = 0;
        foreach (var item in source) action(item, i++);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, int, _> func)
    {
        var i = 0;
        foreach (var item in source) func(item, i++);
    }

    public static void Repeat(Action action, int count)
    {
        for (var i = 0; i < count; i++) action();
    }

    public static void Repeat(Action<int> action, int count)
    {
        for (var i = 0; i < count; i++) action(i);
    }

    public static IEnumerable<T> Repeat<T>(Func<T> func)
    {
        for (var i = 0; ; i++) yield return func();
    }

    public static IEnumerable<T> Repeat<T>(Func<T> func, int count)
    {
        for (var i = 0; i < count; i++) yield return func();
    }

    public static IEnumerable<T> Repeat<T>(Func<int, T> func)
    {
        for (var i = 0; ; i++) yield return func(i);
    }

    public static IEnumerable<T> Repeat<T>(Func<int, T> func, int count)
    {
        for (var i = 0; i < count; i++) yield return func(i);
    }

    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x; x = y; y = tmp;
    }

    public static (T1[], T2[]) Unzip<T1, T2>(
        this ICollection<(T1, T2)> source)
    {
        var ts1 = new T1[source.Count];
        var ts2 = new T2[source.Count];
        var i = 0;
        foreach (var (t1, t2) in source) { ts1[i] = t1; ts2[i] = t2; i++; }
        return (ts1, ts2);
    }

    public static (T1[], T2[], T3[]) Unzip<T1, T2, T3>(
        this ICollection<(T1, T2, T3)> source)
    {
        var ts1 = new T1[source.Count];
        var ts2 = new T2[source.Count];
        var ts3 = new T3[source.Count];
        var i = 0;
        foreach (var (t1, t2, t3) in source) { ts1[i] = t1; ts2[i] = t2; ts3[i] = t3; i++; }
        return (ts1, ts2, ts3);
    }

    public static (T1[], T2[], T3[], T4[]) Unzip<T1, T2, T3, T4>(
        this ICollection<(T1, T2, T3, T4)> source)
    {
        var ts1 = new T1[source.Count];
        var ts2 = new T2[source.Count];
        var ts3 = new T3[source.Count];
        var ts4 = new T4[source.Count];
        var i = 0;
        foreach (var (t1, t2, t3, t4) in source) { ts1[i] = t1; ts2[i] = t2; ts3[i] = t3; ts4[i] = t4; i++; }
        return (ts1, ts2, ts3, ts4);
    }

    public static IEnumerable<T> Zip<T1, T2, T3, T>(
        this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> thrid,
        Func<T1, T2, T3, T> resultSelector)
    {
        using (var e1 = first.GetEnumerator())
        using (var e2 = second.GetEnumerator())
        using (var e3 = thrid.GetEnumerator())
        {
            while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext())
                yield return resultSelector(e1.Current, e2.Current, e3.Current);
        }
    }

    public static IEnumerable<T> Zip<T1, T2, T3, T4, T>(
        this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> thrid, IEnumerable<T4> fourth,
        Func<T1, T2, T3, T4, T> resultSelector)
    {
        using (var e1 = first.GetEnumerator())
        using (var e2 = second.GetEnumerator())
        using (var e3 = thrid.GetEnumerator())
        using (var e4 = fourth.GetEnumerator())
        {
            while (e1.MoveNext() && e2.MoveNext() && e3.MoveNext() && e4.MoveNext())
                yield return resultSelector(e1.Current, e2.Current, e3.Current, e4.Current);
        }
    }
}

#endregion
0