Simple back n-Queens (n Queen) Problem

package com.main;

import java.util.LinkedList;

public class NoQueue {

    public LinkedList<Node> getQueue(int n){
        LinkedList<Node> queues = new LinkedList<Node>();
        int m=0;
        boolean p = true;  // 是否需要向上回溯
        while(m < n){
            if(m == 0){
                Node q= new Node(0,0);
                queues.add(q);
                m ++                             queues.add (q1);; 
            } 
            IF (n-> 0 ) { 
                the Node Q = queues.getLast ();
                 IF (P) { 
                    P = to false ; // default not found 
                    for ( int I = 0 ; I <n-; I ++ ) { 
                        the Node Ql = new new the Node (q.x + . 1 , I);
                         // Comparative 
                        IF (checkQueue (Ql, Queues)) {   // find the results to the next track 

                            m ++ ; 
                            P = to true ;
                             BREAK ; 
                        } 
                    } 
                } the else { // No results found back upwardly 
                    Q = queues.removeLast ();   // the last one taken out 
                    for ( int I = q.y + . 1 ; I <n-; ++ I ) { 
                        the Node Ql = new new the Node (QX, I);
                         IF (checkQueue (Ql, Queues)) {   // find the results to the next track 
                            queues.addLast (q1); 
                            P = to true ;
                             BREAK;
                        }
                    }
                    if(!p){
                        m--;
                    }
                }
            }
            
        }
        return queues;
    }
    public boolean checkQueue(Node q1,LinkedList<Node> queues){
        boolean b = true;
        if(queues.size() == 0){
            return b;
        }
        for(int i=0;i<queues.size();i++){
            Node q = queues.get(i);
            if(q1.x==q.x || q1.y==q.y || q1.x-q.x == q1.y-q.y || q1.x-q.x == -1*(q1.y-q.y)){
                b=false;
                break;
            }
        }
        return b;
    }
    public static void main(String[] args) {
        NoQueue noq = new NoQueue();
        int n= 4;
        LinkedList<Node> l = noq.getQueue(n);
        for(int j=0;j<n;j++){
            for(int i=0;i<n;i++){
            Node node= l.get(i);
                if(j == node.y){
                    System.out.print(" Q");
                }else{
                    System.out.print(" 1");
                }
            }
            System.out.println();
        }
    }
    class Node {
        int x;
        int y;
        public Node(int i,int j){
            this.x=i;
            this.y=j;
        }
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return x+","+y;
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/qinshuipo/p/11454393.html