結果

問題 No.460 裏表ちわーわ
ユーザー 14番14番
提出日時 2016-12-11 03:42:21
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,691 bytes
コンパイル時間 3,648 ms
コンパイル使用メモリ 110,624 KB
実行使用メモリ 189,232 KB
最終ジャッジ日時 2023-08-19 11:05:28
合計ジャッジ時間 8,941 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int[] inp = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        int h = inp[0];
        int w = inp[1];
        this.Map = new bool[h][];
        bool[][] goal = new bool[h][];

        for (int i = 0; i < h; i++)
        {
            Map[i] = Reader.ReadLine().Split(' ').Select(a => a[0] == '1').ToArray();
            goal[i] = new bool[w];
        }

        this.mapKey = GetKey(this.Map);

        Task goalTask = new Task(goal, GetKey(goal), 0);
        SetDic(goalTask);
        if (dic.ContainsKey(mapKey))
        {
            Console.WriteLine(dic[mapKey].Step);
        }
        else
        {
            Console.WriteLine("Impossible");
        }

    }

    private void SetDic(Task t)
    {
        if (t.Key == this.mapKey)
        {
            return;
        }
        if (t.Step > 16)
        {
            return;
        }
        

        for (int i = 0; i < t.Map.Length; i++)
        {
            for (int j = 0; j < t.Map[0].Length; j++)
            {
                bool[][] nextMap = this.Conv(t.Map, i, j);
                Task nextTask = new Task(nextMap, GetKey(nextMap), t.Step + 1);
                if (dic.ContainsKey(nextTask.Key) && dic[nextTask.Key].Step <= nextTask.Step)
                {
                    continue;
                }
                dic[nextTask.Key] = nextTask;
                SetDic(nextTask);
            }
        }
    }

    private Dictionary<ulong, Task> dic = new Dictionary<ulong, Task>();
    private ulong mapKey;

    public struct Task
    {
        public bool[][] Map;
        public ulong Key;
        public int Step;
        public Task(bool[][] mp, ulong key, int step)
        {
            this.Map = mp;
            this.Key = key;
            this.Step = step;
        }
    }

    private ulong GetKey(bool[][] src)
    {
        ulong ret = 0;
        for (int i = 0; i < src.Length; i++)
        {
            for (int j = 0; j < src[i].Length; j++)
            {
                if (src[i][j])
                {
                    int idx = (i * src[i].Length) + j;
                    ret += (ulong)(1<<idx);
                }
            }
        }
        return ret;
    }
    private bool[][] Conv(bool[][] src, int row, int col)
    {
        bool[][] ret = new bool[src.Length][];
        for (int i = 0; i < src.Length; i++)
        {
            ret[i] = new bool[src[i].Length];
            for (int j = 0; j < src[i].Length; j++)
            {
                if (i >= row - 1 && i <= row + 1 && j >= col - 1 && j <= col + 1)
                {
                    ret[i][j] = !src[i][j];
                }
                else
                {
                    ret[i][j] = src[i][j];
                }
            }
        }
        return ret;
    }

    private bool[][] Map;


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"

   
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0






";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0