gpt4 book ai didi

c++ - 使用回溯的骑士​​之旅

转载 作者:行者123 更新时间:2023-11-28 03:02:44 25 4
gpt4 key购买 nike

我正在使用回溯在 C++ 中开发一个骑士之旅程序这是我的代码:

#include <iostream>
#include <iomanip>
using namespace std;

template <class T>
class Stack
{
public:
Stack(int len = 100)
{
length = len;
sp = -1;
a = new T [length];
}

int getLength()
{
return length;
}

void push(T v)
{
if(isFull())
{
throw std::exception("Stack Overflow!");
return;
}
a[++sp] = v;
}
bool isFull()
{
if(sp < getLength()-1)
return false;
return true;
}
T pop()
{
if(isEmpty())
{
throw std::exception("Stack Underflow!");
}
return a[sp--];
}
bool isEmpty()
{
if(sp == -1)
return true;
return false;
}
T getTop()
{
if(isEmpty())
{
throw std::exception("Stack Underflow!");
}
return a[sp];
}

private:
T * a;
int sp;
int length;

};

class MoveStk
{
public:
MoveStk()
{
set(0,0,0);
}

MoveStk(int r , int c , int d)
{
set(r,c,d);
}

void set(int r , int c , int d)
{
row = r;
col = c;
dir = d;
}

void get(int& r , int & c , int & d)
{
r = row;
c = col;
d = dir;
}

private:
int row , col , dir;
};

class Position
{
public:
int x , y;
Position(int i = 0 , int j = 0)
{
x = i;
y = j;
}
};


bool move(Position pos[],int posLen,int ** chess ,int n, int col, int row , int&newRow , int & newCol , int moveNo )
{
if (row+pos[moveNo].x>=0 && row+pos[moveNo].x<n && col+pos[moveNo].y>=0 && col+pos[moveNo].y<n && chess[row+pos[moveNo].x][col+pos[moveNo].y] == 0 )
{
newRow = row+pos[moveNo].x;
newCol = col+pos[moveNo].y;
return true;
}
return false;
}

int main()
{

int n = 8;

//create stack
Stack<MoveStk> myStack(100);



//create dynamic array
int ** chess = new int* [n];
for (int i = 0; i < n; i++)
chess[i] = new int[n];

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
chess[i][j] = 0;

//setup moves
Position pos[8];

pos[0] = Position(-2,1);
pos[1] = Position(-2,-1);
pos[2] = Position(2,1);
pos[3] = Position(2,-1);
pos[4] = Position(-1,-2);
pos[5] = Position(-1,2);
pos[6] = Position(1,2);
pos[7] = Position(1,-2);


//input for start (must be optional) :
int row = 4;
int col = 3;


int count = 1 , moveChoose = 0;
int newCol , newRow;

MoveStk temp(row,col,0);//adding first move to stack
myStack.push(temp);

chess[row][col] = count;

while(count<=(n*n) && !myStack.isEmpty())
{
temp = myStack.getTop();
temp.set(row,col,moveChoose);

while(moveChoose<8 && !move(pos,8,chess,n,col,row,newRow,newCol,moveChoose))//avalin khoone azad baray karkat
{
moveChoose++;
}

if(moveChoose != 8)//there is somewhere we can go
{
myStack.pop();
temp.set(col,row,moveChoose+1);//adding next possible move for returning back
myStack.push(temp);
chess[newRow][newCol] = ++count;
temp.set(newRow,newCol,0);
myStack.push(temp);
}
else//we must return
{
myStack.pop();
chess[newRow][newCol] = 0;
count--;
}

}

printArray(chess);

cin.ignore();
cin.get();
}

问题是,这个应用程序只适用于第一步,我的堆栈在游戏结束前就变空了。我找不到哪里错了?!

可能是什么问题?

最佳答案

首先,我认为您需要更改行:

while(count<=(n*n) && !myStack.isEmpty())
{
temp = myStack.getTop();
temp.set(row,col,moveChoose);

收件人:

while(count<=(n*n) && !myStack.isEmpty())
{
temp = myStack.getTop();
temp.get(row,col,moveChoose);

按照目前的情况,每次循环都覆盖了最后一步和位置。你应该做的是从你离开的地方开始,尝试从以前的位置开始下一步。

此外,我注意到您稍后对 set 调用的参数有误:

        temp.set(col,row,moveChoose+1);//adding next possible move for returning back

应该是row然后是col

关于c++ - 使用回溯的骑士​​之旅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20351928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com