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 = "2014".ToCharArray();
static char[,] mQuestionArr; //問題の初期盤面
const int UB_X = 10;
const int UB_Y = 7;
//許可する空白マスの数
const int AllowSpaceCnt = 10;
struct JyoutaiDef
{
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
internal Dictionary<char, int> UsePieceDict;
internal int PassCnt;
}
//問題を定義
static void QuestionDef()
{
mQuestionArr = new char[UB_X + 1, UB_Y + 1];
for (int X = 0; X <= UB_X; X++)
for (int Y = 0; Y <= UB_Y; Y++)
mQuestionArr[X, Y] = ' ';
}
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
var CanUsePieceDict = new Dictionary<char, int>() {{ '2', 2 },{ '0', 2 },
{ '1', 2 },{ '4', 2 }};
//問題を定義
QuestionDef();
foreach (char EachPiece in PieceNameArr) {
HaitiKouhoListDict[EachPiece] = DeriveHaitiKouhoList(EachPiece);
}
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.BanArr = mQuestionArr;
WillPush.CurrX = WillPush.CurrY = 0;
WillPush.UsePieceDict = new Dictionary<char, int>();
WillPush.PassCnt = 0;
Stk.Push(WillPush);
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
//クリア判定
if (Popped.UsePieceDict.Values.Sum() == 8) {
Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
PrintBan(Popped.BanArr);
return;
}
//Y座標の繰上げ処理
if (Popped.CurrY > UB_Y) {
Popped.CurrY = 0;
Popped.CurrX++;
}
//最終列を超えた場合
if (Popped.CurrX > UB_X) continue;
foreach (char EachPiece in PieceNameArr) {
if (Popped.UsePieceDict.ContainsKey(EachPiece)) {
//使用上限に到達している場合は、不可
if (Popped.UsePieceDict[EachPiece] >= CanUsePieceDict[EachPiece])
continue;
}
//ピースの配置候補リスト
List<bool[,]> HaitiKouhoList = new List<bool[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[EachPiece]);
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArr) == false);
WillPush.UsePieceDict = new Dictionary<char, int>(Popped.UsePieceDict);
if (WillPush.UsePieceDict.ContainsKey(EachPiece))
WillPush.UsePieceDict[EachPiece]++;
else WillPush.UsePieceDict[EachPiece] = 1;
//ピースを配置する経路のPush処理
foreach (bool[,] AnyPieceMap in HaitiKouhoList) {
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY;
WillPush.PassCnt = Popped.PassCnt;
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] = EachPiece;
}
}
Stk.Push(WillPush);
}
}
//ピースを配置しない経路のPush
WillPush.BanArr = Popped.BanArr;
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY + 1;
WillPush.UsePieceDict = Popped.UsePieceDict;
WillPush.PassCnt = Popped.PassCnt;
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] == ' ') {
if (++WillPush.PassCnt > AllowSpaceCnt) continue;
}
Stk.Push(WillPush);
}
}
//ピース名を引数として、回転させた配置のListを返す
static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
{
bool[,] wkArr = null;
//■■■
// ■
//■■■
//■
//■■■
if (pPieceName == '2') {
wkArr = new bool[3, 5];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = true;
wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = true;
wkArr[0, 3] = true;
wkArr[0, 4] = wkArr[1, 4] = wkArr[2, 4] = true;
}
//■■■
//■ ■
//■ ■
//■ ■
//■■■
if (pPieceName == '0') {
wkArr = new bool[3, 5];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[2, 2] = true;
wkArr[0, 3] = wkArr[2, 3] = true;
wkArr[0, 4] = wkArr[1, 4] = wkArr[2, 4] = true;
}
//■
//■
//■
//■
//■
if (pPieceName == '1') {
wkArr = new bool[1, 5];
wkArr[0, 0] = true;
wkArr[0, 1] = true;
wkArr[0, 2] = true;
wkArr[0, 3] = true;
wkArr[0, 4] = true;
}
//■ ■
//■ ■
//■■■
// ■
// ■
if (pPieceName == '4') {
wkArr = new bool[3, 5];
wkArr[0, 0] = wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = true;
wkArr[2, 3] = true;
wkArr[2, 4] = 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 <= 4; I++) KaitenArrList.Add(null);
for (int P = 0; P <= 3; P += 2) KaitenArrList[P] = new bool[BaseArrUB_X + 1, BaseArrUB_Y + 1];
for (int P = 1; P <= 3; 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;
}
}
//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_X) return false;
if (pTargetY + pPieceMap.GetUpperBound(1) > UB_Y) 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; Y++) {
for (int X = 0; X <= UB_X; X++) {
sb.Append(pBanArr[X, Y] == ' ' ? '/' : pBanArr[X, Y]);
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
}