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' };
const int UB = 7;
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 EachPiece in PieceNameArr) {
HaitiKouhoListDict[EachPiece] = DeriveHaitiKouhoList(EachPiece);
}
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.BanArr[0, 0] = '*';
WillPush.CurrX = WillPush.CurrY = 0;
WillPush.PieceCnt = 0;
Stk.Push(WillPush);
var AnswerBanArrList = new List<char[,]>();
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
//クリア判定
if (Popped.PieceCnt == 9) {
AnswerBanArrList.Add(Popped.BanArr);
Console.WriteLine("解候補{0}を発見", AnswerBanArrList.Count);
continue;
}
//X座標の繰上げ処理
if (Popped.CurrX > UB) {
Popped.CurrX = 0;
Popped.CurrY++;
}
//最終行-1を超えた場合
if (Popped.CurrY > UB - 1) continue;
//使用済のピース名の配列
char[] UsedPieceArr = Popped.BanArr.Cast<char>().Distinct().ToArray();
foreach (char EachPiece in PieceNameArr) {
if (Array.IndexOf(UsedPieceArr, EachPiece) >= 0) continue;
//回転解の除外
if (EachPiece == '2') {
if (Popped.CurrX >= 1 && Popped.CurrY <= 0) continue;
if (Popped.CurrX >= 2 && Popped.CurrY <= 1) continue;
if (Popped.CurrX >= 3 && Popped.CurrY <= 2) continue;
if (Popped.CurrX >= 4 && Popped.CurrY <= 3) continue;
if (Popped.CurrX >= 5 && Popped.CurrY <= 4) continue;
if (Popped.CurrX >= 6 && Popped.CurrY <= 5) continue;
}
//ピースの配置候補リスト
List<bool[,]> HaitiKouhoList = new List<bool[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[EachPiece]);
//現在のマス目が空白の場合は、マス目を埋める必要あり
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[,] EachPieceMap 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 <= EachPieceMap.GetUpperBound(0); X++) {
for (int Y = 0; Y <= EachPieceMap.GetUpperBound(1); Y++) {
if (EachPieceMap[X, Y] == false) continue;
WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = EachPiece;
}
}
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("回転解を除外します");
RemoveKaiten(AnswerBanArrList);
Console.WriteLine();
for (int I = 0; I <= AnswerBanArrList.Count - 1; I++) {
Console.WriteLine("解{0}", I + 1);
PrintBan(AnswerBanArrList[I]);
}
Console.WriteLine("経過時間={0}", sw.Elapsed);
}
//回転解を除外
static void RemoveKaiten(List<char[,]> pTargetList)
{
Predicate<int> IsExist = (pCurrInd) =>
{
for (int I = 0; I <= pCurrInd - 1; I++) {
bool IsOK1 = false, IsOK2 = false, IsOK3 = false, IsOK4 = false;
bool IsOK5 = false, IsOK6 = false, IsOK7 = false; //回転1から7
for (int X = 0; X <= UB; X++) {
for (int Y = 0; Y <= UB; Y++) {
char CurrVal = pTargetList[pCurrInd][X, Y];
if (pTargetList[I][UB - X, Y] != CurrVal) IsOK1 = true;
if (pTargetList[I][UB - X, UB - Y] != CurrVal) IsOK2 = true;
if (pTargetList[I][X, UB - Y] != CurrVal) IsOK3 = true;
if (pTargetList[I][Y, X] != CurrVal) IsOK4 = true;
if (pTargetList[I][UB - Y, X] != CurrVal) IsOK5 = true;
if (pTargetList[I][UB - Y, UB - X] != CurrVal) IsOK6 = true;
if (pTargetList[I][Y, UB - X] != CurrVal) IsOK7 = true;
}
}
if (IsOK1 == false || IsOK2 == false || IsOK3 == false || IsOK4 == false
|| IsOK5 == false || IsOK6 == false || IsOK7 == false)
return true;
}
return false;
};
for (int I = pTargetList.Count - 1; I >= 0; I--) {
if (IsExist(I)) pTargetList.RemoveAt(I);
}
}
//ピース名を引数として、回転させた配置のListを返す
static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
{
bool[,] wkArr = null;
// ■
//■■■
//■■
// ■
if (pPieceName == '1') {
wkArr = new bool[3, 4];
wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = true;
wkArr[1, 3] = true;
}
//■■■■
//■■■
if (pPieceName == '2') {
wkArr = new bool[4, 2];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
}
// ■■
// ■■
//■■■
if (pPieceName == '3') {
wkArr = new bool[3, 3];
wkArr[1, 0] = wkArr[2, 0] = true;
wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = true;
}
//■■■■
// ■■
// ■
if (pPieceName == '4') {
wkArr = new bool[4, 3];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = true;
wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[2, 2] = true;
}
//■ ■
//■■■
//■■
if (pPieceName == '5') {
wkArr = new bool[3, 3];
wkArr[0, 0] = wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = true;
}
// ■■
//■■■
// ■■
if (pPieceName == '6') {
wkArr = new bool[3, 3];
wkArr[1, 0] = wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[1, 2] = wkArr[2, 2] = true;
}
// ■
//■■■
// ■■■
if (pPieceName == '7') {
wkArr = new bool[4, 3];
wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = true;
}
//■■
// ■■
// ■■■
if (pPieceName == '8') {
wkArr = new bool[4, 3];
wkArr[0, 0] = wkArr[1, 0] = true;
wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[1, 2] = wkArr[2, 2] = wkArr[3, 2] = true;
}
//■■
//■■
// ■
//■■
if (pPieceName == '9') {
wkArr = new bool[2, 4];
wkArr[0, 0] = wkArr[1, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = true;
wkArr[1, 2] = true;
wkArr[0, 3] = wkArr[1, 3] = true;
}
return DeriveKaitenArrList(wkArr);
}
//配列を引数として、回転させた配列のリストをDistinctして返す
static List<bool[,]> DeriveKaitenArrList(bool[,] pBaseArr)
{
var KaitenArrList = new List<bool[,]>();
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)
{
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 PrintBan(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());
}
}