using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
const int UB_X = 3 * 5 * 2 - 1; //赤のピース5個をX軸の中央から置ける分
const int UB_Y = 3 * 5 - 1; //赤のピース5個を一番上から置ける分
//ピースごとの配置候補
static Dictionary<char, List<bool[,]>> HaitiKouhoListDict =
new Dictionary<char, List<bool[,]>>();
static char[] PieceNameArr = { '黒', '赤' };
struct JyoutaiDef
{
internal int Level;
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
}
static System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
static void Main()
{
foreach (char AnyPiece in PieceNameArr) {
HaitiKouhoListDict[AnyPiece] = DeriveHaitiKouhoList(AnyPiece);
}
var stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
//ピースの配置候補リスト
List<bool[,]> HaitiKouhoList;
HaitiKouhoList = new List<bool[,]>(HaitiKouhoListDict['赤']);
//ピースを配置する経路のPush処理
foreach (bool[,] AnyPieceMap in HaitiKouhoList) {
WillPush.Level = 1;
WillPush.BanArr = new char[UB_X + 1, UB_Y + 1];
for (int X = 0; X <= UB_X; X++)
for (int Y = 0; Y <= UB_Y; Y++)
WillPush.BanArr[X, Y] = ' ';
WillPush.CurrX = UB_X / 2;
WillPush.CurrY = 0;
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[X, Y] = DecToStr(WillPush.Level);
}
}
stk.Push(WillPush);
}
int HaitiCnt = 0;
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
//レベル判定
if (Popped.Level == 5) {
HaitiCnt++;
if (HaitiCnt % 500000 == 1)
Console.WriteLine("赤ピースの配置{0}を検証中。経過時間={1}", HaitiCnt, sw.Elapsed);
SetBlackPiece(ArrSyukusyou(Popped.BanArr));
continue;
}
//X座標の繰上げ処理
if (Popped.CurrX > UB_X) {
Popped.CurrX = 0;
Popped.CurrY++;
//ピースが配置されない行だったら枝切り
bool HasPiece = false;
for (int X = 0; X <= UB_X; X++) {
if (Popped.BanArr[X, Popped.CurrY - 1] != ' ') HasPiece = true;
}
if (HasPiece == false) continue;
}
//最終行の2行前を超えた場合(必ずY軸方向の長さが3なので)
if (Popped.CurrY > UB_Y - 2) continue;
//ピースの配置候補リスト
HaitiKouhoList = new List<bool[,]>(HaitiKouhoListDict['赤']);
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArr, ' ') == false);
//ピースを配置する経路のPush処理
foreach (bool[,] AnyPieceMap in HaitiKouhoList) {
WillPush.Level = Popped.Level + 1;
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
//配置済ピースに隣接していたら配置可
bool wkIsRinsetu = false;
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] = DecToStr(WillPush.Level);
if (IsRinsetu(Popped.CurrX + X, Popped.CurrY + Y, Popped.BanArr))
wkIsRinsetu = true;
}
}
if (wkIsRinsetu) stk.Push(WillPush);
}
//ピースを配置しない経路のPush
WillPush.Level = Popped.Level;
WillPush.BanArr = Popped.BanArr;
WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
stk.Push(WillPush);
}
}
//2次元配列の使用してない部分を縮小して返す
static char[,] ArrSyukusyou(char[,] pTargetArr)
{
int XMin = pTargetArr.GetUpperBound(0), YMin = pTargetArr.GetUpperBound(1);
int XMax = 0, YMax = 0;
for (int X = 0; X <= pTargetArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= pTargetArr.GetUpperBound(1); Y++) {
if (pTargetArr[X, Y] == ' ') continue;
if (XMin > X) XMin = X;
if (YMin > Y) YMin = Y;
if (XMax < X) XMax = X;
if (YMax < Y) YMax = Y;
}
}
char[,] WillReturnArr = new char[XMax - XMin + 1, YMax - YMin + 1];
for (int X = 0; X <= WillReturnArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= WillReturnArr.GetUpperBound(1); Y++) {
WillReturnArr[X, Y] = pTargetArr[XMin + X, YMin + Y];
}
}
return WillReturnArr;
}
static int AnswerCnt = 0;
//黒ピースが配置できるかを検証
static void SetBlackPiece(char[,] pBanArr)
{
int UB_X = pBanArr.GetUpperBound(0);
int UB_Y = pBanArr.GetUpperBound(1);
var stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.Level = 0;
WillPush.BanArr = new char[UB_X + 1, UB_Y + 1];
for (int X = 0; X <= UB_X; X++)
for (int Y = 0; Y <= UB_Y; Y++) {
char wkChar = pBanArr[X, Y];
if (wkChar == ' ') WillPush.BanArr[X, Y] = ' ';
else WillPush.BanArr[X, Y] = '?';
}
WillPush.CurrX = WillPush.CurrY = 0;
stk.Push(WillPush);
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
//クリア判定
if (Popped.BanArr.Cast<char>().All(X => X != '?')) {
Console.WriteLine("解{0}を発見。経過時間={1}", ++AnswerCnt, sw.Elapsed);
Console.WriteLine("赤ピースの配置");
PrintAnswer(pBanArr);
Console.WriteLine("黒ピースの配置");
PrintAnswer(Popped.BanArr);
continue;
}
//X座標の繰上げ処理
if (Popped.CurrX > UB_X) {
Popped.CurrX = 0;
Popped.CurrY++;
}
//最終行の1行前を超えた場合(必ずY軸方向の長さが2以上なので)
if (Popped.CurrY > UB_Y - 1) continue;
//ピースの配置候補リスト
List<bool[,]> HaitiKouhoList;
HaitiKouhoList = new List<bool[,]>(HaitiKouhoListDict['黒']);
//現在のマス目が空きマスの場合は、マス目を埋める必要あり
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.Level = Popped.Level + 1;
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
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] = DecToStr(WillPush.Level);
}
}
stk.Push(WillPush);
}
//現在のマス目が空きマスでない場合は、ピースを配置しない経路のPush
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] != '?') {
WillPush.Level = Popped.Level;
WillPush.BanArr = Popped.BanArr;
WillPush.CurrX = Popped.CurrX + 1;
WillPush.CurrY = Popped.CurrY;
stk.Push(WillPush);
}
}
}
//10進数をchar型に変換
static char DecToStr(int pInt)
{
if (pInt < 10) return (char)((int)'1' + pInt - 1);
return (char)((int)'A' + pInt - 10);
}
//ピース名を引数として、回転させた配置のListを返す
static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
{
bool[,] wkArr = null;
//■■
// ■■
//■■
if (pPieceName == '赤') {
wkArr = new bool[3, 3];
wkArr[0, 0] = wkArr[1, 0] = true; wkArr[2, 0] = false;
wkArr[0, 1] = false; wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = true; wkArr[2, 2] = false;
}
//■
//■■
//■
//■
if (pPieceName == '黒') {
wkArr = new bool[2, 4];
wkArr[0, 0] = true; wkArr[1, 0] = false;
wkArr[0, 1] = wkArr[1, 1] = true;
wkArr[0, 2] = true; wkArr[1, 2] = false;
wkArr[0, 3] = true; wkArr[1, 3] = false;
}
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度回転
// ■
// ■
//■■
//5個目は1個目とX軸で線対称
//■■■
//■
//6個目は5個目を時計回りに90度回転
//■■
// ■
// ■
//7個目は6個目を時計回りに90度回転
// ■
//■■■
//8個目は7個目を時計回りに90度回転
//■
//■
//■■
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, char pCharSpace)
{
for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
if (pTargetX + X > pBanArr.GetUpperBound(0)) return false;
for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
if (pTargetY + Y > pBanArr.GetUpperBound(1)) return false;
if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + Y] != pCharSpace)
return false;
}
}
return true;
}
//配置済ピースに隣接しているか
static bool IsRinsetu(int pTargetX, int pTargetY, char[,] pBanArr)
{
if (pTargetX + 1 <= UB_X && pBanArr[pTargetX + 1, pTargetY] != ' ') return true;
if (0 <= pTargetX - 1 && pBanArr[pTargetX - 1, pTargetY] != ' ') return true;
if (pTargetY + 1 <= UB_Y && pBanArr[pTargetX, pTargetY + 1] != ' ') return true;
if (0 <= pTargetY - 1 && pBanArr[pTargetX, pTargetY - 1] != ' ') return true;
return false;
}
//解を出力
static void PrintAnswer(char[,] pBanArr)
{
var sb = new System.Text.StringBuilder();
for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
sb.Append(pBanArr[X, Y]);
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
}