n-queens

Hints to understand splendid program to solve Queens

最后都变了- 提交于 2019-11-30 02:53:49
问题 In Art of Prolog of Sterling & Shapiro, exercise Section 14.1 (v): queens(N,Qs) :- length(Qs,N), place_queens(N,Qs,_,_). place_queens(0,_Qs,_Ups,_Downs). place_queens(I,Qs,Ups,[_|Downs]) :- I > 0, I1 is I-1, place_queens(I1,Qs,[_|Ups] ,Downs), place_queen(I,Qs,Ups,Downs). place_queen(Q,[Q|_],[Q|_],[Q|_]). place_queen(Q,[_|Qs],[_|Ups],[_|Downs] ):- place_queen(Q,Qs,Ups,Downs). It is a splendid program, in 11 lines, which quickly solves the problem of positioning queens on a chessboard. It's

Understanding CLP(FD) Prolog code of N-queens problem

血红的双手。 提交于 2019-11-29 08:47:45
I am trying to understand N-queens problem's solution as given below: :- use_module(library(clpfd)). n_queens(N, Qs) :- length(Qs, N), Qs ins 1..N, safe_queens(Qs). safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs). safe_queens([], _, _). safe_queens([Q|Qs], Q0, D0) :- Q0 #\= Q, abs(Q0 - Q) #\= D0, D1 #= D0 + 1, safe_queens(Qs, Q0, D1). I am not able to understand the below snippet: safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs). safe_queens([], _, _). safe_queens([Q|Qs], Q0, D0) :- Q0 #\= Q, abs(Q0 - Q) #\= D0, D1 #= D0 + 1, safe

All possible solution of the n-Queen's algorithm

你离开我真会死。 提交于 2019-11-28 03:59:22
问题 When implementing an algorithm for all possible solution of an n-Queen problem, i found that the same solution is reached by many branches. Is there any good way to generate every unique solutions to the n-Queens problem? How to avoid the duplicate solutions generated by the different branches (except store and compare)? Here is what i have tried, for the first solution: http://www.ideone.com/hDpr3 Code: #include <stdio.h> #include <stdlib.h> #include <string.h> /* crude */ #define QUEEN 'Q'

java:implement 8 queen using depth first search

淺唱寂寞╮ 提交于 2019-11-27 08:47:37
问题 i am try to implement 8 queen using depth search for any initial state it work fine for empty board(no queen on the board) ,but i need it to work for initial state if there is a solution,if there is no solution for this initial state it will print there is no solution Here is my code: public class depth { public static void main(String[] args) { //we create a board int[][] board = new int[8][8]; board [0][0]=1; board [1][1]=1; board [2][2]=1; board [3][3]=1; board [4][4]=1; board [5][5]=1;

Solving N-Queens Problem… How far can we go?

风格不统一 提交于 2019-11-27 01:49:17
问题 The N-Queens Problem: This problem states that given a chess board of size N by N, find the different permutations in which N queens can be placed on the board without any one threatening each other. My question is: What is the maximum value of N for which a program can calculate the answer in reasonable amount of time? Or what is the largest N we have seen so far? Here is my program in CLPFD(Prolog): generate([],_). generate([H|T],N) :- H in 1..N , generate(T,N). lenlist(L,N) :- lenlist(L,0