资源简介
用C#写的马的极小满覆盖问题:
在8×8的国际象棋棋盘上,如果在放置若干个马以后,使得整个棋盘的任意空位置上所放置的棋子均能被这些马吃掉,则称这组放置为棋盘的一个满覆盖。若去掉满覆盖中的任意一个棋子都会使这组放置不再是满覆盖,则称这一满覆盖为极小满覆盖。
求解一个极小满覆盖,按照矩阵形式给出,用特殊符号表示马。
代码片段和文件信息
/*
* 程序参考:https://max.book118.com/html/2015/0619/19399060.shtm 数据结构课程设计报告--马儿覆盖问题.doc
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
class Program
{
/*以下变量为全局信息,各个函数可以调用*/
static private int M;//棋盘长
static private int N;//棋盘宽
static List directionList;//方向列表,存储马可走的8个方向
static Dictionary chessboard;//棋盘状态,Postion代表棋盘位置,int代表是否有子。如果为0,表示未占用,为1表示占用。M*N个元素可以代表整个棋盘信息
static Postion startPostion;//遍历的起始点
static List horseHome;//存储运算结果马所在的位置列表
//获得周围可行格子坐标(最多8个)
static List getValiablePos(Postion postion)
{
List valiablelist=new List();
//向周围8个方向遍历,如果新探索位置(newPostion)在棋盘外,则不予记录,否则放到可行列表中
foreach(var i in directionList)
{
Postion newPostion = new Postion(postion + i);
if (chessboard.ContainsKey(newPostion))
valiablelist.Add(newPostion);
}
return valiablelist;
}
//获得周围八个格子中马的数量
static int getValiableNum(Postion postion)
{
int num = 0;
foreach(var i in directionList)
{
Postion tempPostion = new Postion(postion + i);
//tempPostion在棋盘上且此位置有马,则记录
if(chessboard.ContainsKey(tempPostion))
{
if (chessboard[tempPostion] ==1)
num++;
}
}
return num;
}
//核心算法
static void find(Postion myPostion)
{
List ableList = getValiablePos(myPostion);
//条件一:拿掉当前位置的马后,周围马的周围必须至少有一个马
foreach(var i in ableList)
{
if (getValiableNum(i) - 1 <= 0)
return;
}
//条件二:拿掉当前位置的马后,自己位置周围至少有一个马
if (getValiableNum(myPostion) > 0)
{
chessboard[myPostion] = 0;//满足两个条件后,可以拿掉当前位置的马
horseHome.Remove(myPostion);
draw();
}
}
//通过chessboard画棋盘
static void draw()
{
Console.Clear();
int number=0;
String str = ““;
foreach(var i in chessboard)
{
str += i.Value;
str += “ “;
if((number+1)%M==0)
{
str += “\n“;
}
number++;
}
Console.WriteLine(str);
Thread.Sleep(200);//可以看到执行过程
}
static void Main(string[] args)
{
init();
//对棋盘上的每一个位置用find()来决定是否将当前位置的马拿下去。
List newBoard = new List(chessboard.Keys);//chessboard运行中会变动,不能用于枚举,要用newBoard作中间变量
foreach (v
相关资源
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
- C#简单窗体聊天程序
- C#指纹识别系统程序 报告
- c# 高校档案信息管理系统
- c#向word文件插入图片
- C#左侧导航菜单(动态生成)
评论
共有 条评论