当前位置: 首页 > news >正文

独立钻石棋(Diamond Chess)算法

第一种方法,深度搜索的递归方法
/*
CODE BY 我的BLOG AT 2004.10.13
独立钻石棋问题
深度搜索,递归调用法
*/

#include<iostream>
#include<fstream>
using namespace std;

#define IN1// 有棋子
#define NOTIN0// 没有棋子
#define NEVERIN 2// 不可到达位置
#define MAXN7// 棋盘大小
#define STEP31// 31步结束

int c[MAXN+1][MAXN+1];// 存储空间
int pass[4][2]={{0,-1},{-1,0},{0,1},{1,0}};// 四个方向跳过位置
int reach[4][2]={{0,-2},{-2,0},{0,2},{2,0}};// 四个方向到达位置
bool findit = false;// 记录是否找到结果

// 输出一个棋局
void output(int c[][MAXN+1]);
// 递归搜索解
void Search(int depth);

int main(void)
{
ifstream cin("cin.txt");
// 输入
for(int i=1;i<=MAXN;i++){
for(int j=1;j<=MAXN;j++){
cin>>c[i][j];
}
}
// 处理
Search(0);
// 输出
system("pause");
return 0;
}

void Search(int depth)
{
if(depth == STEP && c[4][4] == 1){// 到达目标位置
cout<<"找到解,倒着走如下:\n";
output(c);
findit = true;
return;
}
int x1,y1,x2,y2;
for(int i=1;i<=MAXN;i++){
for(int j=1;j<=MAXN;j++){
if(c[i][j]==IN){// 有棋子
for(int k=0;k<4;k++){// 4个方向
x1 = i+pass[k][0];
y1 = j+pass[k][1];
x2 = i+reach[k][0];
y2 = j+reach[k][1];
if(x1>=1&&x1<=MAXN&&y1>=1&&y2<=MAXN&&c[x1][y1]==IN//跳过位置还有子
&&x2>=1&&x2<=MAXN&&y2>=1&&y2<=MAXN&&c[x2][y2]==NOTIN){//达到为位置为空
c[i][j]=NOTIN;// 初始位置置空
c[x1][y1]=NOTIN;// 跳过位置置空
c[x2][y2]=IN;// 跳到的位置置为peg
Search(depth+1);// 跳到目标位置
c[i][j]=IN;// 还原
c[x1][y1]=IN;
c[x2][y2]=NOTIN;

if(findit == true){
output(c);
return;
}
}
}
}
}
}
}

