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 = { 'F', 'I', 'L', 'N', 'P', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'};
static int UB_X;
static int UB_Y;
struct JyoutaiDef
{
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
}
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
//2次元配列の各要素に、1をセット
Action<int[,]> ActArrFill_1 = (pTargetArr) =>
{
for (int X = 0; X <= pTargetArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= pTargetArr.GetUpperBound(1); Y++) {
pTargetArr[X, Y] = 1;
}
}
};
int[,] Q01Arr = new int[6, 10]; ActArrFill_1(Q01Arr);
int[,] Q02Arr = new int[3, 20]; ActArrFill_1(Q02Arr);
int[,] Q03Arr = new int[4, 15]; ActArrFill_1(Q03Arr);
int[,] Q04Arr = new int[5, 12]; ActArrFill_1(Q04Arr);
int[,] Q05Arr = {{1,1,1,1,1,1},
{1,1,1,1,1,1},
{1,1,1,1,1,1},
{1,1,1,0,0,0},
{1,1,1,0,0,0},
{1,1,1,0,0,0},
{1,1,1,1,1,1},
{1,1,1,1,1,1},
{1,1,1,1,1,1}};
int[,] Q06Arr = {{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0},
{1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0}};
int[,] Q07Arr = {{1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0},
{0,0,0,1,1,1,0,0,0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1,1,1,0,0,0,0,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}};
int[,] Q27Arr = {{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,1,1,1,1,1,1,1},
{1,1,1,1,1,0,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}};
int[,] Q28Arr = {{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,1,1,1,1,1},
{1,1,1,1,1,1,0,1,1,1,1,1,1},
{1,1,1,1,1,1,0,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}};
int[,] Q29Arr = {{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,0,0,0,0,0,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1}};
int[,] Q30Arr = Q01Arr;
int[,] WkArr = Q30Arr;
bool ValidEdakiriQ01 = false; //問題01の枝切りが有効か?
bool ValidEdakiriQ30 = true; //問題30の枝切りが有効か?
UB_X = WkArr.GetUpperBound(1);
UB_Y = WkArr.GetUpperBound(0);
int[,] QuestionArr = new int[UB_X + 1, UB_Y + 1];
for (int X = 0; X <= UB_X; X++)
for (int Y = 0; Y <= UB_Y; Y++)
QuestionArr[X, Y] = WkArr[Y, X];
foreach (char AnyPiece in PieceNameArr) {
HaitiKouhoListDict[AnyPiece] = DeriveHaitiKouhoList(AnyPiece);
}
var stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
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] = (QuestionArr[X, Y] == 1 ? ' ' : '*');
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}", sw.Elapsed);
PrintAnswer(Popped.BanArr);
return;
}
//Y座標の繰上げ処理
if (Popped.CurrY > UB_Y) {
Popped.CurrY = 0;
Popped.CurrX++;
}
//最終列を超えた場合
if (Popped.CurrX > UB_X) continue;
//使用済のピース名の配列
char[] UsedPieceArr = Popped.BanArr.Cast<char>().Distinct().ToArray();
foreach (char AnyPiece in PieceNameArr) {
if (Array.IndexOf(UsedPieceArr, AnyPiece) >= 0) continue;
//ピースの配置候補リスト
List<bool[,]> HaitiKouhoList = new List<bool[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[AnyPiece]);
//現在のマス目が空白の場合は、マス目を埋める必要あり
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.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY + 1;
bool IsRinsetuHuti = 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;
int Haiti_X = Popped.CurrX + X;
int Haiti_Y = Popped.CurrY + Y;
WillPush.BanArr[Haiti_X, Haiti_Y] = AnyPiece;
//ケースのふちに隣接しているか?を判定
if (Haiti_X == 0 || Haiti_X == UB_X) IsRinsetuHuti = true;
if (Haiti_Y == 0 || Haiti_Y == UB_Y) IsRinsetuHuti = true;
}
}
if (ValidEdakiriQ01 && IsEdakiriQ01(WillPush.BanArr)) continue;
if (ValidEdakiriQ30 && IsRinsetuHuti == false) continue;
stk.Push(WillPush);
}
}
//現在のマス目が空白でない場合は、ピースを配置しない経路のPush
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] != ' ') {
WillPush.BanArr = Popped.BanArr;
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY + 1;
stk.Push(WillPush);
}
}
}
//問題01の条件で、枝切りするかを返す
static bool IsEdakiriQ01(char[,] pBanArr)
{
Func<int, int, int, int, bool> wkIsEdakiri = (pX1, pY1, pX2, pY2) =>
{
if (pBanArr[pX1, pY1] == ' ') return false;
if (pBanArr[pX2, pY2] == ' ') return false;
if (pBanArr[pX1, pY1] == pBanArr[pX2, pY2]) return true;
return false;
};
if (wkIsEdakiri(2, 0, 3, 0)) return true;
if (wkIsEdakiri(2, 1, 3, 1)) return true;
if (wkIsEdakiri(2, 2, 3, 2)) return true;
if (wkIsEdakiri(3, 2, 3, 3)) return true;
if (wkIsEdakiri(4, 2, 4, 3)) return true;
if (wkIsEdakiri(5, 2, 5, 3)) return true;
if (wkIsEdakiri(6, 2, 6, 3)) return true;
if (wkIsEdakiri(6, 3, 7, 3)) return true;
if (wkIsEdakiri(6, 4, 7, 4)) return true;
if (wkIsEdakiri(6, 5, 7, 5)) return true;
return false;
}
//ピース名を引数として、回転させた配置のListを返す
static List<bool[,]> DeriveHaitiKouhoList(char pPieceName)
{
bool[,] wkArr = null;
// ■■
//■■
// ■
if (pPieceName == 'F') {
wkArr = new bool[3, 3];
wkArr[0, 0] = false; wkArr[1, 0] = wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = true; wkArr[2, 1] = false;
wkArr[0, 2] = false; wkArr[1, 2] = true; wkArr[2, 2] = false;
}
//■
//■
//■
//■
//■
if (pPieceName == 'I') {
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 == 'L') {
wkArr = new bool[2, 4];
wkArr[0, 0] = true; wkArr[1, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = false;
wkArr[0, 2] = true; wkArr[1, 2] = false;
wkArr[0, 3] = wkArr[1, 3] = true;
}
// ■
//■■
//■
//■
if (pPieceName == 'N') {
wkArr = new bool[2, 4];
wkArr[0, 0] = false; wkArr[1, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = true;
wkArr[0, 2] = true; wkArr[1, 2] = false;
wkArr[0, 3] = true; wkArr[1, 3] = false;
}
// ■
//■■
//■■
if (pPieceName == 'P') {
wkArr = new bool[2, 3];
wkArr[0, 0] = false; wkArr[1, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = true;
wkArr[0, 2] = wkArr[1, 2] = true;
}
//■■■
// ■
// ■
if (pPieceName == 'T') {
wkArr = new bool[3, 3];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = true;
wkArr[0, 1] = false; wkArr[1, 1] = true; wkArr[2, 1] = false;
wkArr[0, 2] = false; wkArr[1, 2] = true; wkArr[2, 2] = false;
}
//■ ■
//■■■
if (pPieceName == 'U') {
wkArr = new bool[3, 2];
wkArr[0, 0] = true; wkArr[1, 0] = false; wkArr[2, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
}
//■
//■
//■■■
if (pPieceName == 'V') {
wkArr = new bool[3, 3];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = false;
wkArr[0, 1] = true; wkArr[1, 1] = wkArr[2, 1] = false;
wkArr[0, 2] = wkArr[1, 2] = wkArr[2, 2] = true;
}
//■
//■■
// ■■
if (pPieceName == 'W') {
wkArr = new bool[3, 3];
wkArr[0, 0] = true; wkArr[1, 0] = wkArr[2, 0] = false;
wkArr[0, 1] = wkArr[1, 1] = true; wkArr[2, 1] = false;
wkArr[0, 2] = false; wkArr[1, 2] = wkArr[2, 2] = true;
}
// ■
//■■■
// ■
if (pPieceName == 'X') {
wkArr = new bool[3, 3];
wkArr[0, 0] = false; wkArr[1, 0] = true; wkArr[2, 0] = false;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = true;
wkArr[0, 2] = false; wkArr[1, 2] = true; wkArr[2, 2] = false;
}
// ■
//■■
// ■
// ■
if (pPieceName == 'Y') {
wkArr = new bool[2, 4];
wkArr[0, 0] = false; wkArr[1, 0] = true;
wkArr[0, 1] = wkArr[1, 1] = true;
wkArr[0, 2] = false; wkArr[1, 2] = true;
wkArr[0, 3] = false; wkArr[1, 3] = true;
}
//■■
// ■
// ■■
if (pPieceName == 'Z') {
wkArr = new bool[3, 3];
wkArr[0, 0] = wkArr[1, 0] = true; wkArr[2, 0] = false;
wkArr[0, 1] = false; wkArr[1, 1] = true; wkArr[2, 1] = false;
wkArr[0, 2] = false; wkArr[1, 2] = wkArr[2, 2] = true;
}
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)
{
for (int X = 0; X <= pPieceMap.GetUpperBound(0); X++) {
if (pTargetX + X > UB_X) return false;
for (int Y = 0; Y <= pPieceMap.GetUpperBound(1); Y++) {
if (pTargetY + Y > UB_Y) return false;
if (pPieceMap[X, Y] && pBanArr[pTargetX + X, pTargetY + Y] != ' ')
return false;
}
}
return true;
}
//解を出力
static void PrintAnswer(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]);
}
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
}