回溯算法:N-皇后问题、K-着色问题(Java实现)

1.N-皇后算法

import java.util.Scanner;

/**
 *  @author: cuttle
 *  @Date: 2020/11/19 15:52
 *  @Description: N-皇后问题回溯算法
 */
public class BT_NQueens {
    private int N;//问题规模N
    private int[] queen;//N个皇后的布局,Xi表示第i行皇后位置
    private boolean done;
    public BT_NQueens(int N){
        this.N = N;
        queen = new int[N];
        done = false;
    }
    public void bt_NQueens(int row){
        for(int col = 0;!done && col < N;col++){
            if(checkPlacing(row,col)){
                queen[row] = col;
                if(row == N-1){
                    done = true;
                }else{
                    bt_NQueens(row + 1);
                }
            }
        }
    }
    public boolean checkPlacing(int row,int col){
        for(int i = 0;i < row;i++){
            if(queen[i] == col || Math.abs(queen[i] - col) == Math.abs(i - row)){
                return false;
            }
        }
        return true;
    }
    public void print(){
        System.out.println(N + "-后问题:");
        for(int i=0;i<N;i++){
            System.out.print("P[" + (i + 1) + "]: ");
            System.out.println("(" + (i + 1) + "," + (queen[i] + 1) +")");
        }
    }
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入N:");
        int N = sc.nextInt();
        sc.close();
        BT_NQueens q = new BT_NQueens(N);
        q.bt_NQueens(0);
        q.print();
    }
}

2.K-着色问题


/**
 *  @author: cuttle
 *  @Date: 2020/11/19 17:07
 *  @Description: 图的K-着色问题回溯算法
 */
public class BT_KColoring {
    private int N;//顶点数
    private int K;//颜色数目
    private int[][] matrix;
    private int[] colors;//每个顶点的颜色记录
    public BT_KColoring(int N,int K,int[][] matrix){
        this.N = N;
        this.K = K;
        this.matrix = matrix;
        colors = new int[N];
    }
    public void bt_KColoring(int v){
        int c = 1;
        while(colors[N - 1] == 0 && c <= K ){
            colors[v] = c;
            if(checkColoring(v) == -1){
                if(v < N - 1){
                    bt_KColoring(v + 1);
                }else{
                    break;
                }
            }else {
                colors[v] = 0;//如果该颜色不满足约束条件,再把此处置为0
            }
            c++;
        }
        if(colors[N - 1] == 0 && c > K){
            colors[v] = 0;
        }
    }
    public int checkColoring(int v){
        for(int u = 0;u < v;u++)
            if(matrix[v][u] == 1 && colors[u] == colors[v])
                return u;
        return -1;
    }
    public void print(){
        System.out.println(K + "-着色问题:");
        for(int i = 0;i < N;i++){
            System.out.print("C[" + (i + 1) + "]: ");
            System.out.println(colors[i] + " ");
        }
    }
    public static void main(String[]args){
        //06U-01(Wikipedia-Dijkstra),K=4
        int N = 6;
        int K = 4;
        int[][] matrix = {
                {0,1,1,0,0,1},
                {1,0,1,1,0,0},
                {1,1,0,1,0,1},
                {0,1,1,0,1,0},
                {0,0,0,1,0,1},
                {1,0,1,0,1,0}
        };
        BT_KColoring k = new BT_KColoring(N,K,matrix);
        k.bt_KColoring(0);
        k.print();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值