首页 > 解决方案 > Knight 的巡回递归只填满了 60 个方格

问题描述

我几天来一直在研究这个骑士的巡回递归问题,但仍然找不到解决方案。有人可以帮我找出错误吗?我一直在寻找它,它可以填充的最大瓷砖只有60。我不知道错误是什么。这是我的代码:

#include <iostream>;
#include <string>;
#include <vector>;
#include <cstdlib>;
#include <cmath>;
#include <ctype.h>;
#include <ctime>;
#include <stdlib.h>;
using namespace std;
int step=1, v[8][8], x[8], y[8];
bool success;
bool SolveKnightTour(int step, int xc, int yc){
    if (step>64){
    success=true;
    }
    else {
    success=false;
        int move=0;
        while(move<8 & success==false){
            if (xc+x[move]>=0 & xc+x[move]<8 & yc+y[move]>=0 & yc+y[move]<8 & v[yc+y[move]][xc+x[move]]==0){
            v[yc+y[move]][xc+x[move]]=step+1;
                if (!SolveKnightTour(step+1, xc+x[move], yc+y[move])){
                v[yc+y[move]][xc+x[move]]=0;
                }
            }
            if (success==false){
            move++;
            }
        }
    }
    return success;
}
int main(){
    int i, j;
    x[0]=1, y[0]=-2;
    x[1]=2, y[1]=-1;
    x[2]=2, y[2]=1;
    x[3]=1, y[3]=2;
    x[4]=-1, y[4]=2;
    x[5]=-2, y[5]=1;
    x[6]=-2, y[6]=-1;
    x[7]=-1, y[7]=-2;
    for (i=0;i<8;i++){
        for (j=0;j<8;j++){
        v[i][j]=0;
        }
    }
    v[4][4]=1;
    SolveKnightTour(step, 4, 4);
    for (i=0;i<8;i++){
        for (j=0;j<8;j++){
        cout<<v[i][j]<<" ";
        }
        cout<<"\n";
    }

    system("pause");

    return 0;
}

任何帮助是极大的赞赏。

标签: c++knights-tour

解决方案


推荐阅读