每天刷個演算法題20160519:回溯法解八皇后

小飛_Xiaofei發表於2016-05-25

版權所有。所有權利保留。

歡迎轉載,轉載時請註明出處:

http://blog.csdn.net/xiaofei_it/article/details/51502622


為了防止思維僵化,每天刷個演算法題。已經刷了幾天了,現在發點程式碼。

我已經建了一個開源專案,每天的題目都在裡面:

https://github.com/Xiaofei-it/Algorithms

絕大部分演算法都是我自己寫的,沒有參考網上通用程式碼。讀者可能會覺得有的程式碼晦澀難懂,因為那是我自己的理解。

最近幾天都是在寫一些原來的東西,大多數是非遞迴。以後準備刷點DP、貪心之類的題。

下面是回溯法八皇后:

/**
 *
 * Copyright 2016 Xiaofei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package xiaofei.algorithm;

/**
 * Created by Xiaofei on 16/5/19.
 */
public class EightQueensPuzzle {

    /**
     * 八皇后(回溯法)
     * pos[n]
     * 豎排:值相同
     * 左上到右下:i - j = pos[i] - pos[j]  -->  i - pos[i] = j - pos[j]
     * 左下到右上:i - j = pos[j] - pos[i]  -->  i + pos[i] = j + pos[j]
     *
     * 每次都要O(n^2)判斷有點蛋疼,不知道有沒有優化方案。
     * 現在自己優化一下:
     * hash判斷
     * 豎排:hash[SIZE]
     * 左上到右下:i - pos[i] = j - pos[j] 從-(SIZE-1)到(SIZE-1) 為了從0開始,偏移(SIZE-1)
     * 左下到右上:i + pos[i] = j + pos[j] 從0到2*(SIZE-1)
     */

    private static final int SIZE = 8;

    private static int number = 0;

    private static void print(int[] pos) {
        System.out.println(++number);
        int length = pos.length;
        for (int i = 0; i < length; ++i) {
            System.out.print(" " + pos[i]);
        }
        System.out.println();
        for (int i = 0; i < length; ++i) {
            for (int j = 0; j < pos[i]; ++j) {
                System.out.print('+');
            }
            System.out.print('*');
            for (int j = pos[i] + 1; j < SIZE; ++j) {
                System.out.print('+');
            }
            System.out.println();
        }
    }

    public static void calculate() {
        int[] pos = new int[SIZE];
        boolean[] hash1 = new boolean[SIZE];
        boolean[] hash2 = new boolean[SIZE * 2 + 1];
        boolean[] hash3 = new boolean[SIZE * 2 + 1];
        int last = 0;
        pos[last] = -1;
        while (last >= 0) {
            if (last == SIZE) {
                print(pos);
                --last;
            } else {
                boolean flag = false;
                if (pos[last] >= 0) {
                    hash1[pos[last]] = false;
                    hash2[last - pos[last] + SIZE - 1] = false;
                    hash3[last + pos[last]] = false;
                }
                while (pos[last] < SIZE - 1) {
                    ++pos[last];
                    if (!hash1[pos[last]] && !hash2[last - pos[last] + SIZE - 1] && !hash3[last + pos[last]]) {
                        hash1[pos[last]] = true;
                        hash2[last - pos[last] + SIZE - 1] = true;
                        hash3[last + pos[last]] = true;
                        ++last;
                        if (last < SIZE) {
                            pos[last] = -1;
                        }
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    --last;
                }
            }
        }
    }
}




相關文章