Eight Queens problem Las Vegas algorithm

Source: Internet
Author: User

Java

ImportJava.util.Random; Public classLvqueen {//Problem Size    Static intSIZE = 8; //Random number generator    StaticRandom rnd =NewRandom (SIZE); //Solution Vector    Static int[] Queen =New int[SIZE]; Private Static BooleanCheckintrow) {         for(inti = 0; I < queen.length && I! = row; i++) {            if(Queen[i] = = Queen[row] | | i-queen[i] = = Row-queen[row] | | i + queen[i] = = row +Queen[row]) {                return false; }        }        return true; }    Private Static BooleanQUEENSLV () {intRow = 0; intCount = 1;  while(Row < SIZE) && (Count > 0) ) {count= 0; intj = 0;  for(intcolumn = 0; Column < SIZE; column++) {Queen[row]=column; if(row) {if(Rnd.nextint (++count) = = 0) {J=column; //Break ;//There is a break if the first to find the appropriate column place (k) is satisfied, then the random (1) ==0 is set up, and encountered the following, the Queen placed in this position. If the Queen's plan is not feasible, the next cycle will be executed the same, so the loop cannot find a solution.                        The possibility that all the remaining queens would not be able to be placed is increased. //without break, you will always be tempted to end the for loop. X[K] will randomly select the last satisfied column of the For loop in the position where it is currently available to be placed. Then the likelihood of n-1 a queen can be reduced.                     }                }            }            if(Count > 0) {Queen[row++] =J; }        }        return(Count > 0); }     Public Static voidNqueen () { while(!QUEENSLV ()); System.out.println ("-----Solution--------");  for(inti = 0; I < 8; i++) {             for(intj = 0; J < 8; J + +) {                if(Queen[i] = =j) {System.out.print ("Q"); } Else{System.out.print (" . ");        }} System.out.println (); }    }     Public Static voidMain (string[] args) {nqueen (); }}

Sum up the thought of it,

is to start from the first line, looking for a position that can be placed, obviously the first row of seven kinds of pendulum can be, randomly extract one, put up

To the second row, the position can be placed less than a few, from the various inside and randomly take a pendulum up

So the loop, but obviously large probability placed in the back of the time, will find no solution, so there will be

while (!  QUEENSLV ());

This is the end of the line, knowing the chance to find out.

The difference between this and the previous windfall recursion algorithm is that

1. The Las Vegas algorithm is designed to find a solution rather than a full solution

2. Because it is not an orderly traversal, the efficiency of the LV algorithm is not high, but theoretically there is a possibility to find the solution faster than the traversal

Eight Queens problem Las Vegas algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.