using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
//ピースごとの配置候補
static Dictionary<int, List<char[,]>> mHaitiKouhoListDict =
new Dictionary<int, List<char[,]>>();
static int[] mPieceArr = Enumerable.Range(1, 8).ToArray();
const int UB = 7;
struct JyoutaiDef
{
internal int[,] BanArrPiece;
internal char[,] BanArrTe;
internal int CurrX;
internal int CurrY;
internal HashSet<int> UsedPieceSet;
}
static void Main()
{
foreach (int EachPiece in mPieceArr) {
mHaitiKouhoListDict[EachPiece] = DeriveHaitiKouhoList(EachPiece);
}
//回転解の排除で、ピース1は回転させない
mHaitiKouhoListDict[1].RemoveRange(1, mHaitiKouhoListDict[1].Count - 1);
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.BanArrPiece = new int[UB + 1, UB + 1];
WillPush.BanArrTe = new char[UB + 1, UB + 1];
for (int X = 0; X <= UB; X++)
for (int Y = 0; Y <= UB; Y++)
WillPush.BanArrTe[X, Y] = '□';
WillPush.CurrX = WillPush.CurrY = 0;
WillPush.UsedPieceSet = new HashSet<int>();
Stk.Push(WillPush);
int AnswerCnt = 0;
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
//クリア判定
if (Popped.UsedPieceSet.Count == 8) {
Console.WriteLine("解{0}を発見", ++AnswerCnt);
PrintBan(Popped.BanArrPiece, Popped.BanArrTe);
continue;
}
//X座標の繰上げ処理
if (Popped.CurrX > UB) {
Popped.CurrX = 0;
Popped.CurrY += 2;
}
//最終行を超えた場合
if (Popped.CurrY > UB) continue;
//現在のマス目が空白でない場合は、ピースを配置しない経路のPush
if (Popped.BanArrTe[Popped.CurrX, Popped.CurrY] != '□') {
WillPush.BanArrPiece = Popped.BanArrPiece;
WillPush.BanArrTe = Popped.BanArrTe;
WillPush.CurrX = Popped.CurrX + 2;
WillPush.CurrY = Popped.CurrY;
WillPush.UsedPieceSet = Popped.UsedPieceSet;
Stk.Push(WillPush);
continue;
}
//現在のマス目が空白の場合は、ピースを配置する経路のPush
foreach (int EachPiece in mPieceArr) {
if (Popped.UsedPieceSet.Contains(EachPiece)) continue;
//ピースの配置候補リスト
List<char[,]> HaitiKouhoList = new List<char[,]>();
HaitiKouhoList.AddRange(mHaitiKouhoListDict[EachPiece]);
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArrTe) == false);
foreach (char[,] EachPieceMap in HaitiKouhoList) {
WillPush.BanArrPiece = (int[,])Popped.BanArrPiece.Clone();
WillPush.BanArrTe = (char[,])Popped.BanArrTe.Clone();
WillPush.CurrX = Popped.CurrX + 2;
WillPush.CurrY = Popped.CurrY;
WillPush.UsedPieceSet = new HashSet<int>(Popped.UsedPieceSet);
WillPush.UsedPieceSet.Add(EachPiece);
for (int X = 0; X <= EachPieceMap.GetUpperBound(0); X++) {
for (int Y = 0; Y <= EachPieceMap.GetUpperBound(1); Y++) {
WillPush.BanArrPiece[Popped.CurrX + X, Popped.CurrY + Y] = EachPiece;
WillPush.BanArrTe[Popped.CurrX + X, Popped.CurrY + Y] = EachPieceMap[X, Y];
}
}
if (IsValid(WillPush.BanArrTe, EachPieceMap, Popped.CurrX, Popped.CurrY))
Stk.Push(WillPush);
}
}
}
}
//ピース名を引数として、回転させた配置のListを返す
static List<char[,]> DeriveHaitiKouhoList(int pPiece)
{
char[,] wkArr = new char[2, 4];
Action<string> SetAct = (pStr) =>
{
wkArr[0, 0] = pStr[0];
wkArr[1, 0] = pStr[1];
wkArr[0, 1] = wkArr[0, 2] = pStr[2];
wkArr[1, 1] = wkArr[1, 2] = pStr[3];
wkArr[0, 3] = pStr[4];
wkArr[1, 3] = pStr[5];
};
if (pPiece == 1) SetAct("チググパパチ");
if (pPiece == 2) SetAct("パグパグチチ");
if (pPiece == 3) SetAct("チパグパグチ");
if (pPiece == 4) SetAct("チグパチグパ");
if (pPiece == 5) SetAct("チグチパパグ");
if (pPiece == 6) SetAct("グチパグパチ");
if (pPiece == 7) SetAct("チチグパパグ");
if (pPiece == 8) SetAct("チグチパグパ");
return DeriveKaitenArrList(wkArr);
}
//配列を引数として、回転させた配列のリストをDistinctして返す
static List<char[,]> DeriveKaitenArrList(char[,] pBaseArr)
{
var KaitenArrList = new List<char[,]>();
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 <= 2; P += 2) KaitenArrList[P] = new char[BaseArrUB_X + 1, BaseArrUB_Y + 1];
for (int P = 1; P <= 3; P += 2) KaitenArrList[P] = new char[BaseArrUB_Y + 1, BaseArrUB_X + 1];
for (int X = 0; X <= BaseArrUB_X; X++) {
for (int Y = 0; Y <= BaseArrUB_Y; Y++) {
char 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<char> wkEnum1 = KaitenArrList[I].Cast<char>();
IEnumerable<char> wkEnum2 = KaitenArrList[J].Cast<char>();
if (wkEnum1.SequenceEqual(wkEnum2) == false) continue;
KaitenArrList.RemoveAt(I);
break;
}
}
return KaitenArrList;
}
//マス目にピースを埋めれるか
static bool CanFillPiece(char[,] pPieceMap, int pTargetX, int pTargetY, char[,] pBanArrTe)
{
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 (pBanArrTe[pTargetX + X, pTargetY + Y] != '□')
return false;
}
}
return true;
}
//盤面とレベルを引数として、有効な盤面かをチェック
static bool IsValid(char[,] pBanArrTe, char[,] pPieceMap, int pBaseX, int pBaseY)
{
var TeSet = new HashSet<char>();
Action<int, int> AddTe = (pTargetX, pTargetY) =>
{
if (pTargetX < 0 || UB < pTargetX) return;
if (pTargetY < 0 || UB < pTargetY) return;
TeSet.Add(pBanArrTe[pTargetX, pTargetY]);
};
Func<bool> HasSyouhai = () =>
{
//未確定マスがあれば勝敗は決まらない
if (TeSet.Contains('□')) return false;
//手が2通りしかなかったら、勝敗あり
return TeSet.Count == 2;
};
if (pPieceMap.GetLength(0) == 2) { //ピースが縦置きの場合
TeSet.Clear();
AddTe(pBaseX, pBaseY);
AddTe(pBaseX - 1, pBaseY - 1);
AddTe(pBaseX - 1, pBaseY);
AddTe(pBaseX, pBaseY - 1);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 1, pBaseY);
AddTe(pBaseX + 1, pBaseY - 1);
AddTe(pBaseX + 2, pBaseY - 1);
AddTe(pBaseX + 2, pBaseY);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX, pBaseY + 1);
AddTe(pBaseX - 1, pBaseY + 1);
AddTe(pBaseX - 1, pBaseY + 2);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 1, pBaseY + 1);
AddTe(pBaseX + 2, pBaseY + 1);
AddTe(pBaseX + 2, pBaseY + 2);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX, pBaseY + 3);
AddTe(pBaseX - 1, pBaseY + 3);
AddTe(pBaseX - 1, pBaseY + 4);
AddTe(pBaseX, pBaseY + 4);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 1, pBaseY + 3);
AddTe(pBaseX + 2, pBaseY + 3);
AddTe(pBaseX + 1, pBaseY + 4);
AddTe(pBaseX + 2, pBaseY + 4);
if (HasSyouhai()) return false;
}
else { //ピースが横置きの場合
TeSet.Clear();
AddTe(pBaseX, pBaseY);
AddTe(pBaseX - 1, pBaseY - 1);
AddTe(pBaseX - 1, pBaseY);
AddTe(pBaseX, pBaseY - 1);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 1, pBaseY);
AddTe(pBaseX + 1, pBaseY - 1);
AddTe(pBaseX + 2, pBaseY - 1);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 3, pBaseY);
AddTe(pBaseX + 3, pBaseY - 1);
AddTe(pBaseX + 4, pBaseY);
AddTe(pBaseX + 4, pBaseY - 1);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX, pBaseY + 1);
AddTe(pBaseX - 1, pBaseY + 1);
AddTe(pBaseX - 1, pBaseY + 2);
AddTe(pBaseX, pBaseY + 2);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 1, pBaseY + 1);
AddTe(pBaseX + 1, pBaseY + 2);
AddTe(pBaseX + 2, pBaseY + 2);
if (HasSyouhai()) return false;
TeSet.Clear();
AddTe(pBaseX + 3, pBaseY + 1);
AddTe(pBaseX + 3, pBaseY + 2);
AddTe(pBaseX + 4, pBaseY + 1);
AddTe(pBaseX + 4, pBaseY + 2);
if (HasSyouhai()) return false;
}
return true;
}
//盤面を出力
static void PrintBan(int[,] pBanArrPiece, char[,] pBanArrTe)
{
var sb = new System.Text.StringBuilder();
sb.AppendLine("ピースの配置");
for (int Y = 0; Y <= UB; Y++) {
for (int X = 0; X <= UB; X++) {
sb.Append(pBanArrPiece[X, Y]);
}
sb.AppendLine();
}
sb.AppendLine("手の配置");
for (int Y = 0; Y <= UB; Y++) {
for (int X = 0; X <= UB; X++) {
sb.Append(pBanArrTe[X, Y]);
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
}