| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1876 人关注过本帖
标题:[求助]求马踏棋盘问题代码!
只看楼主 加入收藏
浪子逍遥
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-6-4
收藏
 问题点数:0 回复次数:5 
[求助]求马踏棋盘问题代码!

  求助各位编程高手.
  我现在急需马踏棋盘问题可视化编程的代码,编程软件:VC++;在windows下运行而不是在DOS下运行,用栈和非递归方法实现的!
  急急急啊!希望有好心的人帮帮忙!急用!我现在这里谢谢了啊!~~~

[此贴子已经被作者于2006-6-4 14:05:40编辑过]

搜索更多相关主题的帖子: 棋盘 代码 
2006-06-04 14:04
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
对阿,回溯法,栈是用来保存马走过的地方的

http://kongfuziandlife. http://codeanddesign.
2006-06-04 15:53
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
什么叫马踏棋盘问题?给出具体描述吧。

http://myajax95./
2006-06-05 01:24
sanpedro
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-6-14
收藏
得分:0 

/* ------------------------------------------------------ */
/* PROGRAM knight tour : */
/* Given a n*n chess board and a starting position, */
/* this program will find a knight tour path passing */
/* through each square on the chess board exactly once by */
/* using backtrack technique without recursion. For */
/* larger n, for example n > 6, it will take very long */
/* time to find such path. Therefore be patience please. */
/* */
/* ------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10 /* max. board size */
#define MAX_STACK 100 /* stack size = board-size^2*/
#define SUCCESS 1 /* return value for a succ. */
#define FAILURE 0 /* for a failure. */
#define EMPTY -1 /* value to indicate empty */

/* ----------------- external variables ----------------- */

int board[MAXSIZE+1][MAXSIZE+1]; /* chess board */
int n; /* working board size */
int offset_x[] = { 2, 1, -1, -2, -2, -1, 1, 2};
int offset_y[] = { 1, 2, 2, 1, -1, -2, -2, -1};

int path_x[MAX_STACK+1]; /* stack for x coordinate */
int path_y[MAX_STACK+1]; /* stack for y coordinate */
int direction[MAX_STACK+1]; /* stack for direction */
int top; /* stack pointer */

/* ----------------- function prototype ----------------- */

void initial(void);
void display(void);
int try(int, int);

/* -------------------- main program -------------------- */

void main(void)
{
int row, column;
char line[100];

printf("\nRecursive Knight Tour Problem");
printf("\n=============================");
printf("\n\nBoard Size ----> ");
gets(line);
n = atoi(line);
printf( "Start Row -----> ");
gets(line);
row = atoi(line);
printf( "Start Column --> ");
gets(line);
column = atoi(line);

initial();
if (try(row, column) == FAILURE)
printf("\nNO SOLUTION AT ALL.");
else
display();
}


/* ------------------------------------------------------ */
/* FUNCTION initial : */
/* initialize the chess board to EMPTY. */
/* ------------------------------------------------------ */

void initial(void)
{
int i, j;

for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
board[i][j] = EMPTY;
}


/* ------------------------------------------------------ */
/* FUNCTION display : */
/* display to chess board. */
/* ------------------------------------------------------ */

#define DRAWGRID(N) { int i; \
printf("\n+"); \
for (i = 1; i <= N; i++) \
printf("--+"); \
}


#define DRAWLINE(N, r) { int i; \
printf("\n|"); \
for (i = 1; i <= N; i++) \
printf("%2d|",board[r][i]);\
}


void display(void)
{
int r;

printf("\n\nHere is One Possible Solution :\n");
DRAWGRID(n);
for (r = 1; r <= n; r++) {
DRAWLINE(n,r);
DRAWGRID(n);
}
}


/* ------------------------------------------------------ */
/* FUNCTION try : */
/* The main non-recursive backtrack working routine. */
/* ------------------------------------------------------ */

#define YES 1
#define NO 0
#define BOARD(x,y) (1<=x) && (x<=n) && (1<=y) && (y<=n)
#define CHECK(x,y) board[x][y] == EMPTY
#define PUSH(x,y) { top++; \
path_x[top] = x; path_y[top] = y; \
direction[top] = 0; \
board[x][y] = top; \
}

int try(int x, int y)
{
int new_x, new_y;
int found;

top = -1; /* initial to empty */
PUSH(x, y); /* push first pos. and dir. */
while (top < n*n-1) { /* loop until the board full*/
found = NO;
while (direction[top] < 8) { /* try all 8 pos. */
new_x = path_x[top] + offset_x[direction[top]];
new_y = path_y[top] + offset_y[direction[top]];
if (BOARD(new_x,new_y) && CHECK(new_x,new_y)) {
PUSH(new_x, new_y); /* a new pos. PUSH*/
found = YES; /* set flag */
break; /* try next pos. */
}
else
direction[top]++; /* OR try next dir*/
}
if (!found) /* if no new pos. is found */
if (top > 0) { /* do we have prev. item? */
board[path_x[top]][path_y[top]] = EMPTY;
direction[--top]++; /* YES, backtrack */
}
else
return FAILURE; /* otherwise, FAILURE */
}
return SUCCESS; /* all pos. visited. DONE */
}



2006-06-14 19:09
sanpedro
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-6-14
收藏
得分:0 

马踏棋盘,又称武士巡游
要求knight踏遍国际象棋棋盘,且同一格上不准停留两次
挺经典的问题,就是太累了
各位大哥大姐大叔大婶有好算法都来往上贴啊!!!!!!!!!


2006-06-14 19:15
剑随风
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-4-12
收藏
得分:0 
最近也被这个算法难住了,希望多借鉴下别人的高见,有好的算法多让小弟借鉴呀~~~

2007-11-27 18:48
快速回复:[求助]求马踏棋盘问题代码!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020086 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved