HDU 2553 n queen problem (Deep Search DFS)

Source: Internet
Author: User
N queen's question

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1757 accepted submission (s): 772


 

Problem description places n queens on the square board of N * n so that they do not attack each other (that is, two queens are not allowed to be in the same row, the same column, it is not allowed to be on a diagonal line with 45 corners of the checker border.
Your task is to determine the number of valid placement methods for the given n.
There are several input rows. Each row has a positive integer of N ≤ 10, indicating the number of the Board and the Queen. If n = 0, it indicates the end. There are several rows in output. Each row has a positive integer, indicating the number of different places corresponding to the queen of the input row. Sample input1850 sample output19210
1 // classic n queen Question 2 # include <iostream> 3 # include <stdio. h> 4 # include <memory. h> 5 using namespace STD; 6 7 int ch [25] [25]; 8 int N, num, result [25]; 9 10 void DFS (int x, int y) 11 {12 if (CH [x] [Y]) return; // if the point has been attacked, 13 int I, XX, YY is returned; 14 if (x = N) // If 15 {16 num ++; 17 return; 18} 19 20 // below I think it is clever, because there will be duplicates, it is difficult to analyze which queen belongs 21 // if the following position's ++ or -- is used --, this cleverly avoids the above 22 // marking in a total of eight directions: top, bottom, left, right, top left, bottom left, top right, bottom right, bottom right 23 xx = x; YY = y; 24 while (XX> 0) CH [xx --] [Y] ++; 25 xx = x; YY = y; 26 while (yy> 0) ch [x] [yy --] ++; 27 xx = x; YY = y; 28 while (XX <= N) CH [XX ++] [Y] ++; 29 xx = x; YY = y; 30 While (yy <= N) CH [x] [yy ++] ++; 31 xx = x; YY = y; 32 While (XX <= N & YY <= N) CH [XX ++] [yy ++] ++; 33 xx = x; YY = y; 34 while (XX> 0 & YY <= N) CH [xx --] [yy ++] ++; 35 xx = x; YY = y; 36 while (XX <= N & YY> 0) CH [XX ++] [yy --] ++; 37 xx = x; YY = y; 38 While (XX> 0 & YY> 0) CH [xx --] [yy --] ++; 39 40 for (I = 1; I <= N; I ++) 41 {42 DFS (x + 1, I); 43} 44 45 // if the results of the Queen's Deep Search fail, the original situation will be returned, in all eight directions -- 46 xx = x; YY = y; 47 While (XX> 0) CH [xx --] [Y] --; 48 xx = x; YY = y; 49 while (yy> 0) CH [x] [yy --] --; 50 xx = x; YY = y; 51 while (XX <= N) ch [XX ++] [Y] --; 52 xx = x; YY = y; 53 While (yy <= N) CH [x] [yy ++] --; 54 xx = x; YY = y; 55 while (XX <= N & YY <= N) CH [XX ++] [yy ++] --; 56 xx = x; YY = y; 57 while (XX> 0 & YY <= N) CH [xx --] [yy ++] --; 58 xx = X; YY = y; 59 While (XX <= N & YY> 0) CH [XX ++] [yy --] --; 60 xx = x; YY = y; 61 while (XX> 0 & YY> 0) CH [xx --] [yy --] --; 62} 63 64 void Init () // initialization function 65 {66 int I, j; 67 for (I = 0; I <12; I ++) 68 for (j = 0; j <12; j ++) 69 ch [I] [J] = 0; 70} 71 72 void set () 73 {74 int I, K; 75 for (k = 1; k <= 10; k ++) 76 {77 num = 0; n = K; // initialize 78 for (I = 1; I <= K; I ++) 79 {80 Init (); // initialize 81 DFS (1, I); // continue DFS search 82} 83 result [k] = num; 84} 85} 86 87 int main () 88 {89 set (); 90 while (scanf ("% d", & N), n) 91 {92 printf ("% d \ n", result [N]); 93} 94 95 return 0; 96}
View code

 

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.