using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
const int UB = 6 - 1;
//ピースごとの配置候補
static Dictionary<char, List<char[,]>> HaitiKouhoListDict =
new Dictionary<char, List<char[,]>>();
static char[] PieceNameArr = "FIPUloabd".ToCharArray();
struct JyoutaiDef
{
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
internal int PieceCnt;
}
static void Main()
{
foreach (char EachPiece in PieceNameArr) {
HaitiKouhoListDict[EachPiece] = DeriveHaitiKouhoList(EachPiece);
}
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.BanArr = new char[UB + 1, UB + 1];
FillSpace(WillPush.BanArr);
WillPush.CurrX = WillPush.CurrY = 0;
WillPush.PieceCnt = 0;
Stk.Push(WillPush);
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
//クリア判定
if (Popped.PieceCnt == 9) {
Console.WriteLine("解を発見");
PrintBan(Popped.BanArr);
return;
}
//X座標の繰上げ処理
if (Popped.CurrX > UB) {
Popped.CurrX = 0;
Popped.CurrY++;
}
//最終行を超えた場合
if (Popped.CurrY > UB) continue;
//使用済のピース名の配列
char[] UsedPieceArr = Popped.BanArr.Cast<char>().Distinct().ToArray();
foreach (char EachPiece in PieceNameArr) {
if (UsedPieceArr.Contains(EachPiece)) continue;
//ピースの配置候補リスト
List<char[,]> HaitiKouhoList = new List<char[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[EachPiece]);
//現在のマス目が空白の場合は、マス目を埋める必要あり
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] == ' ') {
HaitiKouhoList.RemoveAll(X => X[0, 0] == ' ');
}
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArr) == false);
//ピースを配置する経路のPush処理
foreach (char[,] EachPieceArr in HaitiKouhoList) {
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
if (EachPiece == 'F' || EachPiece == 'l' || EachPiece == 'd')
WillPush.CurrX = Popped.CurrX;
else WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
WillPush.PieceCnt = Popped.PieceCnt + 1;
for (int X = 0; X <= EachPieceArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= EachPieceArr.GetUpperBound(1); Y++) {
if (EachPieceArr[X, Y] == ' ') 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);
}
}
}
static void FillSpace(char[,] pArr)
{
for (int X = 0; X <= pArr.GetUpperBound(0); X++)
for (int Y = 0; Y <= pArr.GetUpperBound(1); Y++)
pArr[X, Y] = ' ';
}
//ピース名を引数として、回転させた配置のListを返す
static List<char[,]> DeriveHaitiKouhoList(char pPieceName)
{
char[,] wkArr = null;
// □■
//□■
// □
if (pPieceName == 'F') {
wkArr = new char[3, 3]; FillSpace(wkArr);
wkArr[1, 0] = 'W'; wkArr[2, 0] = 'B';
wkArr[0, 1] = 'W'; wkArr[1, 1] = 'B';
wkArr[1, 2] = 'W';
}
//■
//□
//■
//□
//■
if (pPieceName == 'I') {
wkArr = new char[1, 5]; FillSpace(wkArr);
wkArr[0, 0] = 'B';
wkArr[0, 1] = 'W';
wkArr[0, 2] = 'B';
wkArr[0, 3] = 'W';
wkArr[0, 4] = 'B';
}
//■□
//□■
//■
if (pPieceName == 'P') {
wkArr = new char[2, 3]; FillSpace(wkArr);
wkArr[0, 0] = 'B'; wkArr[1, 0] = 'W';
wkArr[0, 1] = 'W'; wkArr[1, 1] = 'B';
wkArr[0, 2] = 'B';
}
//□ □
//■□■
if (pPieceName == 'U') {
wkArr = new char[3, 2]; FillSpace(wkArr);
wkArr[0, 0] = 'W'; wkArr[2, 0] = 'W';
wkArr[0, 1] = 'B'; wkArr[1, 1] = 'W'; wkArr[2, 1] = 'B';
}
//■
//□
//■□
if (pPieceName == 'l') {
wkArr = new char[2, 3]; FillSpace(wkArr);
wkArr[0, 0] = 'B';
wkArr[0, 1] = 'W';
wkArr[0, 2] = 'B'; wkArr[1, 2] = 'W';
}
//■□
//□■
if (pPieceName == 'o') {
wkArr = new char[2, 2]; FillSpace(wkArr);
wkArr[0, 0] = 'B'; wkArr[1, 0] = 'W';
wkArr[0, 1] = 'W'; wkArr[1, 1] = 'B';
}
//■
//□
//■
if (pPieceName == 'a') {
wkArr = new char[1, 3]; FillSpace(wkArr);
wkArr[0, 0] = 'B';
wkArr[0, 1] = 'W';
wkArr[0, 2] = 'B';
}
//□
//■□
if (pPieceName == 'b') {
wkArr = new char[2, 2]; FillSpace(wkArr);
wkArr[0, 0] = 'W';
wkArr[0, 1] = 'B'; wkArr[1, 1] = 'W';
}
//■
//□
if (pPieceName == 'd') {
wkArr = new char[1, 2]; FillSpace(wkArr);
wkArr[0, 0] = 'B';
wkArr[0, 1] = 'W';
}
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 <= 8; I++) KaitenArrList.Add(null);
for (int P = 0; P <= 6; P += 2) KaitenArrList[P] = new char[BaseArrUB_X + 1, BaseArrUB_Y + 1];
for (int P = 1; P <= 7; 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;
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<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[,] pPieceArr, int pTargetX, int pTargetY, char[,] pBanArr)
{
if (pTargetX + pPieceArr.GetUpperBound(0) > UB) return false;
if (pTargetY + pPieceArr.GetUpperBound(1) > UB) return false;
for (int X = 0; X <= pPieceArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= pPieceArr.GetUpperBound(1); Y++) {
bool IsWhiteMasu = ((pTargetX + X + pTargetY + Y) % 2 == 1);
if (pPieceArr[X, Y] == 'B') {
if (pBanArr[pTargetX + X, pTargetY + Y] != ' ') return false;
if (IsWhiteMasu) return false;
}
if (pPieceArr[X, Y] == 'W') {
if (pBanArr[pTargetX + X, pTargetY + Y] != ' ') return false;
if (IsWhiteMasu == false) 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());
}
}