IDE: Visual Studio 2012
视频: https://www.bilibili.com/video/BV1JL4y1378X/
源码下载:https://pan.baidu.com/s/1YIp6qK4bugnwR9uOkdcK-A?pwd=3xj7
主程序 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DayPuzzle {
class Program {
/// <summary>
/// 要解的月份
/// </summary>
static int Month = 1;
/// <summary>
/// 要解的日份
/// </summary>
static int Day = 1;
static void Main(string[] args) {
while (true) {
//获取日期输入
GetDateInput();
//克隆 预设拼图块 列表
List<PuzzlePiece> PieceList = PuzzlePiece.ClonePieceList();
//新建 底盘Map
PuzzleMap Map = new PuzzleMap();
//根据日期 锁定 底盘对应点
if (Month % 6 == 0) Map.LockPoint(6, 7 - (Month - 1) / 6);
else Map.LockPoint(Month % 6, 7 - Month / 6);
if (Day % 7 == 0) Map.LockPoint(7, 5 - (Day - 1) / 7);
else Map.LockPoint(Day % 7, 5 - Day / 7);
//设置游标起点
Map.SetCursor(1, 7);
//步进
int step = 0;
//拼图块索引 排列 数组
int[] pieceSeq = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
//拼图块放置方向 数组
int[] pieceDirSeq = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
//拼图块置位 标志
bool setFlag = false;
//当前准备放置的拼图块
PuzzlePiece piece = null;
while (step < 8) { //如果步进大于等于8(步进有效值0~7) 退出循环
setFlag = false; //清空拼图块置位标志
while (pieceSeq[step] < 8) { //如果当前拼图块索引超出范围 退出循环
piece = PieceList[pieceSeq[step]]; //获取拼图块
if (piece.SetPoint == null) { //判断当前拼图块是否已经放置
while (pieceDirSeq[step] < piece.EffectiveDirection.Count) { //如果尝试过所有方向 退出循环
piece.Direction = piece.EffectiveDirection[pieceDirSeq[step]]; //设置当前拼图 放置方向
//调用 放置拼图块到底盘 方法
if (Map.SetPiece(piece)) { //拼图块成功放置
setFlag = true; //置位标志
break; //退出循环
} else { //如果没有放置成功
pieceDirSeq[step] += 1; //尝试当前拼图其他放置方向
}
}
}
if (setFlag == true) { //如果放置成功
break; //退出循环
} else { //如果没有放置成功
pieceSeq[step] += 1; //尝试下一个拼图块
pieceDirSeq[step] = 0; //清除存储的拼图块放置方向
}
}
if (setFlag == true) { //如果拼图块放置成功
if (Map.PieceList.Count == 8) { //成功放置8块拼图 拼图完成
break;
} else { //拼图没有完成
step += 1; //步进加1
pieceSeq[step] = 0; //清除存储的当前拼图块索引
pieceDirSeq[step] = 0; //清除存储的当前拼图块放置方向
PuzzlePoint p = Map.GetNextCursor(); //获取下一个
if (p == null) { //如果没有可以继续放置的点
Console.WriteLine(Map.ToString()); //输出当前错误排列信息
Map.RemovePrevPiece(); //移除上一块放置的拼图块
step -= 1; //步进减1 返回上一步
pieceDirSeq[step] += 1;//尝试以其他方向放置上一步的拼图块
}
}
} else { //如果拼图块没有放置成功 当前点没有拼图块可以设置
pieceSeq[step] = 0; //清除存储的当前拼图块索引
pieceDirSeq[step] = 0; //清除存储的当前拼图块放置方向
PuzzlePoint p = Map.GetNextCursor(); //游标移动到下一个空置的点
if (p == null) { //如果没有可以继续放置的点
Console.WriteLine(Map.ToString()); //输出当前错误排列信息
Map.RemovePrevPiece(); //移除上一块放置的拼图块
step -= 1; //步进减1 返回上一步
pieceDirSeq[step] += 1; //尝试以其他方向放置上一步的拼图块
}
}
}
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine(Map.ToMapString()); //输出结果
Console.ReadLine();
}
}
/// <summary>
/// 获取日期输入
/// </summary>
static void GetDateInput() {
string input = null;
while (true) {
try {
Console.Write("输入月份:");
input = Console.ReadLine();
Month = int.Parse(input);
Console.Write("输入日份:");
input = Console.ReadLine();
Day = int.Parse(input);
if (Month > 0 && Month <= 12) {
if (Day > 0 && Day <= 31) {
break;
}
}
} catch (Exception) {
} finally {
}
Console.WriteLine("输入错误请重新输入");
}
}
}
}
拼图块类(PuzzlePiece)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DayPuzzle {
/// <summary>
/// 拼图块 类
/// </summary>
public class PuzzlePiece {
/// <summary>
/// 放置 方向
/// </summary>
public enum SetDirection {
/// <summary>
/// 原始方向
/// </summary>
Origin = 0,
/// <summary>
/// 顺时针旋转90度
/// </summary>
Rotate90 = 1,
/// <summary>
/// 顺时针旋转180度
/// </summary>
Rotate180 = 2,
/// <summary>
/// 顺时针旋转270度
/// </summary>
Rotate270 = 3,
/// <summary>
/// 镜像
/// </summary>
Mirror = 4,
/// <summary>
/// 镜像后顺时针旋转90度
/// </summary>
MirrorRotate90 = 5,
/// <summary>
/// 镜像后顺时针旋转180度
/// </summary>
MirrorRotate180 = 6,
/// <summary>
/// 镜像后顺时针旋转270度
/// </summary>
MirrorRotate270 = 7,
};
/// <summary>
/// 双轴对称 拼图块 方向数组
/// </summary>
private static SetDirection[] BiaxialSymmetryDirection = new SetDirection[] {
SetDirection.Rotate90
};
/// <summary>
/// 单轴对称 拼图块 方向数组
/// </summary>
private static SetDirection[] UniaxialSymmetryDirection = new SetDirection[] {
SetDirection.Rotate90, SetDirection.Rotate180, SetDirection.Rotate270
};
/// <summary>
/// 非轴对称 拼图块 方向数组
/// </summary>
private static SetDirection[] NonaxialSymmetryDirection = new SetDirection[] {
SetDirection.Mirror,
SetDirection.Rotate90, SetDirection.Rotate180, SetDirection.Rotate270,
SetDirection.MirrorRotate90, SetDirection.MirrorRotate180, SetDirection.MirrorRotate270
};
/// <summary>
/// 矩形 拼图块
/// </summary>
public static PuzzlePiece Piece1 = new PuzzlePiece(
1,
BiaxialSymmetryDirection,
new PuzzlePoint[] {
PuzzlePoint.Get(0, 0), PuzzlePoint.Get(1, 0), PuzzlePoint.Get(2, 0),
PuzzlePoint.Get(0, 1), PuzzlePoint.Get(1, 1), PuzzlePoint.Get(2, 1)
},
new PuzzlePoint[] {
PuzzlePoint.Get(0, -1), PuzzlePoint.Get(1, -1), PuzzlePoint.Get(2, -1),
PuzzlePoint.Get(0, 2), PuzzlePoint.Get(1, 2), PuzzlePoint.Get(2, 2),
PuzzlePoint.Get(-1, 0), PuzzlePoint.Get(-1, 1),
PuzzlePoint.Get(3, 0), PuzzlePoint.Get(3, 1),
}
);
/// <summary>
/// 凹形 拼图块
/// </summary>
public static PuzzlePiece Piece2 = new PuzzlePiece(
2,
UniaxialSymmetryDirection,
new PuzzlePoint[] {
PuzzlePoint.Get(0, 0), PuzzlePoint.Get(1, 0), PuzzlePoint.Get(2, 0),
PuzzlePoint.Get(0, 1), PuzzlePoint.Get(2, 1)
},
new PuzzlePoint[] {
PuzzlePoint.Get(0, -1), PuzzlePoint.Get(1, -1), PuzzlePoint.Get(2, -1),
PuzzlePoint.Get(0, 2), PuzzlePoint.Get(1, 1), PuzzlePoint.Get(2, 2),
PuzzlePoint.Get(-1, 0), PuzzlePoint.Get(-1, 1),
PuzzlePoint.Get(3, 0), PuzzlePoint.Get(3, 1),
}
);
/// <summary>
/// ::.形 拼图块
/// </summary>
public static PuzzlePiece Piece3 = new PuzzlePiece(
3,
NonaxialSymmetryDirection,
new PuzzlePoint[] {
PuzzlePoint.Get(0, 0), PuzzlePoint.Get(1, 0), PuzzlePoint.Get(2, 0),
PuzzlePoint.Get(0, 1), PuzzlePoint.Get(1, 1)
},
new PuzzlePoint[] {
PuzzlePoint.Get(0, -1), PuzzlePoint.Get(1, -1), PuzzlePoint.Get(2, -1),
PuzzlePoint.Get(0, 2), PuzzlePoint.Get(1, 2), PuzzlePoint.Get(2, 1),
PuzzlePoint.Get(-1, 0), PuzzlePoint.Get(-1, 1),
PuzzlePoint.Get(3, 0),
}
);
/// <summary>
/// .:..形 拼图块
/// </summary>
public static PuzzlePiece Piece4 = new PuzzlePiece(
4,
NonaxialSymmetryDirection,
new PuzzlePoint[] {
PuzzlePoint.Get(0, 0), PuzzlePoint.Get(1, 0), PuzzlePoint.Get(2, 0), PuzzlePoint.Get(3, 0),
PuzzlePoint.Get(1, 1)
},
new PuzzlePoint[] {
PuzzlePoint.Get(0, -1), PuzzlePoint.Get(1, -1), PuzzlePoint.Get(2, -1), PuzzlePoint.Get(3, -1),
PuzzlePoint.Get(0, 1), PuzzlePoint.Get(1, 2), PuzzlePoint.Get(2, 1), PuzzlePoint.Get(3, 1),
PuzzlePoint.Get(-1, 0),
PuzzlePoint.Get(4, 0),
}
);
/// <summary>
/// S形 拼图块
/// </summary>
public static PuzzlePiece Piece5 = new PuzzlePiece(
5,
NonaxialSymmetryDirection,
new PuzzlePoint[] {
PuzzlePoint.Get(0, 0), PuzzlePoint.Get(1, 0),
PuzzlePoint.Get(1, 1),
PuzzleP