救急!!!!!马踏棋盘问题!!!!!!!

xiaochangzi 2003-12-21 06:24:19
今晚要交的程序,运行的时候很有问题,高手们快帮帮忙看看是怎么回事啊?很急啊,5555……
具体程序在C/C++面向对象版块
http://expert.csdn.net/Expert/topic/2586/2586744.xml?temp=.3807032
拜托了……
...全文
54 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hdqqq 2003-12-23
  • 打赏
  • 举报
回复
前面7个显示的计算进度,程序每5百万次出栈,就显示一下当前计算的进度.
xiaochangzi 2003-12-23
  • 打赏
  • 举报
回复
天哪,好长的程序啊!
可是运行的结果前面的7个答案怎么会有那么多的0出来的?这些结果应该是不正确的吧?

我只想用自己的程序交作业,而且作业要求是用回溯法,不可以用栈,我的那个程序应该怎么改啊?》

大侠帮我改改好了,不用另外写一个出来的。先谢谢了!!!
hdqqq 2003-12-21
  • 打赏
  • 举报
回复
以下代码在vc6和dev-cpp4.98编译通过
#include <iostream>
#include <stack>
using namespace std;

#ifdef _MSC_VER //使用vc编译器
typedef bool BOOL;
#endif

struct chess_step{
int stepid;
int stepx;
int stepy;
};

class chess_plan {
private:
int m_axis[8][8]; //用于棋盘
int m_curcal; //当前计算到的步数
chess_step m_first; //第一步的初试值
chess_step m_curstep; //当前的步
int m_firstfree;
stack<chess_step> m_stlstack; //使用stl堆栈
public:
inline chess_plan() : m_curcal(0) {}
inline void initial(int a,int b) {
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++)
m_axis[i][j] = 0;
}
m_curcal = 1;
m_first.stepid = 1;
m_first.stepx = a;
m_first.stepy = b;
m_curstep.stepid = 1;
m_curstep.stepx = a;
m_curstep.stepy = b;
m_axis[a][b] = 1;
}

inline BOOL calover(); //检查是否计算完成
int calfree(int); //计算第一步可能的连接
inline BOOL cantry(int,int);


inline BOOL pushstl(); //压入下一步的可能走法
inline BOOL popstl(); //弹出走法

void calcstep();

};





BOOL chess_plan::pushstl()
{
int m,n;
chess_step step;
step.stepid = 2000;

m = m_curstep.stepx + 1;
n = m_curstep.stepy + 2;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx + 2;
n = m_curstep.stepy + 1;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx + 2;
n = m_curstep.stepy - 1;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx + 1;
n = m_curstep.stepy - 2;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx - 1;
n = m_curstep.stepy - 2;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx - 2;
n = m_curstep.stepy - 1;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx - 2;
n = m_curstep.stepy + 1;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

m = m_curstep.stepx - 1;
n = m_curstep.stepy + 2;
if((m >= 0 && m <= 7) && (n >= 0 && n <= 7) && m_axis[m][n] == 0) {
if(cantry(m,n)){
step.stepid = m_curcal + 1;
step.stepx = m;
step.stepy = n;
m_stlstack.push(step);
}
}

if(step.stepid != 2000) { //压到了下一步
m_curcal ++;
m_curstep.stepid = m_curcal;
m_curstep.stepx = step.stepx;
m_curstep.stepy = step.stepy;
m_axis[step.stepx][step.stepy] = m_curcal;
return 1;
}else{ //没有压入下一步
return 0;
}
}

BOOL chess_plan::popstl()
{
chess_step pstep;
chess_step nstep;

pstep = m_stlstack.top();
m_stlstack.pop();
m_axis[pstep.stepx][pstep.stepy] = 0;
nstep = m_stlstack.top();

while(pstep.stepid != nstep.stepid) {
pstep = m_stlstack.top();
m_stlstack.pop();
m_axis[pstep.stepx][pstep.stepy] = 0;
nstep = m_stlstack.top();
}

//memcpy((void*)&m_curstep,nstep,sizeof(chess_step));
m_curstep = nstep;
m_axis[nstep.stepx][nstep.stepy] = nstep.stepid;
m_curcal = nstep.stepid ;
return 0;
}


//计算第一步可能的连接
int chess_plan::calfree(int a)
{
m_firstfree = a;
return m_firstfree;
}

BOOL chess_plan::cantry(int x,int y)
{
if(m_curcal < 63) {
if(abs((x - m_first.stepx) * (y - m_first.stepy)) == 2) {
if(m_firstfree > 1) {
m_firstfree --;
return 1;
}else{
return 0;
}
}
}
return 1;
}

