您的位置:首页 > 编程语言 > PHP开发

关于 马走棋盘 (骑士遍历)的深度优先算法

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;
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 search output struct