编用回溯法解决跳棋销子的程序http://home.jlu.edu.cn/~z/Algorithms/JumpChess_1.rar
z4b 2003-05-15 12:03:11 跳棋
1 1 1
1 1 1
1 1 1 1 1 1 1
1 1 1 0 1 1 1
1 1 1 1 1 1 1
1 1 1
1 1 1
最后把盘面变成 中心为 1 其余为0
我的实现想法是:
设计32个栈组,每个组两个栈,一个 from记录 从那里开始跳,一个to跳倒那里
pathfrom[0]pathto[0] 记录最后的得到的移动方案。
pathfrom[1]pathto[1] 初始可以移动的方案(4个)弹最上到pathfrom[0]pathto[0]
pathfrom[2]pathto[2]记录当pathfrom[0]pathto[0]最上的移动方案对应当前可移动的方案。
依次类推。。。。
当某个pathfrom[n]pathto[n]为空 就是找不到可以移动的棋子,回溯上一层 删除最上的移动方法。用次上的继续找。。。
信箱:z4b@163.com
qq: 13628198
全文件:
http://home.jlu.edu.cn/~z/Algorithms/JumpChess_1.rar
那位老大能帮我调试出结果 谢谢。
主程序如下:
#include <iostream.h>
#include "stack.h"
#include "position.h"
#include "make2db.h"
// globals
int **maze, m,count;
Stack<Position> *pathfrom[31],*pathto[31];
// pointer to stack
bool InputMaze()
{// Input the maze.
cout << "Enter maze size" << endl;
cin >> m;
Make2DArray(maze, 10, 10);
cout << "Enter maze in row major order" << endl;
for (int i=2; i<=8; i++)
for (int j=2; j<=8; j++) cin >> maze[i][j];
return true;
}
bool ExistPoint(int nn)
{ Position herefrom,hereto;
nn=0;
for (int i=2; i<=8; i++)
for (int j=2; j<=8; j++)
for (int option =0; option<=3; option++)
{
if(maze[i][j] == 1
||maze[i+option/2][j+option/2] == 1
||maze[i+option][j+option] == 0)
{
nn++;
pathfrom[count]->Add(herefrom);
pathto[count]->Add(hereto);
}
}
return nn;
}
bool CanMovePoint()
{
int floorn = 0;
if( ExistPoint(floorn))
return true;
else
return false;
}
bool FindPath()
{// Find a path from (1,1) to the exit (m,m).
// Return true if successful, false if impossible.
// Throw NoMem exception if inadequate space.
pathfrom[0] = new Stack<Position>(31);
pathto[0] = new Stack<Position>(31);
for (int ii=1; ii<=30;ii++){
pathfrom[ii] = new Stack<Position>(10);
pathto[ii] = new Stack<Position>(10);
}
count = 0;
// initialize offsets
Position offset[4];
offset[0].row = 0; offset[0].col = 2; // right
offset[1].row = 2; offset[1].col = 0; // down
offset[2].row = 0; offset[2].col = -2; // left
offset[3].row = -2; offset[3].col = 0; // up
// initialize wall of obstacles around maze
for (int i = 0; i <= 10; i++) {
maze[0][i] = maze[10][i] = 2; // bottom and top
maze[i][0] = maze[i][10] = 2; // left and right
}
for (int j = 1; j <= 9; j++) {
maze[1][j] = maze[9][j] = 2; // bottom and top
maze[j][1] = maze[j][9] = 2; // left and right
}
Position herefrom,hereto;
herefrom.row = 2; herefrom.col = 4;
hereto.row = 4; hereto.col = 4;
// maze[1][1] = 1; // prevent return to entrance
int option = 0; // next move
int LastOption = 3;
// search for a path
while (count = 31) {// not exit
// find two point to move to
int fromr, fromc,tor,toc;
while (CanMovePoint()) {
fromr = herefrom.row + offset[option].row;
fromc = herefrom.col + offset[option].col;
tor = hereto.row + offset[option].row;
toc = hereto.col + offset[option].col;
// if (maze[r][c] == 0) break;
option++; // next option
}
// was a neighbor found?
if (CanMovePoint()) {// move to maze[r][c]
++count;
pathfrom[count]->Top();
pathto[count]->Top();
pathfrom[0]->Add(herefrom);
pathto[0]->Add(hereto);
herefrom.row = fromr; herefrom.col = fromc;
hereto.row = tor; hereto.col = toc;
// set to 1 to prevent revisit
maze[fromr][fromc] = 0;
maze[(fromr+tor)/2][(fromc+toc)/2] = 0;
maze[tor][toc] = 1;
option = 0;
}
else {// no neighbor to move to, back up
//if (pathto[n]->IsEmpty()) return false;
Position nextfrom,nextto;
pathfrom[count-1]->Delete(nextfrom);
pathto[count-1]->Delete(nextto);
if (nextfrom.row == herefrom.row)
option = 2 + nextfrom.col - herefrom.col;
else option = 3 + nextfrom.row - herefrom.row;
// from = ;
}
}
return true; // at exit
}
void OutputPath()
{// Output path to exit.
cout << "The path is" << endl;
Position herefrom,hereto;
while (!pathfrom[0]->IsEmpty()) {
pathfrom[count]->Delete(herefrom);
pathto[count]->Delete(hereto);
cout << herefrom.row<<' '<< herefrom.col<< '->' <<hereto.row<<' '<<hereto.col<< endl;}
}
void main(void)
{
// welcome();
InputMaze();
if (FindPath()) OutputPath();
else cout << "No path" << endl;
}
0*004014ba 指令引用0*fdfdfd 内存 该内存不能为 written
怎么回师?
我一执行一个程序 酒出现哪个错误 怎么回师?/