BOOL chess_plan::calover()
{
//return ((m_curcal >= 64) ? 1 : 0);
if(m_curcal >= 64) {
if(abs((m_curstep.stepx - m_first.stepx) * (m_curstep.stepy - m_first.stepy)) == 2)
return 1;
}
return 0;
}

void chess_plan::calcstep()
{
int count = 0;
int i,j;
while(!calover()) {
if(!pushstl()){
popstl();
}
count ++;
if(count > 5000000) {
count = 0;
for(i=0;i<8;i++) {
for(j=0;j<8;j++) {
printf("%2d ",m_axis[7-i][j]);
}
printf("\n");
}
printf("%d\n\n",m_curcal);
}
}
for(i=0;i<8;i++) {
for(j=0;j<8;j++) {
printf("%2d ",m_axis[7-i][j]);
}
printf("\n");
}
printf("%d\n\n",m_curcal);
}

int main(int argc, char* argv[])
{

chess_plan plan;
plan.initial(0,0);
plan.calfree(2);
plan.calcstep();
return 0;
}
xiaochangzi 2003-12-21
  • 打赏
  • 举报
回复
程序用到的主要算法思想为:
回溯法,即在深度优先遍历的基础上加上限制条件的深度优先搜索;在遍历的时候,首先判断源点周围8个方向的点是不是已经走过了,如果还有没有被走过的点就同时判断当前点的八个方向是否可走,可走就继续遍历,不可走即八个方向都是已经走过了的就回溯,回到前一节点再试探另外方向;当步数为64时则表明找到了一条路径,当步数变为0的时候则表示由初始点出发不存在路径回到源点;
很急啊……SOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
xiaochangzi 2003-12-21
  • 打赏
  • 举报
回复
算了,我把源程序也粘上来好了(不过,在C/C++面向对象版块中分值是50分),哪位大侠可以帮忙改改啊?:(

include <iostream.h>

#include <iomanip.h>

// 棋子移动的八种方案

int move1[ 8 ] = { 2, 1, -1, -2, -2, -1, 1, 2 };

int move2[ 8 ] = { -1, -2, -2, -1, 1, 2, 2, 1 };

int board[ 8 ][ 8 ] = { 0 }; // 用二维数组表示棋盘,并初始化为0.

int nextX[ 8 ], nextY[ 8 ]; // 出口位置数组,记录出口的的X,Y坐标。

void main()
{
int currentX,currentY; //当前位置
int success = 1;
int originX,originY; //保存初始位置,源点
int step = 1;
int temp;
int i = 0;

cout<<endl<<"请输入马的初始位置(X和Y):"<<endl;
cin>>currentX>>currentY;

originX = currentX;
originY = currentY;
board[currentX][currentY] = step; //初始化第一步为1

do
{ if(success)
{
if(i<8)
{ //向前试探
nextX[i] = currentX + move1[i];
nextY[i] = currentY + move2[i];

if( ( nextX[i] > 7 ) || ( nextX[i] < 0 ) || ( nextY[i] > 7 ) || ( nextY[i] <0 ) )// 超出棋盘范围
{ i++;
continue;
}

//把马放置在棋盘上
if(board[nextX[i]][nextY[i]]==0)
{ currentX = nextX[i];
currentY = nextY[i];
temp = i;
step++;
board[currentX][currentY] = step;
}

else
i++;
}

else //回溯调整
line2:
{ board[currentX][currentY] = 0;
step--;
currentX = currentX - move1[temp];
currentY = currentY - move2[temp];
board[currentX][currentY] = step;
i = temp;
}
}

else
{
if(step==0)
{
cout<<endl<<"没有路径!"<<endl;
break;
}
if(step==64) //找到一条路径
{ cout<<endl<<"有一路径如下:"<<endl;

for ( i = 0; i < 8; i++ )
{ for ( int j = 0; j < 8; j++ )
cout << setw( 4 ) << board[ i ][ j ];
cout << endl;
}
break;
}

//当步数少于64但源点的8个方向已经都走过了,这种走法也肯定要回溯调整
if((board[originX+2][originY-1]&&board[originX+1][originY-2]
&&board[originX-1][originY-2]&&board[originX-2][originY-1]&&board[originX-2][originY+1]
&&board[originX-1][originY+2]&&board[originX+1][originY+2]&&board[originX+2][originY+1])
&&(step!=0)&&(step<64))
goto line2;
}

//检查走的合理性
success = (step!=0)&&(step<64)&&!(board[originX+2][originY-1]&&board[originX+1][originY-2]
&&board[originX-1][originY-2]&&board[originX-2][originY-1]&&board[originX-2][originY+1]
&&board[originX-1][originY+2]&&board[originX+1][originY+2]&&board[originX+2][originY+1]);
} while((step!=0)&&(step<64));
}

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