微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

骑士之旅启发式C ++

如何解决骑士之旅启发式C ++

我被困住了。我知道我希望我的程序采取行动,从启发式的可能行动中减去一个值,并且当位置达到0或被触摸时,我不希望它尝试。我正在尝试使骑士之旅首先在可访问性数组中达到最低值,然后再尝试获得更高的值。如果有人对我将如何执行此操作有任何见解,我将不胜感激。

#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <iomanip>

using namespace std;
int access[8][8] ={ {2,3,4,2},{3,6,3},{4,8,4},{2,2}};

bool boardTest(int move,int row,int col);
bool newMove(int board[8][8],int move,int col);
bool validTest(int board[8][8],int col);
void printHeuristic(int[8][8]);
void printArray( int[8][8]);
void knightMvt(int board[8][8],int &currentRow,int &currentCol);

int runKnight(int board[8][8],bool movesKnightTried[8],int startRow,int startCol);

const int horizontal[8] = {2,1,-1,-2,2};
const int vertical[8]= {-1,2,1};

int main()
{
    bool movesKnightTried[8] = { 0 };
    int board[8][8] = { 0 };
    printHeuristic(access);
    int currentRow = 0;
    int currentCol = 0;
    
    int startRow = 0;
    int startCol=0;

    int counter = runKnight(board,movesKnightTried,startRow,startCol);
    printArray(board);

}

int runKnight(int board[8][8],int startCol)
{
    int pastCounter;
    bool novalidMove = false;
    int currentRow;
    int currentCol;
    int counter = 1;
    currentRow = startRow;
    currentCol = startCol;

    int bestMove = -1;
    int bestAccessibility = 99;
    board[currentRow][currentCol] = counter;
    while (counter < 65 && novalidMove == false)
    {

        for (int move = 0; move<8;move++)
        {
        
            if (validTest(board,move,currentRow,currentCol) && 
            access[currentRow + vertical[move]][currentCol + horizontal[move]] <= bestAccessibility)
            {
                bestMove = move;
                bestAccessibility = access[currentRow+vertical[move]][currentCol + horizontal[move]];
                
                knightMvt(board,currentCol);
            
                counter++;
            
                board[currentRow][currentCol] = counter;
            
                break;
            
            }
            if (move == 7)
            {
                novalidMove = true;
            }
        
        }

    }
    
    return 0;
};

void printArray( int board[8][8])
{
    for (int r = 0; r < 8; r++)
    {
        for (int c = 0; c < 8; c++)
            cout << setw(3) << board[r][c] << ' ';
            
        cout << endl;
    }
};

void printHeuristic( int heuristic[8][8])
{
    for (int r = 0; r < 8; r++)
    {
        for (int c = 0; c < 8; c++)
            cout << setw(3) << heuristic[r][c] << ' ';
            
        cout << endl;
    }
};

bool boardTest(int move,int col)
{
    if (row + vertical[move] <0 || row+ vertical[move]>7)
    {
        return false;
    }
    else if (col + horizontal[move]<0 || col + horizontal[move]>7)
    {
        return false;
    }
    else
    {
        return true;
    }
};


bool validTest(int board[][8],int col)
{
    if(boardTest(move,row,col) && newMove(board,col))
    {
        return true;
    }
    else
    {
        return false;
    }
};


bool newMove(int board[8][8],int col)
{
    if (board[row + vertical[move]][col + horizontal[move]] == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
};

void knightMvt(int board[8][8],int& curRow,int& curCol)
{
    curRow += vertical[move];
    curCol += horizontal[move];
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。