using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
const char SP = ' '; //全角空白
const int UB_X = 6 - 1;
const int UB_Y = 5 - 1;
//ピースごとの配置候補
static Dictionary<char, List<char[,]>> HaitiKouhoListDict =
new Dictionary<char, List<char[,]>>();
static char[] PieceNameArr = { '1', '2', '3', '4', '5', '6', '7' };
struct JyoutaiDef
{
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
}
static int[] QuestionArr;
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
int[] Q1Arr = { 0, 3, 1, 1, 1, 0 };
int[] Q2Arr = { 2, 0, 2, 0, 2, 0 };
int[] Q3Arr = { 1, 1, 1, 1, 1, 1 };
int[] Q4Arr = { 1, 0, 3, 0, 2, 0 };
int[] Q5Arr = { 2, 2, 0, 1, 1, 0 };
QuestionArr = Q1Arr;
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] = SP;
WillPush.CurrX = WillPush.CurrY = 0;
stk.Push(WillPush);
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
//クリア判定
if (IsClear(Popped.BanArr)) {
Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
PrintAnswer(Popped.BanArr);
return;
}
//Y座標の繰上げ処理
if (Popped.CurrY > UB_Y) {
Popped.CurrY = 0;
Popped.CurrX++;
//指定列の花火の数をチェック
if (IsOKHanabiCnt(Popped.BanArr, Popped.CurrX - 1) == false)
continue;
}
//最終列の1列前を超えた場合(必ずX軸方向の長さが2以上なので)
if (Popped.CurrX > UB_X - 1) continue;
//使用済のピース名の配列
char[] UsedPieceArr = Popped.BanArr.Cast<char>().Distinct().ToArray();
foreach (char AnyPiece in PieceNameArr) {
if (Array.IndexOf(UsedPieceArr, AnyPiece) >= 0) continue;
//対称解の除外で、ピース7は、上半分に配置する
if (AnyPiece == '7' && Popped.CurrY >= 2)
continue;
//ピースの配置候補リスト
List<char[,]> HaitiKouhoList = new List<char[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[AnyPiece]);
//現在のマス目が空白の場合は、マス目を埋める必要あり
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] == SP) {
HaitiKouhoList.RemoveAll(X => X[0, 0] == SP);
}
//マス目にピースを埋めれない候補をRemove
HaitiKouhoList.RemoveAll(X =>
CanFillPiece(X, Popped.CurrX, Popped.CurrY, Popped.BanArr) == false);
//ピースを配置する経路のPush処理
foreach (char[,] AnyPieceMap in HaitiKouhoList) {
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY + 1;
//花火の配置数が0の列だったら、花火を配置しない
bool WillEdairi = false;
for (int X = 0; X <= AnyPieceMap.GetUpperBound(0); X++) {
for (int Y = 0; Y <= AnyPieceMap.GetUpperBound(1); Y++) {
if (AnyPieceMap[X, Y] == SP) continue;
if (AnyPieceMap[X, Y] == '◎') {
WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = '◎';
if (QuestionArr[Popped.CurrX + X] == 0) {
WillEdairi = true;
break;
}
}
if (AnyPieceMap[X, Y] == '■')
WillPush.BanArr[Popped.CurrX + X, Popped.CurrY + Y] = AnyPiece;
}
if (WillEdairi) break;
}
if (WillEdairi == false) stk.Push(WillPush);
}
}
//現在のマス目が空白でない場合は、ピースを配置しない経路のPush
if (Popped.BanArr[Popped.CurrX, Popped.CurrY] != SP) {
WillPush.BanArr = Popped.BanArr;
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY + 1;
stk.Push(WillPush);
}
}
}
//指定列の花火の数をチェック
static bool IsOKHanabiCnt(char[,] pBanArr, int pX)
{
int Cnt = 0;
for (int Y = 0; Y <= UB_Y; Y++) {
if (pBanArr[pX, Y] == '◎') Cnt++;
}
return QuestionArr[pX] == Cnt;
}
//クリア判定
static bool IsClear(char[,] pBanArr)
{
//全列の花火の数の配列
int[] CntArr = new int[UB_X + 1];
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
//空白を含んでいないこと
if (pBanArr[X, Y] == SP) return false;
if (pBanArr[X, Y] == '◎')
CntArr[X]++;
}
}
return CntArr.SequenceEqual(QuestionArr);
}
//ピース名を引数として、回転させた配置のListを返す
static List<char[,]> DeriveHaitiKouhoList(char pPieceName)
{
char[,] wkArr = null;
//◎■■
//■
if (pPieceName == '1') {
wkArr = new char[3, 2];
wkArr[0, 0] = '◎'; wkArr[1, 0] = wkArr[2, 0] = '■';
wkArr[0, 1] = '■'; wkArr[1, 1] = wkArr[2, 1] = SP;
}
//■■■
// ◎
if (pPieceName == '2') {
wkArr = new char[3, 2];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = '■';
wkArr[0, 1] = wkArr[1, 1] = SP; wkArr[2, 1] = '◎';
}
//■◎
// ■
if (pPieceName == '3') {
wkArr = new char[2, 2];
wkArr[0, 0] = '■'; wkArr[1, 0] = '◎';
wkArr[0, 1] = SP; wkArr[1, 1] = '■';
}
//■◎
//■
if (pPieceName == '4') {
wkArr = new char[2, 2];
wkArr[0, 0] = '■'; wkArr[1, 0] = '◎';
wkArr[0, 1] = '■'; wkArr[1, 1] = SP;
}
//◎
//■■■■
if (pPieceName == '5') {
wkArr = new char[4, 2];
wkArr[0, 0] = '◎'; wkArr[1, 0] = wkArr[2, 0] = wkArr[3, 0] = SP;
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = wkArr[3, 1] = '■';
}
// ■
//■■■◎
if (pPieceName == '6') {
wkArr = new char[4, 2];
wkArr[0, 0] = wkArr[1, 0] = wkArr[2, 0] = SP; wkArr[3, 0] = '■';
wkArr[0, 1] = wkArr[1, 1] = wkArr[2, 1] = '■'; wkArr[3, 1] = '◎';
}
//■■
//■■
//■■
if (pPieceName == '7') {
wkArr = new char[2, 3];
wkArr[0, 0] = wkArr[1, 0] = '■';
wkArr[0, 1] = wkArr[1, 1] = '■';
wkArr[0, 2] = wkArr[1, 2] = '■';
}
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[,] 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] != SP && pBanArr[pTargetX + X, pTargetY + Y] != SP)
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());
}
}