gpt4 book ai didi

c++ - Knight's Tour C++ 使用堆栈

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:54:37 25 4
gpt4 key购买 nike

我目前正在使用 C++ 开发 Knight tour Chessboard 游戏,使用 Stack 来存储我的移动。我遇到了一个奇怪的循环,它没有结束程序。有人可以帮我处理代码吗?

#include <iostream>
#include <stack>
#include <map>
#include <cstdlib>
using namespace std;

struct whereIam
{
int row, col;
};

struct L_shape_pattern
{
int Lrow[8]={1,1,2,2,-1,-1,-2,-2};
int Lcol[8]={2,-2,1,-1,2,-2,1,-1};


};
bool check_if_valid(int row, int col)
{
if ((row >= 0 && col >= 0) && (row < 8 && col < 8))
{
// cout << "here in valid " <<endl;
return true ;
}

else
return false;

}

bool check_empty(bool board[8][8], whereIam position)
{
// if (board[position.row][position.col] == false)
// return false;
// else
// return true;
if (board[position.row][position.col] == true)
{
// cout << "here in check empty" <<endl;
return true;
}

else
return false;

}
bool isReady(whereIam &position,bool board[8][8])
{
// cout << "here" << endl;
int ready = 0;
for (int i = 0 ; i < 8 ; i ++)
{
for (int j = 0 ; j < 8 ; j++)
{
if(board[i][j] == false)
{
ready += 1;
}

}
}
cout << "ready: " <<ready << endl;
if (ready == 64)
{
cout << "done" << endl;
return true;
}
else
return false;
}
void findspot(whereIam &position,bool board[8][8], stack<whereIam> &sequence)
{
L_shape_pattern Lshape;

// stack<whereIam> initial;
stack<int> counter;


for (int j = 0 ; j< 9 ;j++)
{
//nothing is assign here
if (check_if_valid(position.row+Lshape.Lrow[j],position.col+Lshape.Lcol[j]) /*&& check_empty(board,position)*/)
{
// cout << "here in valid in spot " <<endl;
whereIam hello;
hello.row = position.row+Lshape.Lrow[j];

hello.col = position.col+Lshape.Lcol[j];
// cout << hello.row << " " << hello.col << endl;
if (check_empty(board,hello))
{
// cout << "here in empty" <<endl;
// int possible_row = position.row+Lshape.Lrow[j];
// int possible_col = position.col+Lshape.Lcol[j];
// position.row = possible_row;
// position.col = possible_col;
position.row = hello.row;
position.col = hello.col;

sequence.push(position);
// initial.push(position);
// cout << position.row << " " << position.col << endl;
counter.push(j);
board[position.row][position.col] = false;
j = -1;
if (isReady(position,board) == true)
{
cout << "in if ready" << endl;
exit(0);
}




}



}
if (j == 8 )
{
// cout << "here in j = 8" <<endl;
board[position.row][position.col] = true;
// cout << " pop board " << position.row <<" " << position.col << endl;
sequence.pop();
position = sequence.top();
// increment to the position where it need to be backtracking and it increment by one
j = counter.top();
counter.pop();
if (isReady(position,board) == true)
{
cout << "in if ready" << endl;
exit(0);
}

}




}



}
//bool movetheKnight(whereIam &position,bool board[8][8], stack<whereIam> &sequence)
//{

//}
void open_all_spot( bool board[8][8])
{

for (int i = 0 ; i< 8 ; i++)
for (int j= 0 ; j <8 ; j++)
{
board[i][j] = true;
}
}
int main()
{
bool board[8][8];
open_all_spot(board);


whereIam position;
stack<whereIam> sequence;
cout << "Enter the initial position" << endl;
cout << "row : " ;
cin >> position.row;
cout << "column:";
cin >> position.col;
sequence.push(position);
//assign the initial position to be occupied already
board[position.row][position.col] = false;

findspot(position,board,sequence);
cout << "here end all" << endl;

return 0;
}

我刚刚创建的某些部分用于调试并查看每个函数的工作原理,因此请忽略这些部分。循环总是在继续,似乎永远不会结束。我试图跟踪堆栈中的数据,但对我来说这似乎是合理的。

如有任何帮助,我们将不胜感激。

最佳答案

查看这部分代码时:

for ( int j = 0; j < 9; j++ ) {
if ( check_if_valid(position.row+Lshape.Lrow[j],position.col+Lshape.Lcol[j]) /*&& check_empty(board,position)*/) {
// code...
if ( checkEmpty( board, hello ) {
// code...
j = -1;
if ( isReady(position, board) == true ) { // == true not needed
// code...
}
}
}
if ( j == 8 ) {
// code...
j = counter.top()
// code...
if ( isReady(position, board) == true ) { // == true not needed
// code...
}
}
} // for loop

想想当条件返回 true 时,for 循环中的第 1st 嵌套 if 语句会发生什么。您正在将 j 更改为 -1

再次在 for 循环中的第 2 个nd if 语句中 if j==8 您再次将 j 更改为 counter.top().

这种行为会导致for循环的无限递归。这是一个简单的例子:

#include <iostream>
#include <iomanip>

int main() {

int counter = 0;
for ( int i = 0; i < 5; i++ ) {
if ( i == 4 ) {
i = 0;
}
++counter;
std::cout << "Count the recursion: "
<< std::setw( 2 ) << counter << " " << i << '\n';

// just to stop the recursion
if ( counter == 10 ) break;
}

std::cout << "\nPress any key and enter to quit.\n";
std::cin.get();
return 0;
}

上面没有最后一个if 语句的程序将模拟程序中发生的情况。我只包含它以停止循环以显示输出的进度。

我不知道你是否有意想要无限递归 for 循环;但是如果你这样做,你需要在调试器中检查你的计数器变量,以确保它们与执行退出循环以停止递归所涉及的语句所需的值相匹配。就像我在上面的小例子中展示的那样;没有计数器等于 10 的条件。循环将永远持续下去。

如果您确实打算有一个无限循环,请作为旁注;通常最好以这种方式构建它们,这样可以更清楚地了解您打算做什么。

int counter = 0;
for ( ; ; ) {
++counter;

// do some work;

if ( counter == exit condition ) break;
}

int counter = 0;
for ( ; ; ) {
// do some work;

if work above is valid
increment counter
else
break;
}

或者你可以使用 while 循环来代替

counter = some value
while ( true ) {
check counter for condition;

do some work

increment or set counter;
}

关于c++ - Knight's Tour C++ 使用堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49604066/

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