using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
//ピースごとの配置候補
static Dictionary<char, List<bool[,]>> HaitiKouhoListDict =
new Dictionary<char, List<bool[,]>>();
static char[] PieceNameArr = { '1', '2', '3', '4',
'5', '6', '7', '8', '9', 'A'};
struct JyoutaiDef
{
internal List<char[,]> BanArrList;
internal int CurrBanInd;
internal int CurrX;
internal int CurrY;
}
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
//2次元配列の各要素に、半角空白をセット
Action<char[,]> ActArrFill_1 = (pTargetArr) =>
{
for (int X = 0; X <= pTargetArr.GetUpperBound(0); X++)
for (int Y = 0; Y <= pTargetArr.GetUpperBound(1); Y++)
pTargetArr[X, Y] = ' ';
};
char[,] Q1Arr1 = new char[3, 6]; ActArrFill_1(Q1Arr1);
char[,] Q1Arr2 = new char[7, 6]; ActArrFill_1(Q1Arr2);
char[,] Q2Arr1 = new char[4, 6]; ActArrFill_1(Q2Arr1);
char[,] Q2Arr2 = new char[6, 6]; ActArrFill_1(Q2Arr2);
char[,] Q3Arr1 = new char[5, 6]; ActArrFill_1(Q3Arr1);
char[,] Q3Arr2 = new char[5, 6]; ActArrFill_1(Q3Arr2);
char[,] Q4Arr1 = new char[8, 6]; ActArrFill_1(Q4Arr1);
char[,] Q4Arr2 = new char[4, 3]; ActArrFill_1(Q4Arr2);
char[,] Q5Arr1 = new char[3, 5]; ActArrFill_1(Q5Arr1);
char[,] Q5Arr2 = new char[9, 5]; ActArrFill_1(Q5Arr2);
char[,] Q6Arr1 = new char[4, 5]; ActArrFill_1(Q6Arr1);
char[,] Q6Arr2 = new char[8, 5]; ActArrFill_1(Q6Arr2);
char[,] Q7Arr1 = new char[5, 5]; ActArrFill_1(Q7Arr1);
char[,] Q7Arr2 = new char[7, 5]; ActArrFill_1(Q7Arr2);
var QuestionArrList = new List<char[,]>() { Q1Arr1, Q1Arr2 };
bool ValidEdakiriQ4 = false; //Q4の枝切りが有効か?
foreach (char AnyPiece in PieceNameArr) {
HaitiKouhoListDict[AnyPiece] = DeriveHaitiKouhoList(AnyPiece);
}
//回転解の除外で、ピース1は回転させない
HaitiKouhoListDict['1'].RemoveRange(1, HaitiKouhoListDict['1'].Count - 1);
var stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.BanArrList = QuestionArrList;
WillPush.CurrBanInd = 0;
WillPush.CurrX = WillPush.CurrY = 0;
stk.Push(WillPush);
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
//クリア判定
if (Popped.BanArrList[Popped.CurrBanInd].Cast<char>().All(X => X != ' ')) {
if (++Popped.CurrBanInd > QuestionArrList.Count - 1) {
Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
PrintAnswer(Popped.BanArrList);
return;
}
Popped.CurrX = Popped.CurrY = 0;
}
//Y座標の繰上げ処理
if (Popped.CurrY > Popped.BanArrList[Popped.CurrBanInd].GetUpperBound(1)) {
Popped.CurrY = 0;
Popped.CurrX++;
}
//最終列を超えた場合
if (Popped.CurrX > Popped.BanArrList[Popped.CurrBanInd].GetUpperBound(0)) continue;
//使用済のピース名のListジェネリック
var UsedPieceList = new List<char>();
foreach (char[,] EachBanArr in Popped.BanArrList) {
UsedPieceList.AddRange(EachBanArr.Cast<char>().Distinct());
}
//Q4で、3*4の面積を作成可能なのは、
//3単位、4単位、5単位のピースの組み合わせのみ
if (ValidEdakiriQ4 && Popped.CurrBanInd == 0)
if (UsedPieceList.Contains('7') && UsedPieceList.Contains('8')) continue;
foreach (char AnyPiece in PieceNameArr) {
if (UsedPieceList.Contains(AnyPiece)) continue;
if (ValidEdakiriQ4 && Popped.CurrBanInd == 0)
if (AnyPiece == '9' || AnyPiece == 'A') continue;
//ピースの配置候補リスト
var HaitiKouhoList = new List<bool[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[AnyPiece]);
//現在のマス目が空白の場合は、マス目を埋める必要あり
if (Popped.BanArrList[Popped.CurrBanInd][Popped.CurrX, Popped.CurrY] == ' ') {
HaitiKouhoList.RemoveAll(X => X[0, 0] == false);
}
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY,
Popped.BanArrList[Popped.CurrBanInd]) == false);
//ピースを配置する経路のPush処理
foreach (bool[,] AnyPieceMap in HaitiKouhoList) {
WillPush.CurrBanInd = Popped.CurrBanInd;
WillPush.BanArrList = new List<char[,]>(Popped.BanArrList);
WillPush.BanArrList[WillPush.CurrBanInd] =
(char[,])Popped.BanArrList[Popped.CurrBanInd].Clone();
WillPush.CurrX = Popped.CurrX;
if (AnyPiece == '4')
WillPush.CurrY = Popped.CurrY + 1;
else WillPush.CurrY = Popped.CurrY;
for (int X = 0; X <= AnyPieceMap.GetUpperBound(0); X++) {
for (int Y = 0; Y <= AnyPieceMap.GetUpperBound(1); Y++) {
if (AnyPieceMap[X, Y] == false) continue;
WillPush.BanArrList[WillPush.CurrBanInd]
[Popped.CurrX + X, Popped.CurrY + Y] = AnyPiece;
}
}
stk.Push(WillPush);
}
}
//現在のマス目が空白でない場合は、ピースを配置しない経路のPush
if (Popped.BanArrList[Popped.CurrBanInd][Popped.CurrX, Popped.CurrY] != ' ') {
WillPush.CurrBanInd = Popped.CurrBanInd;
WillPush.BanArrList = Popped.BanArrList;
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY + 1;
stk.Push(WillPush);
}
}
}
//ピース名を引数として、回転させた配置のListを返す
static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
{
bool[,] wkArr = null;
//■
//■
//■
//■
//■■■■■
if (pPieceName == '1') {
wkArr = new bool[5, 5];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = wkArr[4, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 1] = wkArr[3, 1] = wkArr[4, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = wkArr[4, 2] = false;
wkArr[0, 3] = true; wkArr[1, 3] = wkArr[2, 3] = wkArr[3, 3] = wkArr[4, 3] = false;
wkArr[0, 4] = wkArr[1, 4] = wkArr[2, 4] = wkArr[3, 4] = wkArr[4, 4] = true;
}
//■
//■
//■
//■
//■■■■
if (pPieceName == '2') {
wkArr = new bool[4, 5];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 1] = wkArr[3, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = false;
wkArr[0, 3] = true; wkArr[1, 3] = wkArr[2, 3] = wkArr[3, 3] = false;
wkArr[0, 4] = wkArr[1, 4] = wkArr[2, 4] = wkArr[3, 4] = true;
}
//■
//■
//■
//■
//■■■
if (pPieceName == '3') {
wkArr = new bool[3, 5];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = wkArr[2, 2] = false;
wkArr[0, 3] = true; wkArr[1, 3] = wkArr[2, 3] = false;
wkArr[0, 4] = wkArr[1, 4] = wkArr[2, 4] = true;
}
//■
//■
//■
//■
//■■
if (pPieceName == '4') {
wkArr = new bool[2, 5];
wkArr[0, 0] = true; wkArr[1, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = false;
wkArr[0, 3] = true; wkArr[1, 3] = false;
wkArr[0, 4] = wkArr[1, 4] = true;
}
//■
//■
//■
//■■■■
if (pPieceName == '5') {
wkArr = new bool[4, 4];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 1] = wkArr[3, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = false;
wkArr[0, 3] = wkArr[1, 3] = wkArr[2, 3] = wkArr[3, 3] = true;
}
//■
//■
//■
//■■■
if (pPieceName == '6') {
wkArr = new bool[3, 4];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = wkArr[2, 2] = false;
wkArr[0, 3] = wkArr[1, 3] = wkArr[2, 3] = true;
}
//■
//■
//■
//■■
if (pPieceName == '7') {
wkArr = new bool[2, 4];
wkArr[0, 0] = true; wkArr[1, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = false;
wkArr[0, 3] = wkArr[1, 3] = true;
}
//■
//■
//■■■
if (pPieceName == '8') {
wkArr = new bool[3, 3];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 0] = false;
wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = true;
}
//■
//■
//■■
if (pPieceName == '9') {
wkArr = new bool[2, 3];
wkArr[0, 0] = true; wkArr[1, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = false;
wkArr[0, 2] = wkArr[1, 2] = true;
}
//■
//■■
if (pPieceName == 'A') {
wkArr = new bool[2, 2];
wkArr[0, 0] = true; wkArr[1, 0] = false;
wkArr[0, 1] = wkArr[1, 1] = true;
}
return DeriveKaitenArrList(wkArr);
}
//配列を引数として、回転させた配列のリストをDistinctして返す
static List<bool[,]> DeriveKaitenArrList(bool[,] pBaseArr)
{
var KaitenArrList = new List<bool[,]>();
//1個目はそのまま
//■
//■■■
//2個目は1個目を時計回りに90度回転
//■■
//■
//■
//3個目は2個目を時計回りに90度回転
//■■■
// ■
//4個目は3個目を時計回りに90度回転
// ■
// ■
//■■
//5個目は1個目とX軸で線対称
//■■■
//■
//6個目は5個目を時計回りに90度回転
//■■
// ■
// ■
//7個目は6個目を時計回りに90度回転
// ■
//■■■
//8個目は7個目を時計回りに90度回転
//■
//■
//■■
int BaseArrUB_X = pBaseArr.GetUpperBound(0);
int BaseArrUB_Y = pBaseArr.GetUpperBound(1);
for (int I = 1; I <= 8; I++) KaitenArrList.Add(null);
for (int P = 0; P <= 6; P += 2) KaitenArrList[P] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];
for (int P = 1; P <= 7; P += 2) KaitenArrList[P] = new bool[BaseArrUB_Y + 1, BaseArrUB_X + 1];
for (int X = 0; X <= BaseArrUB_X; X++) {
for (int Y = 0; Y <= BaseArrUB_Y; Y++) {
bool SetVal = pBaseArr[X, Y];
KaitenArrList[0][X, Y] = SetVal;
KaitenArrList[1][Y, BaseArrUB_X - X] = SetVal;
KaitenArrList[2][BaseArrUB_X - X, BaseArrUB_Y - Y] = SetVal;
KaitenArrList[3][BaseArrUB_Y - Y, X] = SetVal;
KaitenArrList[4][X, BaseArrUB_Y - Y] = SetVal;
KaitenArrList[5][BaseArrUB_Y - Y, BaseArrUB_X - X] = SetVal;
KaitenArrList[6][BaseArrUB_X - X, Y] = SetVal;
KaitenArrList[7][Y, X] = SetVal;
}
}
//Distinctする
for (int I = KaitenArrList.Count - 1; 0 <= I; I--) {
for (int J = 0; J <= I - 1; J++) {
if (KaitenArrList[I].GetUpperBound(0) !=
KaitenArrList[J].GetUpperBound(0)) continue;
if (KaitenArrList[I].GetUpperBound(1) !=
KaitenArrList[J].GetUpperBound(1)) continue;
IEnumerable<bool> wkEnum1 = KaitenArrList[I].Cast<bool>();
IEnumerable<bool> wkEnum2 = KaitenArrList[J].Cast<bool>();
if (wkEnum1.SequenceEqual(wkEnum2) == false) continue;
KaitenArrList.RemoveAt(I);
break;
}
}
return KaitenArrList;
}
//マス目にピースを埋めれるか
static bool CanFillPiece(bool[,] pPieceMap, int pTargetX, int pTargetY, char[,] pBanArr)
{
for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
if (pTargetX + X > pBanArr.GetUpperBound(0)) return false;
for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
if (pTargetY + Y > pBanArr.GetUpperBound(1)) return false;
if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + Y] != ' ')
return false;
}
}
return true;
}
//解を出力
static void PrintAnswer(List<char[,]> pBanArrList)
{
var sb = new System.Text.StringBuilder();
for (int I = 0; I <= pBanArrList.Count - 1; I++) {
sb.AppendFormat("{0}個目の平面", I + 1);
sb.AppendLine();
for (int Y = 0; Y <= pBanArrList[I].GetUpperBound(1); Y++) {
for (int X = 0; X <= pBanArrList[I].GetUpperBound(0); X++) {
sb.Append(pBanArrList[I][X, Y]);
}
sb.AppendLine();
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
}