using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Dictionary<int, string> mMonthNameDict = new Dictionary<int, string>();
//月名のDictを初期化
static void DeriveMonthNameDict()
{
mMonthNameDict.Add(1, "JANUARY");
mMonthNameDict.Add(2, "FEBRUARY");
mMonthNameDict.Add(3, "MARCH");
mMonthNameDict.Add(4, "APRIL");
mMonthNameDict.Add(5, "MAY");
mMonthNameDict.Add(6, "JUNE");
mMonthNameDict.Add(7, "JULY");
mMonthNameDict.Add(8, "AUGUST");
mMonthNameDict.Add(9, "SEPTEMBER");
mMonthNameDict.Add(10, "OCTOBER");
mMonthNameDict.Add(11, "NOVEMBER");
mMonthNameDict.Add(12, "DECEMBER");
}
static int UB_X;
static int UB_Y;
static System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
static void Main()
{
//月名のDictを初期化
DeriveMonthNameDict();
int StaMenseki =
mMonthNameDict.Values.SelectMany(A => A.ToCharArray()).Distinct().Count();
//面積でループ
for (int Menseki = StaMenseki; ; Menseki++) {
//最低2つは、横置きするので横幅の最低は4
for (int X = 4; X <= Menseki; X++) {
for (int Y = X; Y <= Menseki; Y++) {
if (X * Y != Menseki) continue;
//SEPTEMBERが9文字なので、縦が9未満なら不適
if (Y < 9) continue;
UB_X = X - 1;
UB_Y = Y - 1;
Console.WriteLine("面積={0,2}(横{1,2}、縦{2,2})の長方形を判定中。経過時間={3}"
, Menseki, X, Y, sw.Elapsed);
if (CanShikitume()) return;
}
}
}
}
struct JyoutaiDef
{
internal char[,] BanArr;
internal HashSet<int> UsedMonthSet; //使用済の月のSet
internal int CanSetMinY; //次の配置での最小のY座標
}
//敷き詰め可能かを返す
static bool CanShikitume()
{
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] = '*';
}
}
WillPush.UsedMonthSet = new HashSet<int>();
WillPush.CanSetMinY = 0;
stk.Push(WillPush);
var VisitedSet = new HashSet<string>();
VisitedSet.Add(BanToStr(WillPush.BanArr));
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
//クリア判定
if (Popped.UsedMonthSet.Count == 12) {
Console.WriteLine("解を発見。経過時間={0}", sw.Elapsed);
PrintBan(Popped.BanArr);
return true;
}
for (int Y = 0; Y <= Popped.CanSetMinY; Y++) {
for (int X = 0; X <= UB_X; X++) {
foreach (var EachPair in mMonthNameDict) {
if (Popped.UsedMonthSet.Contains(EachPair.Key)) continue;
PushSyori(Popped, X, Y, EachPair, VisitedSet, stk);
}
}
}
}
return false;
}
//Push処理
static void PushSyori(JyoutaiDef pPopped, int pX, int pY, KeyValuePair<int, string> pEachPair,
HashSet<string> pVisitedSet, Stack<JyoutaiDef> pStk)
{
bool NeedConn = (pPopped.UsedMonthSet.Count > 0);
//縦に置く場合
if (CanSetTate(pPopped.BanArr, pX, pY, pEachPair.Value, NeedConn)) {
//端に置けない月かを判定
bool IsOK = true;
if (pX == 0) {
if (pEachPair.Value == "APRIL") IsOK = false;
if (pEachPair.Value == "OCTOBER") IsOK = false;
}
if (pX == UB_X) {
if (pEachPair.Value == "MAY") IsOK = false;
if (pEachPair.Value == "JUNE") IsOK = false;
if (pEachPair.Value == "AUGUST") IsOK = false;
}
if (IsOK) {
JyoutaiDef WillPush;
WillPush.BanArr = (char[,])pPopped.BanArr.Clone();
WillPush.UsedMonthSet = new HashSet<int>(pPopped.UsedMonthSet) { pEachPair.Key };
WillPush.CanSetMinY = pPopped.CanSetMinY;
for (int I = 0; I <= pEachPair.Value.Length - 1; I++) {
WillPush.BanArr[pX, pY + I] = pEachPair.Value[I];
if (WillPush.CanSetMinY < pY + I)
WillPush.CanSetMinY = pY + I;
}
if (pVisitedSet.Add(BanToStr(WillPush.BanArr)))
pStk.Push(WillPush);
}
}
//横に置く場合
if (CanSetYoko(pPopped.BanArr, pX, pY, pEachPair.Value, NeedConn)) {
//端に置けない月かを判定
bool IsOK = true;
if (pY == 0) {
if (pEachPair.Value == "APRIL") IsOK = false;
if (pEachPair.Value == "OCTOBER") IsOK = false;
}
if (pY == UB_Y) {
if (pEachPair.Value == "MAY") IsOK = false;
if (pEachPair.Value == "JUNE") IsOK = false;
if (pEachPair.Value == "AUGUST") IsOK = false;
}
if (IsOK) {
JyoutaiDef WillPush;
WillPush.BanArr = (char[,])pPopped.BanArr.Clone();
WillPush.UsedMonthSet = new HashSet<int>(pPopped.UsedMonthSet) { pEachPair.Key };
WillPush.CanSetMinY = pPopped.CanSetMinY;
for (int I = 0; I <= pEachPair.Value.Length - 1; I++) {
WillPush.BanArr[pX + I, pY] = pEachPair.Value[I];
}
if (pVisitedSet.Add(BanToStr(WillPush.BanArr)))
pStk.Push(WillPush);
}
}
}
//縦に置けるかのチェック
static bool CanSetTate(char[,] pBanArr, int pCurrX, int pCurrY, string pMonthName, bool pNeedConn)
{
if (pCurrY + pMonthName.Length - 1 > UB_Y) return false;
bool HasConn = false;
for (int I = 0; I <= pMonthName.Length - 1; I++) {
char CurrChar = pBanArr[pCurrX, pCurrY + I];
if (CurrChar == '*') continue;
else if (CurrChar == pMonthName[I]) HasConn = true;
else return false;
}
return pNeedConn ? HasConn : true;
}
//横に置けるかのチェック
static bool CanSetYoko(char[,] pBanArr, int pCurrX, int pCurrY, string pMonthName, bool pNeedConn)
{
if (pCurrX + pMonthName.Length - 1 > UB_X) return false;
bool HasConn = false;
for (int I = 0; I <= pMonthName.Length - 1; I++) {
char CurrChar = pBanArr[pCurrX + I, pCurrY];
if (CurrChar == '*') continue;
else if (CurrChar == pMonthName[I]) HasConn = true;
else return false;
}
return pNeedConn ? HasConn : true;
}
//盤面をString型に変換
static string BanToStr(char[,] pBanArr)
{
var sb = new System.Text.StringBuilder();
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
sb.Append(pBanArr[X, Y]);
}
}
return sb.ToString();
}
//盤面を出力
static void PrintBan(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());
}
}