void output(int c[][MAXN+1])
{
cout<<"*********************************************************\n";
for(int i=1;i<=MAXN;i++){
for(int j=1;j<=MAXN;j++){
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
第二种:常用方法深度搜索的open集和closed集算法,用堆是为了给启发式模板准备.
#ifndef NODE_H
#define NODE_H

#include <iostream>
using namespace std;

class Node {

friend ostream &operator <<(ostream &output,const Node & op2);// 输出当前布局

public:

int c[34];// 棋局状态
int moveid[32];// 各步移动的棋子号,第0个元素存放了到该棋局为止移动的步数
int dirction[32];// 各步的到达该局的移动方向,1,2,3,4分别为上下左右
int key;// 关键码,由于解空间不大,直接只用深度搜索

Node();

bool operator> (const Node &op2) const;// 建堆和向量需要重载的几个运算符
bool operator< (const Node &op2) const;
bool operator>=(const Node &op2) const;
bool operator==(const Node &op2) const;

void output(ostream &out) const;// 输出解

bool up(int id, Node& next);// 判断某一棋子的移动,移动后的布局存在next结点
bool down(int id, Node& next);
bool left(int id, Node& next);
bool right(int id, Node& next);

};

#endif

#include <iostream>
#include <fstream>
#include "Node.h"
using namespace std;

// 下移的位置
int downit[34][2] =
{{0,0},
{4,9},{5,10},{6,11},
{9,16},{10,17},{11,18},
{14,21},{15,22},{16,23},{17,24},{18,25},{19,26},{20,27},
{0,0},{0,0},{23,28},{24,29},{25,30},{0,0},{0,0},
{0,0},{0,0},{28,31},{29,32},{30,33},{0,0},{0,0},
{0,0},{0,0},{0,0},
{0,0},{0,0},{0,0}};

// 上移的位置
int upit[34][2] =
{{0,0},
{0,0},{0,0},{0,0},
{0,0},{0,0},{0,0},
{0,0},{0,0},{4,1},{5,2},{6,3},{0,0},{0,0},
{0,0},{0,0},{9,4},{10,5},{11,6},{0,0},{0,0},
{14,7},{15,8},{16,9},{17,10},{18,11},{19,12},{20,13},
{23,16},{24,17},{25,18},
{28,23},{29,24},{30,25}};

// 左移的位置
int leftit[34][2] =
{{0,0},
{0,0},{0,0},{2,1},
{0,0},{0,0},{5,4},
{0,0},{0,0},{8,7},{9,8},{10,9},{11,10},{12,11},
{0,0},{0,0},{15,14},{16,15},{17,16},{18,17},{19,18},
{0,0},{0,0},{22,21},{23,22},{24,23},{25,24},{26,25},
{0,0},{0,0},{29,28},
{0,0},{0,0},{32,31}};

// 右移的位置
int rightit[34][2] =
{{0,0},
{2,3},{0,0},{0,0},
{5,6},{0,0},{0,0},
{8,9},{9,10},{10,11},{11,12},{12,13},{0,0},{0,0},
{15,16},{16,17},{17,18},{18,19},{19,20},{0,0},{0,0},
{22,23},{23,24},{24,25},{25,26},{26,27},{0,0},{0,0},
{29,30},{0,0},{0,0},
{32,33},{0,0},{0,0}};

//down 1,up 2,left 3,right 4;
bool Node::down(int id,Node& next)
{
if(downit[id][0] == 0){// 由于移动后出界不能移
return false;
}else{
if(c[downit[id][0]] == 1 && c[downit[id][1]] == 0){
next = *this;// 拷贝当前
next.key ++;

// 移动后的棋局
next.c[id] = 0;
next.c[downit[id][0]] = 0;
next.c[downit[id][1]] = 1;

// 记录移动到达的步数
next.moveid[0] ++;
next.moveid[next.moveid[0]] = id ;
next.dirction[next.moveid[0]] = 1;

return true;
}else{// 由于移动后位置不允许不能移
return false;
}
}
}

bool Node::up(int id,Node& next)
{
if(upit[id][0] == 0){// 由于移动后出界不能移
return false;
}else{
if(c[upit[id][0]] == 1 && c[upit[id][1]] == 0){
next = *this;// 拷贝当前
next.key ++;

// 移动后的棋局
next.c[id] = 0;
next.c[upit[id][0]] = 0;
next.c[upit[id][1]] = 1;

// 记录移动到达的步数
next.moveid[0] ++;
next.moveid[next.moveid[0]] = id ;
next.dirction[next.moveid[0]] = 2;
return true;
}else{// 由于移动后位置不允许不能移
return false;
}
}
}

bool Node::left(int id,Node& next)
{
if(leftit[id][0] == 0){// 由于移动后出界不能移
return false;
}else{
if(c[leftit[id][0]] == 1 && c[leftit[id][1]] == 0){
next = *this;// 拷贝
next.key ++;

// 移动后的棋局
next.c[id] = 0;
next.c[leftit[id][0]] = 0;
next.c[leftit[id][1]] = 1;

// 记录移动到达的步数
next.moveid[0] ++;
next.moveid[next.moveid[0]] = id ;
next.dirction[next.moveid[0]] = 3;
return true;
}else{// 由于移动后位置不允许不能移
return false;
}
}
}

bool Node::right(int id,Node& next)
{
if(rightit[id][0] == 0){
return false;
}else{
if(c[rightit[id][0]] == 1 && c[rightit[id][1]] == 0){
next = *this;// 拷贝
next.key ++;

// 移动后的棋局
next.c[id] = 0;
next.c[rightit[id][0]] = 0;
next.c[rightit[id][1]] = 1;

// 记录移动到达的步数
next.moveid[0] ++;
next.moveid[next.moveid[0]] = id ;
next.dirction[next.moveid[0]] = 4;
return true;
}else{
return false;
}
}
}

Node::Node()
{
memset(c,0,sizeof(c));
moveid[0] = 0;
}

bool Node::operator> (const Node &op2) const
{
if(key>op2.key)
return
true;
else
return
false;
}
bool Node::operator< (const Node &op2) const
{
if(key<op2.key)
return
true;
else
return
false;
}
bool Node::operator>=(const Node &op2) const
{
if(key>=op2.key)
return
true ;
else
return
false;
}
bool Node::operator==(const Node &op2) const
{
for(int i=1;i<=33;i++){
if(c[i]!=op2.c[i])
return false;
}
return true;
}
void Node::output(ostream &out) const
{
out<<"\n--------------------------各步走法如下-------------------\n";
Node tmp = *this;
for(int i=1;i<=33;i++)
tmp.c[i] = 1;
tmp.c[17] = 0;
out<<tmp;
for(int i=1;i<=31;i++){
if(tmp.dirction[i] == 1){
tmp.c[tmp.moveid[i]] = 0;
tmp.c[downit[tmp.moveid[i]][0]] = 0;
tmp.c[downit[tmp.moveid[i]][1]] = 1;
out<<"移动棋子:"<<tmp.moveid[i]<<"号,移动方向:向下.\n";
}else if(tmp.dirction[i] == 2){
tmp.c[tmp.moveid[i]] = 0;
tmp.c[upit[tmp.moveid[i]][0]] = 0;
tmp.c[upit[tmp.moveid[i]][1]] = 1;
out<<"移动棋子:"<<tmp.moveid[i]<<"号,移动方向:向上.\n";
}else if(tmp.dirction[i] == 3){
tmp.c[tmp.moveid[i]] = 0;
tmp.c[leftit[tmp.moveid[i]][0]] = 0;
tmp.c[leftit[tmp.moveid[i]][1]] = 1;
out<<"移动棋子:"<<tmp.moveid[i]<<"号,移动方向:向左.\n";
}else if(tmp.dirction[i] == 4){
tmp.c[tmp.moveid[i]] = 0;
tmp.c[rightit[tmp.moveid[i]][0]] = 0;
tmp.c[rightit[tmp.moveid[i]][1]] = 1;
out<<"移动棋子:"<<tmp.moveid[i]<<"号,移动方向:向右.\n";
}
out<<tmp;
}
out<<"\n----------------------移动完成-----------------------\n";
}

ostream &operator <<(ostream &output,const Node &op2)
{
output<<" "<<op2.c[1]<<" "<<op2.c[2]<<" "<<op2.c[3]<<" \n";
output<<" "<<op2.c[4]<<" "<<op2.c[5]<<" "<<op2.c[6]<<" \n";
for(int i=1;i<=3;i++){
for(int j=7*i;j<7*(i+1);j++){
output<<op2.c[j]<<" ";
}
output<<"\n";
}
output<<" "<<op2.c[28]<<" "<<op2.c[29]<<" "<<op2.c[30]<<" \n";
output<<" "<<op2.c[31]<<" "<<op2.c[32]<<" "<<op2.c[33]<<" \n";
return output;
}
/*
CODY BY 01120136 我的BLOG AT 2004.10.9
独立钻石棋问题, 深度优先搜索,尽量清除重复点
棋盘编号布局如下:
01 02 03
04 05 06
07 08 09 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
31 32 33
*/

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include "Node.h"
using namespace std;

vector<Node> open;// OPEN集
vector<Node> closed;// CLOSED集
vector<Node>::iterator iter;

// 判断next结点是否已扩展过,是返回true,否则false
bool extended(Node &next);
// 搜索解
void search();

int main(void)
{

search();
system("pause");
return 0;
}

void search()
{
// 初始化
Node first;
for(int i=1;i<=33;i++){
first.c[i] = 1;
}
first.c[17] = 0;
first.key = 0;
open.push_back(first);

// 开始循环
while(!open.empty()){
cout<<"正在搜索...... 已扩展结点:"<<closed.size()<<"\n";
make_heap(open.begin(),open.end());// 建最大堆
pop_heap(open.begin(),open.end());// 取走堆首元素,可为启发式搜索准备
Node tmp = open.back();
open.pop_back();

// 看所取节点是否为目标节点
if(tmp.moveid[0] >= 31 && tmp.c[17] == 1){
tmp.output(cout);// 输出解并返回
ofstream out("out.txt");
tmp.output(out);
return ;
}

// 若不是则生成所有的子状态
Node next;
bool hadit;
for(int i = 1;i <= 33 ;i++){
if(tmp.c[i] != 0){
if( tmp.down(i,next)){
if(extended(next) == false){// 还没扩展过
open.push_back(next);
}
}
if( tmp.up(i,next)){
if(extended(next) == false){// 还没扩展过
open.push_back(next);
}
}
if( tmp.left(i,next)){
if(extended(next) == false){// 还没扩展过
open.push_back(next);
}
}
if( tmp.right(i,next)){
if(extended(next) == false){// 还没扩展过
open.push_back(next);
}
}
}
}
// 将刚才取出的open点放入closed表
closed.push_back(tmp);
}
}

bool extended(Node &next)
{
// 若next在closed中,即已扩展过了,返回true
for(iter = closed.begin();iter !=closed.end();iter++){
if(*iter == next){
return true;
}
}
// 若已在open集中,则返回true;
for(iter = open.begin();iter !=open.end();iter++){
if(*iter == next){
return true;
}
}
// 都没有则返回false
return false;
}

相关文章:

  • python3 生成二维码_windows使用python3.4生成二维码
  • 刚刚随便GOOGLE和BAIDU了下PIPO和BLOG
  • python 多进程全局变量_浅谈Python 多进程默认不能共享全局变量的问题
  • 嗯,毕业之前还要整理的几件事
  • pb mdi窗口多sheet_趣头条百 PB 规模 Hadoop 实践
  • 26个日文片假名导致Access搜索(80040e14/内存溢出)的解决办法
  • pep8 python 编码规范_读懂PEP8,让你的Python代码更加优雅
  • 关于数据库查询次数的问题
  • python 服务器_我的Python_WEB服务器简单配置
  • 7 安装_python3.7安装教程
  • GMail Drive shell --Windows上的GmailFS
  • qt 自定义控件_Qt编写自定义控件47-面板区域控件
  • 成龙《新警察故事》观后
  • springcloud 消息队列_Apache RocketMQ 消息队列部署与可视化界面安装
  • 字符集问题的初步探讨(六)-----乱码的产生
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • angular2开源库收集
  • canvas 绘制双线技巧
  • const let
  • golang中接口赋值与方法集
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • mysql外键的使用
  • mysql中InnoDB引擎中页的概念
  • Otto开发初探——微服务依赖管理新利器
  • React16时代,该用什么姿势写 React ?
  • SOFAMosn配置模型
  • Xmanager 远程桌面 CentOS 7
  • 记录:CentOS7.2配置LNMP环境记录
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • PostgreSQL之连接数修改
  • #if 1...#endif
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (3)llvm ir转换过程
  • (C#)一个最简单的链表类
  • (C语言)球球大作战
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • (转)可以带来幸福的一本书
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .NET Core 和 .NET Framework 中的 MEF2
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .net快速开发框架源码分享
  • .Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)
  • .NET中的十进制浮点类型,徐汇区网站设计
  • @autowired注解作用_Spring Boot进阶教程——注解大全(建议收藏!)
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...
  • @Documented注解的作用
  • [ vulhub漏洞复现篇 ] Celery <4.0 Redis未授权访问+Pickle反序列化利用
  • [android学习笔记]学习jni编程
  • [C++]18:set和map的使用
  • [Electron]ipcMain.on和ipcMain.handle的区别