骑士之旅C ++

时间:2011-10-06 15:24:40

标签: c++ optimization

我正在尝试使用递归回溯解决Knight Tour问题。有人可以帮助我优化我的代码。我的代码工作到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;
}

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

3 个答案:

答案 0 :(得分:1)

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

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

在每个步骤上创建一个Vector会花费很多时间。您可以使用数组(固定大小,因为您知道有8种可能的移动)或unroll the loop entirely。与this version, similar to yours比较。

答案 1 :(得分: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;
}

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

答案 2 :(得分:0)

您正在将计划板的副本传递给calculateNextSquare,但似乎您不需要此方法。

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