0

我正在尝试使用递归回溯解决骑士巡回赛问题。有人可以帮我优化我的代码。我的代码工作到 6X6 板。. 在 N=7 之后,几乎需要无限时间来求解 。这是我的代码:

#include <iostream>
#include "genlib.h"
#include "grid.h"
#include "vector.h"
#include <iomanip>

const int NOT_VISITED = -1;
//Size of the board
const int N = 6;
const int N2 = N*N;

typedef Grid<int> chess;

struct position{
    int row;
    int col;
};

//Initializes the board and makes each and every
//square value as NOT_VISITED
void initializeBoard(chess &board)
{
    for(int i=0;i<board.numRows();i++)
        for(int j=0;j<board.numCols();j++)
            board[i][j] = NOT_VISITED;
}

//Returns true if the square is visited;
bool visited(chess &board,position square)
{
    return board[square.row][square.col ] != NOT_VISITED;
}

//Returns true if the givien position variable is outside the chess board
bool outsideChess(chess &board, position square)
{
    if(square.row <board.numRows() && square.col <board.numCols() && square.row >=0 && square.col >=0)
        return false;
    return true;
}

void visitSquare(chess &board,position square,int count)
{
    board[square.row] [square.col] = count;
}

void unVisitSquare(chess &board,position square)
{
    board[square.row] [square.col] = NOT_VISITED;
}

position next(position square,int irow, int icol)
{
    square.row += irow;
    square.col += icol;
    return square;
}
Vector<position> calulateNextSquare(chess board,position square)
{
    Vector<position> list;
    for(int i=-2;i<3;i=i+4)
    {
        for(int j=-1;j<2;j=j+2)
        {
            list.add(next(square,i,j));
            list.add(next(square,j,i));
        }
    }
    return list;

}

bool knightTour(chess &board,position square, int count)
{
    //cout<<count<<endl;
    //Base Case if the problem is solved;
    if(count>N2)
        return true;
    if(outsideChess(board,square))
        return false;
    //return false if the square is already visited
    if(visited(board,square))
        return false;
    visitSquare(board,square,count);
    Vector<position> nextSquareList = calulateNextSquare(board,square); 
    for(int i=0;i<nextSquareList.size();i++)
        if(knightTour(board, nextSquareList[i], count+1))
            return true;
    unVisitSquare(board,square);
    return false;
}


void printChess(chess &board)
{
    for(int i=0;i<board.numRows();i++)
    {
        for(int j=0;j<board.numCols();j++)
            cout<<setw(4)<<board[i][j];
        cout<<endl;
    }
}


int main()
{
    chess board(N,N);
    initializeBoard(board);
    position start;
    start.row = 0; start.col = 0;
    if(knightTour(board,start,1))
        printChess(board);
    else
        cout<<"Not Possible";
    return 0;
}

我正在使用斯坦福 106B 库(网格是二维向量)Visual Studio 2008 带有所需库文件的空白项目https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0BwLe9NJT8IreNWU0N2M5MGUtY2UxZC00ZTY2LWE1YjQtMjgxYzAxMWE3OWU2&hl=en

4

3 回答 3

1

我想说,首先,摆脱这个:

Vector<position> nextSquareList = calulateNextSquare(board,square);

在每个步骤上创建一个 Vector 将花费大量时间。您可以使用一个数组(固定大小,因为您知道有 8 个可能的移动),或者完全展开循环。与此版本相比,与您的类似

于 2011-10-06T15:48:52.410 回答
0

我想建议的一些修改:

#include <iostream>
#include "genlib.h"
#include "grid.h"
#include "vector.h"
#include <iomanip>

const int NOT_VISITED = -1;
//Size of the board
const int N = 6;
const int N2 = N*N;

typedef int chess[N][N]; // <------------- HERE

struct position{
    int row;
    int col;
};

//Initializes the board and makes each and every
//square value as NOT_VISITED
void initializeBoard(chess &board)
{
    for(int i=0;i<board.numRows();i++)
        for(int j=0;j<board.numCols();j++)
            board[i][j] = NOT_VISITED;
}

//Returns true if the square is visited;
bool visited(chess &board,position square)
{
    return board[square.row][square.col ] != NOT_VISITED;
}

//Returns true if the givien position variable is outside the chess board
bool outsideChess(chess &board, position square)
{
    if(square.row <board.numRows() && square.col <board.numCols() && square.row >=0 && square.col >=0)
        return false;
    return true;
}

void visitSquare(chess &board,position square,int count)
{
    board[square.row] [square.col] = count;
}

void unVisitSquare(chess &board,position square)
{
    board[square.row] [square.col] = NOT_VISITED;
}

position next(position square,int irow, int icol)
{
    square.row += irow;
    square.col += icol;
    return square;
}
void calulateNextSquare(chess board,position square, Vector<position>& list)  // <------------- HERE
{
    // ------------- HERE
    //Also, change this part to add only unvisited and not out-of-board positions.
    for(int i=-2;i<3;i=i+4)
    {
        for(int j=-1;j<2;j=j+2)
        {
            list.add(next(square,i,j));
            list.add(next(square,j,i));
        }
    }
}

bool knightTour(chess &board,position square, int count)
{
    //cout<<count<<endl;
    //Base Case if the problem is solved;
    if(count>N2)
        return true;
    if(outsideChess(board,square))
        return false;
    //return false if the square is already visited
    if(visited(board,square))
        return false;
    visitSquare(board,square,count);
    Vector<position> nextSquareList;  // <------------- HERE
    calulateNextSquare(board,square,nextSquareList); 
    for(int i=0;i<nextSquareList.size();i++)
        if(knightTour(board, nextSquareList[i], count+1))
            return true;
    unVisitSquare(board,square);
    return false;
}


void printChess(chess &board)
{
    for(int i=0;i<board.numRows();i++)
    {
        for(int j=0;j<board.numCols();j++)
            cout<<setw(4)<<board[i][j];
        cout<<endl;
    }
}


int main()
{
    chess board(N,N);
    initializeBoard(board);
    position start;
    start.row = 0; start.col = 0;
    if(knightTour(board,start,1))
        printChess(board);
    else
        cout<<"Not Possible";
    return 0;
}

但请注意,您仍然有指数级的复杂性,优化您的代码不会改变它。

于 2011-10-06T15:49:15.990 回答
0

您正在将电路板的副本传递给 calculateNextSquare,但在此方法中您似乎不需要它。

此外,您在此方法中返回一个向量,但您应该通过引用传递它。

于 2011-10-06T15:55:49.737 回答