using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 5 5");
WillReturn.Add("1 1");
WillReturn.Add("3 3");
WillReturn.Add("4 4");
WillReturn.Add("2 3");
WillReturn.Add("2 5");
//Yes
}
else if (InputPattern == "Input2") {
WillReturn.Add("1 1 2");
WillReturn.Add("2 3");
//No
}
else if (InputPattern == "Input3") {
WillReturn.Add("1 2 2");
WillReturn.Add("1 1");
//No
}
else if (InputPattern == "Input4") {
WillReturn.Add("5 3 3");
WillReturn.Add("1 1");
WillReturn.Add("2 2");
WillReturn.Add("2 2");
WillReturn.Add("2 2");
WillReturn.Add("2 2");
//No
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
// ピースごとの配置候補
static Dictionary<char, List<bool[,]>> HaitiKouhoListDict =
new Dictionary<char, List<bool[,]>>();
static char[] PieceNameArr;
static int UB_X;
static int UB_Y;
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
int N = wkArr[0];
UB_X = wkArr[1] - 1;
UB_Y = wkArr[2] - 1;
PieceNameArr = new char[N];
for (int I = 0; I <= PieceNameArr.GetUpperBound(0); I++) {
PieceNameArr[I] = (char)('1' + I);
}
// 埋める必要があるマスが1
// 埋める必要がないマスが0
int[,] Q01Arr = new int[UB_X + 1, UB_Y + 1];
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
Q01Arr[X, Y] = 1;
}
}
char CurrPieceName = '1';
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int Width = wkArr[0];
int Height = wkArr[1];
HaitiKouhoListDict[CurrPieceName] = DeriveKaitenArrList(Width, Height);
CurrPieceName++;
}
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.CurrX = WillPush.CurrY = 0;
Stk.Push(WillPush);
bool FoundAnswer = false;
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
// クリア判定
if (IsClear(Popped.BanArr)) {
FoundAnswer = true;
break;
}
// X座標の繰上げ処理
if (Popped.CurrX > UB_X) {
Popped.CurrX = 0;
Popped.CurrY++;
}
// 最終行を超えた場合
if (Popped.CurrY > UB_Y) continue;
// 使用済のピース名のHashSet
var UsedPieceSet = DeriveUseSet(Popped.BanArr);
foreach (char EachPiece in PieceNameArr) {
if (UsedPieceSet.Contains(EachPiece)) continue;
// ピースの配置候補リスト
List<bool[,]> HaitiKouhoList = new List<bool[,]>();
HaitiKouhoList.AddRange(HaitiKouhoListDict[EachPiece]);
// 現在のマス目が空白の場合は、マス目を埋める必要あり
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[,] EachPieceMap in HaitiKouhoList) {
WillPush.BanArr = (char[,])Popped.BanArr.Clone();
WillPush.CurrX = Popped.CurrX;
WillPush.CurrY = Popped.CurrY;
for (int X = 0; X <= EachPieceMap.GetUpperBound(0); X++) {
for (int Y = 0; Y <= EachPieceMap.GetUpperBound(1); Y++) {
if (EachPieceMap[X, Y] == false) 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;
Stk.Push(WillPush);
}
}
Console.WriteLine(FoundAnswer ? "Yes" : "No");
}
static bool IsClear(char[,] pBanArr)
{
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
if (pBanArr[X, Y] == ' ') return false;
}
}
return true;
}
static HashSet<char> DeriveUseSet(char[,] pBanArr)
{
var WillReturn = new HashSet<char>();
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
if (pBanArr[X, Y] != ' ') {
WillReturn.Add(pBanArr[X, Y]);
}
}
}
return WillReturn;
}
struct JyoutaiDef
{
internal char[,] BanArr;
internal int CurrX;
internal int CurrY;
}
// マス目にピースを埋めれるか
static bool CanFillPiece(bool[,] 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] && pBanArr[pTargetX + X, pTargetY + Y] != ' ')
return false;
}
}
return true;
}
// 配列を引数として、回転させた配列のリストをDistinctして返す
static List<bool[,]> DeriveKaitenArrList(int pWidth, int pHeight)
{
var KaitenArrList = new List<bool[,]>();
bool[,] pBaseArr = new bool[pWidth, pHeight];
for (int X = 0; X <= pBaseArr.GetUpperBound(0); X++) {
for (int Y = 0; Y <= pBaseArr.GetUpperBound(1); Y++) {
pBaseArr[X, Y] = true;
}
}
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;
}
}
// 鏡像は不許可なので、4回RemoveAt
//KaitenArrList.RemoveAt(4);
//KaitenArrList.RemoveAt(4);
//KaitenArrList.RemoveAt(4);
//KaitenArrList.RemoveAt(4);
// 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;
}
////////////////////////////////////////////////////////////////
// 2次元配列(char型)のデバッグ出力
////////////////////////////////////////////////////////////////
static void PrintBan(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++) {
if (pBanArr[X, Y] == ' ') {
sb.Append('?');
}
else {
sb.Append(pBanArr[X, Y]);
}
}
sb.AppendLine();
}
Console.Write(sb.ToString());
}
}