关于 马走棋盘 (骑士遍历)的深度优先算法
2008-08-25 14:03
162 查看
问题:
在 8 x 8 方格的棋盘上,从任意指定的方格出发,为马寻求一条走遍棋盘每一格并只经过一次的一条路径。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
提示:设马在某个方格中,可以在一步到达的不同位置最多有8个如下图。
| 4 | | 3 | |
5 | | | | 2 |
| | 马 | | |
6 | | | | 1 |
| 7 | | 0 | |
================================================================================================
horse.h文件:
#include <stdio.h>
#include <stdlib.h>
#define MAX_BOARD 8
#define MIN_BOARD 0
/************************************************************************/
/* Every Horse Node */
/************************************************************************/
typedef struct
{
int x; // X position
int y; // Y position
int chirldNumber; // node's child node
int node[MAX_BOARD][MAX_BOARD]; // All checker Board
}horse_node;
// initial all the node
void initial_node(horse_node *hn);
// get may out ways of the node
int get_node_may_out_way(horse_node *hn,int x,int y);
// sort for the nodes
void sort_node(horse_node *hn,int n);
// output the solution to the Control application
void output_solution(horse_node *hn);
// Depth first Search all the node
void search_node_dfs(horse_node *hn,int x,int y,int count);
=====================================================================================
.c文件:
#include "horse.h"
// get may out ways of the node
int get_node_may_out_way(horse_node *hn,int x,int y)
{
int i = 0;
int count=0;
int tmpx = 0;
int tmpy = 0;
int dx[8] = {-2,-2,-1,-1,1,1,2,2}; // X-position moved data
int dy[8] = {-1,1,-2,2,-2,2,-1,1}; // Y-position moved data
// check the node
if(x < MIN_BOARD || y < MIN_BOARD || x >= MAX_BOARD ||
y >= MAX_BOARD || hn->node[x][y] > 0)
{
return -1;// have walked or not a legal node
}
// get the out way of the node
for(i;i < 8;++i)
{
tmpx = x + dx[i]; // X-position moved data
tmpy = y + dy[i]; // Y-position moved data
// check the node while is a legal node
if(tmpx < MIN_BOARD || tmpy < MIN_BOARD ||
tmpx >= MAX_BOARD || tmpy >= MAX_BOARD)
{
continue;
}
else if(hn->node[tmpx][tmpy] == 0)
{
++count;
}
}
return count;
}
// sort for all the nodes
void sort_node(horse_node *hn,int number)
{
int i;
int j;
int tmp;
horse_node temp;
for(i = 0;i < number;++i)
{
tmp = i;
for(j = i + 1; j < number; ++j)
{
if(hn[j].chirldNumber < hn[tmp].chirldNumber)
{
tmp = j;
}
}
if(tmp > i)
{
temp = hn[i];
hn[i] = hn[tmp];
hn[tmp] = temp;
}
}
}
// output the solution to the Control application
void output_solution(horse_node *hn)
{
int i,j;
// horse_node *hn;
printf("/n *********** Start walk **********/n");
for(i = 0;i < 8;i++)
{
for(j = 0;j < 8;j++)
{
printf("%4d",hn->node[j][i]);
}
printf("/n");
}
printf("*********** End **********/n/n");
}
// Depth first Search all the node
void search_node_dfs(horse_node *hn,int x,int y,int count)
{
int i = 0;
int j = 0;
int dx[8] = {-2,-2,-1,-1,1,1,2,2}; // X-position moved data
int dy[8] = {-1,1,-2,2,-2,2,-1,1}; // Y-position moved data
horse_node hnode[8];
for(i;i < 8; ++i)//get the node's child node ( out ways)
{
hnode[i].x = x + dx[i];
hnode[i].y = y + dy[i];
hnode[i].chirldNumber = get_node_may_out_way(hn,hnode[i].x,hnode[i].y);
}
// Sort the node by Sorts simply
sort_node(hnode,8);
for(i = 0;hnode[i].chirldNumber < 0; ++i); //except the legal node
{
for(j = i;j < 8; ++j)
{
hn->node[hnode[j].x][hnode[j].y] = count;
search_node_dfs(hn,hnode[j].x,hnode[j].y,count+1);
}
}
}
// initial all the node
void initial_node(horse_node *hn)
{
int i = 0;
int j = 0;
for(i;i < MAX_BOARD;++i)
{
for(j = 0; j < MAX_BOARD;++j)
{
hn->node[i][j] = 0;
}
}
}
// The main Function
int main(void)
{
int x = 0;
int y = 0;
int cnt = 1;
horse_node hn;
printf(" /n");
for(x;x < MAX_BOARD;x++)
{
for(y = 0;y <MAX_BOARD;y++)
{
printf("horse walk count:%d/n",cnt++);
printf("X- Y position is (%d,%d)/n ",x,y);
initial_node(&hn);
// Set the first node
hn.node[x][y] = 1;
// Begin to search
search_node_dfs(&hn,x,y, 2);
// output the last solution
output_solution(&hn);
}
}
return 1;
}
相关文章推荐
- POJ_2488——骑士遍历棋盘,字典序走法
- codevs1219 骑士遍历(棋盘DP)
- codevs1219 骑士遍历(棋盘DP)
- java关于迭代器的分析...设计更通用的容器遍历方法
- hdu 2067 小兔的棋盘【关于Catalan数的一点学习】
- 关于java中HashMap遍历效率问题
- C语言-数据结构-骑士周游-马踏棋盘问题-源代码
- 关于ThinkPHP遍历后jquery失效的问题
- 递归算法之马遍历棋盘问题
- MYSQL游标(CURSOR)关于NOT FOUND或02000结束状态只遍历一次的问题
- 关于javascript遍历对象
- 关于hadoop reduce阶段遍历Iterable的注意事项
- 转:数据结构与算法1:马踏棋盘问题(骑士周游问题)
- 关于js遍历表格的实例
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
- 关于jquery遍历已选中的checkbox事件,获取其值,批量进行删除
- 关于javsacript中 for in遍历对象
- 算法训练-骑士走棋盘
- 关于hashMap遍历方法,用Iterator对hashMap进行遍历