using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
const int UB = 7 - 1;
//ピースごとの配置候補
static Dictionary<char, List<bool[,]>> HaitiKouhoListDict =
new Dictionary<char, List<bool[,]>>();
static char[] PieceNameArr = { 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L'};
struct JyoutaiDef
{
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
internal int PieceCnt;
}
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
foreach (char AnyPiece in PieceNameArr) {
HaitiKouhoListDict[AnyPiece] = DeriveHaitiKouhoList(AnyPiece);
}
//回転解を除外(ピースKは回転させない)
HaitiKouhoListDict['K'].RemoveRange(1, HaitiKouhoListDict['K'].Count - 1);
var stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.BanArr = new char[UB + 1, UB + 1];
for (int X = 0; X <= UB; X++)
for (int Y = 0; Y <= UB; Y++)
WillPush.BanArr[X, Y] = '*';
WillPush.CurrX = WillPush.CurrY = 0;
WillPush.PieceCnt = 0;
stk.Push(WillPush);
int AnwserCnt = 0;
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
//クリア判定
if (Popped.PieceCnt == 12) {
Console.WriteLine("解{0}を発見。経過時間={1}", ++AnwserCnt, sw.Elapsed);
PrintAnswer(Popped.BanArr);
continue;
}
//X座標の繰上げ処理
if (Popped.CurrX > UB) {
Popped.CurrX = 0;
Popped.CurrY++;
}
//最終行を超えた場合
if (Popped.CurrY > UB) continue;
//使用済のピース名の列挙インターフェース
IEnumerable<char> UsedPieceEnum = Popped.BanArr.Cast<char>().Where(X => X != '*').Distinct();
char[] UsedPieceArr = UsedPieceEnum.ToArray();
foreach (char AnyPiece in PieceNameArr) {
if (Array.IndexOf(UsedPieceArr, AnyPiece) >= 0) continue;
//BからJでは、最小の文字を優先して使う
bool IsMinChar = true;
if ('B' <= AnyPiece && AnyPiece <= 'J') {
for (char wkChar = 'A'; wkChar < AnyPiece; wkChar++) {
if (UsedPieceEnum.Contains(wkChar) == false) {
IsMinChar = false;
break;
}
}
}
if (IsMinChar == false) continue;
//ピースの配置候補リスト
List<bool[,]> HaitiKouhoList = new List<bool[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[AnyPiece]);
//現在のマス目が空白の場合は、マス目を埋める必要あり
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] == '*') {
HaitiKouhoList.RemoveAll(X => X[0, 0] == false);
}
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArr) == false);
//ピースを配置する経路のPush処理
foreach (bool[,] AnyPieceMap in HaitiKouhoList) {
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
WillPush.PieceCnt = Popped.PieceCnt + 1;
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.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = AnyPiece;
}
}
stk.Push(WillPush);
}
}
//現在のマス目が空白でない場合は、ピースを配置しない経路のPush
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] != '*') {
WillPush.BanArr = Popped.BanArr;
WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
WillPush.PieceCnt = Popped.PieceCnt;
stk.Push(WillPush);
}
}
Console.WriteLine("解は{0}通りです。経過時間={1}", AnwserCnt, sw.Elapsed);
}
//ピース名を引数として、回転させた配置のListを返す
static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
{
bool[,] wkArr = null;
//■
//■
//■■
if ('A' <= pPieceName && pPieceName <= 'J') {
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 == 'K') {
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 == 'L') {
wkArr = new bool[2, 3];
wkArr[0, 0] = false; wkArr[1, 0] = true;
wkArr[0, 1] = false; wkArr[1, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = 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度回転
// ■
//■■■
int BaseArrUB_X = pBaseArr.GetUpperBound(0);
int BaseArrUB_Y = pBaseArr.GetUpperBound(1);
for (int I = 1; I <= 4; I++) KaitenArrList.Add(null);
KaitenArrList[0] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];
KaitenArrList[2] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];
KaitenArrList[1] = new bool[BaseArrUB_Y + 1, BaseArrUB_X + 1];
KaitenArrList[3] = 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;
}
}
return KaitenArrList;
}
//マス目にピースを埋めれるか
static bool CanFillPiece(bool[,] pPieceMap, int pTargetX, int pTargetY, char[,] pBanArr)
{
if (pTargetX + pPieceMap.GetUpperBound(0) > UB) return false;
if (pTargetY + pPieceMap.GetUpperBound(1) > UB) return false;
for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + Y] != '*')
return false;
}
}
return true;
}
//解を出力
static void PrintAnswer(char[,] pBanArr)
{
var sb = new System.Text.StringBuilder();
for (int Y = 0; Y <= UB; Y++) {
for (int X = 0; X <= UB; X++) {
sb.Append(pBanArr[X, Y]);
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
}