回溯:8位主教占据整个董事会

时间:2015-04-27 08:54:01

标签: c++ algorithm recursion backtracking

我坚持写下这种回溯算法。问题:8位主教必须覆盖整个棋盘。它所要做的就是在白棋盘空间放置4位主教,检查它是否占据32个空间。如果确实如此,将4位新主教移到左边,这样他们就站在黑空间,问题就解决了。如果没有 - 使用回溯将主教放在其他地方。问题是,我只是不能写下回溯 - 对我来说似乎太复杂了。

这就是我所做的:

void findBishops(){
    for (int i=0; i<N; i++){ //get row
        int j;
        if (i%2==1) j=1; //2n+1 row has second white space, so we skip the first black space
        else j=0;
        for (; j<N; j+=2){ //get column
            //put into array those bishop coordinates and repeat 3 more times to get all 4 bishops.
            isFull(board, array); //give coordinates and check if all white spaces are occupied
            //if not - backtrack
            }
    }
}

bool isFull(int  board[][N], array[]){
   putIntoBoard(board, array[0], array[1]);
   putIntoBoard(board, array[2], array[3]);
   putIntoBoard(board, array[4], array[5]);
   putIntoBoard(board, array[6], array[7]);

    int i,j;
    int count=0;
    for (i=0; i<=7; i++){

        if (i%2==1) j=1;
        else j=0;

        for (; j<=7; j+=2){
            if (board[i][j]==1) count++;
        }
    }
    if (count==32){
        clean(board); 
        return true;
    }else{  
        clean(board);
        return false;
    }
}

void putIntoBoard(int board[][N], int a, int b){ //fills diagonal white spaces on board with 1's
    int i=a,j=b;
    board[i][j]=1;

    while(i>0 && (j<7) )/*to Up right*/{
        i--;
        j++;
        board[i][j]=1;
    }
    i=a;
    j=b;
    while(j>0 && i>0) /*to Up left*/{
        i--;
        j--;
        board[i][j]=1;

    }
    i=a;
    j=b;
    while(i<7&& j<7) /*to bottom right*/{
        i++;
        j++;
        board[i][j]=1;

    }
    i=a;
    j=b;

    while(i<7 && j>0) /*to bottom left*/{

        i++;
        j--;
        board[i][j]=1;
    }


}

这是main function

#include <iostream>
#define N 8
using namespace std;
void print(int board[][N]);
void putIntoBoard(int a, int b, int board[][N]);
bool isFull(int  board[][N], array[]);   
void clean(int board[][N]);

int main()
{
    int board [N][N]= {0};
    int count= 0;

    findBishops();

    cout<<"Counted possibilites: "<<count<<endl;
    return 0;
}

这只是一个原型,如果你有更好的东西,请分享,我很乐意接受所有的评论。

编辑:我忘了包含几天前使用的其他算法,但它没有递归也没有回溯,这里是:

int main()
{
    int board [N][N]= {0};
    int count= 0;



    for (int i=0; i<N; i++)
    {
        int j;
        if (i%2==1) j=1;
        else j=0;
        for (; j<N; j+=2)
        {
            for(int k=0; k<N; k++)
            {
                int n;
                if (k%2==1) n=1;
                else n=0;
                for (; n<N; n+=2)
                {

                    for (int l=0; l<N; l++)
                    {
                        int o;
                        if (l%2==1) o=1;
                        else o=0;
                        for (; o<N; o+=2)
                        {

                            for(int m=0; m<N; m++)
                            {
                                int p;
                                if (m%2==1) p=1;
                                else p=0;
                                for (; p<N; p+=2)
                                {

                                    if (isFull(board,i,j,k,n,l,o,m,p))
                                    {
                                        count++;
                                        cout<<"Board filled up with white spaces on: ("<<i<<","<<j<<"), "<<"("<<k<<","<<n<<"), "<<"("<<l<<","<<o<<"), "<<"("<<m<<","<<p<<"), "<<endl;
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout<<"Counted possibilities: "<<count<<endl;

    return 0;
}

1 个答案:

答案 0 :(得分:1)

为了将4位主教移动到黑场,只需镜像它们相对于垂直中心轴。或横向 - 结果将是相同的。他们将前往黑色的田野,他们将威胁所有黑色的田地(如果他们在镜像之前威胁所有的白色田野)。