第一种方法,深度搜索的递归方法
/*
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;
}

独立钻石棋(Diamond Chess)算法相关推荐

  1. 独立钻石棋游戏设计制作(C语言)

    独立钻石棋游戏设计制作(C语言) 我是编程爱好者.早期使用windows时就被扫雷游戏,钻石棋游戏,红心大战游戏迷住.相信大家也都玩过吧.在学习VB6编程时就编制过钻石棋和红心大战那样的纸牌游戏.都是 ...

  2. html5独立钻石棋,自制独立钻石棋

    在女儿的杂志上看到了独立钻石棋,以前从来没听说过,至少对我来说是非常新鲜的.女儿是对所有的新鲜事物都有浓厚的兴趣,自己就极其想尝试,我说,照着做吧. 女儿说干就干,找了一个白纸,在上面画了棋盘,但是没 ...

  3. html5独立钻石棋,独立钻石棋初级入门玩法讲解

    独立钻石棋初级入门玩法讲解 独立钻石玩法是在棋盘33孔中,每孔都放下一棋,可是取中心的一孔是空著的.玩的时分是像跳棋相同行子.一棋子依直线在平行或笔直(不能依斜线)的方向越过一棋子,而放在此棋子之后的 ...

  4. html5独立钻石棋,独立钻石跳棋问题的C++实现

    /**********************************说明***********************************/ /* 问题描述: 独立钻石跳棋问题.在下图中,33个 ...

  5. java 钻石依赖_独立钻石的解决方案-java算法

    回溯法求解独立钻石源码 独立钻石实体类: package xuzhenzhen.com; import java.util.ArrayList; public class Diamond { publ ...

  6. 求助独立钻石算法实现

    问题描述:     独立钻石跳棋问题.在下图中,33个方格顶点摆放着32枚棋子,仅中央的顶点空着未摆放棋子. 下棋的规则是任一棋子可以沿水平或成垂直方向跳过与其相邻的棋子,进入空着的顶点并吃掉被跳过的 ...

  7. 算法设计与分析: 5-14 独立钻石跳棋问题

    5-14 独立钻石跳棋问题 问题描述 独立钻石跳棋的棋盘上有 33 个方格,每个方格中可放 1 枚棋子.棋盘中最多可摆放 32枚棋子.下棋的规则是任一棋子可以沿水平或垂直方向跳过与其相邻的棋子进入空着 ...

  8. 独立钻石Solitaire算法求解

    独立钻石Solitaire算法求解 关键字 茶余饭后,休闲娱乐, 独立钻石,solitaire,DFS,剪枝. 摘要 茶余饭后,休闲娱乐. 百度略为查找,算法实现很多,但并不认为很多实现在实践中可以在 ...

  9. 独立钻石跳棋问题的C++实现

    /**********************************说明***********************************/ /* 问题描述:     独立钻石跳棋问题.在下图中 ...

最新文章

  1. Linux系统性能分析: I/O栈 优化
  2. 软件测试基础--笔记6
  3. 表达式计算 DataTable/DataRow/DataColumn Expression、JScript CodeDomProvider Eval
  4. 完美解决:"library not found for - "
  5. windows睡眠(休眠)唤醒助手_一劳永逸解决WIN10所有睡眠问题
  6. 【转】推荐10本C#编程的最佳书籍
  7. Linux expect与Shell交互
  8. JS全选功能代码优化
  9. 百度云:centos7.0+ 安装宝塔与ShopXO开源商城(从0搭建到部署上线) - 教程篇
  10. 演示数据块整理(合并)的效果
  11. Spring之ApplicationContextAware接口详解
  12. linux脚本中空格怎么显示,linux vim 显示空格
  13. 搞清字符集和字符编码
  14. android7.1修改默认休眠时间为1分钟
  15. Windows 2012 always on 读写分离集群搭建配置
  16. ORA-06512 问题解决
  17. 【codevs 1376】帕秋莉·诺蕾姬
  18. imx6ull项目也是封装成脚本安装 Poky 交叉编译工具链类似ATK-STM32MP157交叉编译
  19. Oracle体系结构VI
  20. 银行管理系统,正则,time,random,超全注释

热门文章

  1. [其它] 使用AdBlock屏蔽网页特定内容
  2. Echarts图表x轴y轴刻度值过长时显示不全(超过一千显示...k)
  3. 修筑产学研用一体化的通衢,从一场比赛背后瞭望苏州园区人才培养新范式
  4. 2600路由密码恢复
  5. Pycharm mac 语法检测失效
  6. linux 停止java服务,linux脚本启动停止java 服务
  7. stm32 m5311上传gps数据上onenet云平台实现地图定位
  8. 最新彩虹易支付六合一支付插件
  9. 探究:公司用智慧软文在网上发布宣传新闻稿水深几何
  10. 【JPG到DWG】图片直转成CAD