Lover to meet the problem. Java code

Source: Internet
Author: User
Tags count int size return
Problem/*
* 8 Valentine Questions:
*
* Problem Description:
* Place 8 lovers in a 8x8 chessboard, asking each Valentine 22 not to conflict
* (in each row, vertical column, oblique column only one lover).
*
* Data indicates:
* Use a 8-bit 8-digit number to indicate the position of the lover on the chessboard:
* For example: 45615353 means:
* No. 0 Lover in 4th place
* 1th lover in 5th place
* 2nd lover in 6th place
* 。。。
* 7th lover in 3rd place
*
* The process of looping variables from 00000000 to 77777777 (8 numbers) traverses all the cases of the lover
* The program uses octal numbers in a one-dimensional array data[] to represent
*
* Detect conflicts:
* Row conflict: data[i] = = Data[j]
* Diagonal column conflict: (data[i]+i) = = (Data[j]+j) or (data[i]-i) = = (DATA[J]-J)
*
Benefits
* Using loops rather than recursion, with less system resources
* can calculate n Valentine's problem
* The problem of linearization processing, you can block the problem, in a distributed environment with multiple computers together.
*
* ToDo:
* The enumeration section can also be optimized, and more judgment conditions can be faster.
* Output part can be modified into a checkerboard form of output
*
* @author CinC 2002-09-11
*
*/

public class Queen {
int size;
int resultcount;

public void compute (int size) {
this.size = size;
Resultcount = 0;
int data[] = new Int[size];
int count; Number of all possible cases
int i,j;

Calculate the number of all possible cases
Count = 1;
for (i=0; i<size; i++) {
Count = count * size;
}
For every possible situation
for (i=0; i<count; i++) {
Calculates the position of the lover on the chessboard in this case, using a 8-digit representation
Here you can optimize
int temp = i;
for (j=0; j<size; j) {
data [j] = temp% size;
temp = temp/size;
}
Test the feasibility of this situation, if possible, output
if (Test (data))
Output (data);
}
}

/*
* Test this situation if the lover's arrangement is feasible
*
*/
public boolean test (int[] data) {
int i,j;
for (i=0; i<size; i++) {
for (j=i+1; j<size; j) {
Test whether in the same row
if (data[i] = = Data[j])
return false;
Test whether in a slash
if ((data[i]+i) = = (DATA[J]+J)
return false;
Test whether in a backslash
if ((data[i]-i) = = (DATA[J]-J)
return false;
}
}
return true;
}

/*
* Output the coordinates of a lover in some case
*
*/
public void output (int[] data) {
int i;
System.out.print (++resultcount + ":");
for (i=0; i<size; i++) {
System.out.print ("+ i +", "+ data[i] +" ");
}
System.out.println ();
}

The main () is here.
public static void Main (String args[]) {
(New Queen ()). Compute (8);
}
}




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.