IDE: Visual Studio 2012
视频:https://www.bilibili.com/video/BV1pu411Q73D/
源码下载:https://pan.baidu.com/s/1YIp6qK4bugnwR9uOkdcK-A?pwd=3xj7
Program.cs
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;
/// <summary>
/// 要解的星期
/// </summary>
static int Week = 1;
static void Main(string[] args) {
while (true) {
//获取日期输入
GetDateInput();
/**/
//----------------------------------------------------------------------------------------
//阶段1
PuzzleSolutionPhase1 p1 = new PuzzleSolutionPhase1(1);
p1.Lock(Month, Day, Week);
while (true) {
p1.Process();
if (p1.Proc == PuzzleSolutionPhase.PuzzleSolutionProcess.COMPLETE) {
break;
}
}
Console.WriteLine("-------------------------------------------------------");
for (int i = 0; i < p1.Phase2Cache.Keys.Count; i++) {
Console.WriteLine("(" + i + ") :" + p1.Phase2Cache[i].Count);
}
Console.ReadLine();
//----------------------------------------------------------------------------------------
//阶段2
List<PuzzleSolutionPhase2> p2list = new List<PuzzleSolutionPhase2>(); //阶段2方案执行列表
bool complete = false; //完成标志
int procnum = 1; //加载的方案计数
while (true) {
for (int i = 0; i < p2list.Count; i++) {
p2list[i].Process();
if (p2list[i].Processed == true) {
//判断方案是否处理完成
if (p2list[i].Proc == PuzzleSolutionPhase.PuzzleSolutionProcess.COMPLETE) {
//输出结果
Console.WriteLine("-------------------------------------------------------");
Console.WriteLine(p2list[i].ID + ") " + p2list[i].Map.ToString());
Console.WriteLine(p2list[i].Map.ToMapString());
complete = true;
} else {
Console.WriteLine("-1");
p2list.RemoveAt(i);
}
break;
}
}
if (complete == true) {
break;
}
//执行方案加载
if (p2list.Count < 30) {
PuzzleSolutionPhase2 p2 = p1.GetNextPhase2();
if (p2 != null) {
p2.ID = procnum;
procnum++;
p2list.Add(p2);
}
}
}
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);
Console.Write("输入星期:");
input = Console.ReadLine();
Week = int.Parse(input);
if (Month > 0 && Month <= 12) {
if (Day > 0 && Day <= 31) {
if (Week > 0 && Week <= 7) {
break;
}
}
}
} catch (Exception) {
} finally {
}
Console.WriteLine("输入错误请重新输入");
}
}
}
}
PuzzleMap.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DayPuzzle {
/// <summary>
/// 拼图 底盘 类
/// </summary>
public class PuzzleMap {
/// <summary>
/// 搜索方向
/// </summary>
public enum SearchDirection {
/// <summary>
/// 右下
/// </summary>
RightDown = 0,
/// <summary>
/// 左下
/// </summary>
LeftDown = 1,
/// <summary>
/// 右上
/// </summary>
RightUp = 2,
/// <summary>
/// 左上
/// </summary>
LeftUp = 3,
}
public static PuzzleMap GetNewMap() {
return new PuzzleMap();
}
/// <summary>
/// 构造方法
/// </summary>
public PuzzleMap() {
ResetMap();
}
/// <summary>
/// 描述底盘状态的二维数组
/// </summary>
public PuzzlePoint[,] PointMap = new PuzzlePoint[8, 11];
//public PuzzlePoint[,] PointMap = new PuzzlePoint[9, 9];
/// <summary>
/// 置入底盘的拼图列表
/// </summary>
public List<PuzzlePiece> PieceList = new List<PuzzlePiece>();
/// <summary>
/// 判断拼图是否完成
/// </summary>
/// <returns></returns>
public bool IsComplete() {
return (PieceList.Count >= 9);
}
/// <summary>
/// 转换成结果字符串
/// </summary>
/// <returns></returns>
public override string ToString() {
string result = "";
foreach (PuzzlePiece piece in PieceList) {
result = result + " " + piece.ToString();
}
return result;
}
/// <summary>
/// 转换成结果字符串
/// </summary>
/// <returns></returns>
public string ToMapString() {
string result = "";
int[,] map = new int[8,11];
foreach (PuzzlePiece piece in PieceList) {
//piece.SetPoint.X;
foreach (PuzzlePoint point in piece.CurrentRelativePosition) {
int tempX = piece.SetPoint.X + point.X;
int tempY = piece.SetPoint.Y + point.Y;
map[tempX, tempY] = piece.ID;
}
}
for (int i = 10; i >= 0; i--) {
for (int j = 0; j < 8; j++) {
if (this.PointMap[j, i].Status == PuzzlePointStatus.Locked) {
result = result + " +";
} else if (map[j, i] == 0) {
if (i == 10 || i == 0) {
result = result + " -";
} else if (j == 0 || j == 7) {
result = result + " -";
} else {
result = result + " -";
}
} else {
result = result + " " + map[j, i];
}
}
result = result + "\r\n";
}
return result;
}
/// <summary>
/// 获得指定点对象
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public PuzzlePoint GetPoint(int x, int y) {
return PointMap[x, y];
}
/// <summary>
/// 获得指定点对象
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public PuzzlePoint GetPoint(PuzzlePoint point) {
return PointMap[point.X, point.Y];
}
/// <summary>
/// 获得指定点的状态
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public PuzzlePointStatus GetPointStatus(PuzzlePoint point) {
return PointMap[point.X, point.Y].Status;
}
/// <summary>
/// 获得指定点的状态
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public PuzzlePointStatus GetPointStatus(int x, int y) {
return PointMap[x, y].Status;
}
/// <summary>
/// 置位指定点
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void SetPoint(int x, int y) {
PointMap[x, y].Status = PuzzlePointStatus.Set;
}
/// <summary>
/// 锁定指定点
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void LockPoint(int x, int y) {
PointMap[x, y].Status = PuzzlePointStatus.Locked;
}
/// <summary>
/// 置空指定点
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
private void RemovePoint(int x, int y) {
PointMap[x, y].Status = PuzzlePointStatus.Null;
}
/// <summary>
/// 检查指定点是否被孤立
/// </summary>
/// <param name="point">要检查的点</param>
/// <returns>是否被孤立</returns>
public Boolean CheckPointAlone(List<PuzzlePoint> point) {
for (int i = 0; i < point.Count; i++) {
if (CheckPointAlone(point[i].X, point[i].Y)) {
return true;
}
}
return false;
}
/// <summary>
/// 检查指定点是否被孤立
/// </summary>
/// <param name="x">要检查点的X坐标</param>
/// <param name="y">要检查点的Y坐标</param>
/// <returns>是否被孤立</returns>
private Boolean CheckPointAlone(int x, int y) {
if (GetPointStatus(x, y) != PuzzlePointStatus.Null) {
return false;
} else {
List<PuzzlePoint> pointList = GetNearNullPoint(x, y);
if (pointList.Count == 0) {
return true;
} else if (pointList.Count == 1) {
List<PuzzlePoint> pointList2 = GetNearNullPoint(pointList[0]);
if (pointList2.Count == 1) {
return true;
} else if (pointList2.Count == 2) {
if (GetNearNullPoint(pointList2[0]).Count == 1 && GetNearNullPoint(pointList2[1]).Count == 1) {
return true;
}
} else if (pointList2.Count == 3) {
if (GetNearNullPoint(pointList2[0]).Count == 1 && GetNearNullPoint(pointList2[1]).Count == 1 && GetNearNullPoint(pointList2[2]).Count == 1) {
return true;
}
}
} else if (pointList.Count == 2) {
if (GetNearNullPoint(pointList[0]).Count == 1 && GetNearNullPoint(pointList[1]).Count == 1) {
return true;
}
} else if (pointList.Count == 3) {
if (GetNearNullPoint(pointList[0]).Count == 1 && GetNearNullPoint(pointList[1]).Count == 1 && GetNearNullPoint(pointList[2]).Count == 1) {
return true;
}
}
}
return false;
}
private List<PuzzlePoint> GetNearNullPoint(PuzzlePoint point) {
return GetNearNullPoint(point.X, point.Y);
}
private List<PuzzlePoint> GetNearNullPoint(int x, int y) {
List<PuzzlePoint> list = new List<PuzzlePoint>();
if (GetPointStatus(x + 1, y) == PuzzlePointStatus.Null) {
list.Add(this.GetPoint(x + 1, y)); }
if (GetPointStatus(x - 1, y) == PuzzlePointStatus.Null) {
list.Add(this.GetPoint(x - 1, y)); }
if (GetPointStatus(x, y + 1) == PuzzlePointStatus.Null) {
list.Add(this.GetPoint(x, y + 1)); }
if (GetPointStatus(x, y - 1) == PuzzlePointStatus.Null) {
list.Add(this.GetPoint(x, y - 1)); }
return list;