A POJ1321-Chess Problem

Source: Internet
Author: User

 

General question:

Chinese question .. I have nothing to say.

 

Solution:

DFS, it is very difficult to have no idea, and it is easy to have an idea.

Board rules are not difficult or not, and can be marked to solve the problem no matter the rules are irregular.

The difficulty lies in the relationship between the number of rows (number of columns) n of the board and the total number of chess pieces to be placed k <= N

 

K = N is easy to handle.

K <n is confusing.

 

The common practices on the Internet are row-by-row deep search, which is very efficient. I have also learned a little from it. For details, seeProgramNot to mention, the search question is too abstract and the text is hard to say clearly.

 

 // memory time // 184 K 32 Ms # include 
  
    using namespace STD; bool chess [9] [9]; bool vist_col [9]; // mark int status in the column; // status counter int N, K; void DFS (INT row, int num) // search by row, row is the current search row, and num is the number of filled pieces {If (num = k) {status ++; return ;} if (row> N) // used with the following DFS (row + 1, num); statement to avoid cross-border return; For (Int J = 1; j <= N; j ++) if (chess [row] [J] &! Vist_col [J]) {vist_col [J] = true; // mark DFS (row + 1, num + 1) as the column where the pawn is placed; vist_col [J] = false; // After backtracking, it indicates that the status of the pawns has been recorded, and the current column marker is restored} DFS (row + 1, num); // This is difficult. When k 
   
     N> K) {If (n =-1 & K =-1) break;/* Initial */memset (chess, false, sizeof (ChEss); memset (vist_col, false, sizeof (vist_col); status = 0; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) {char temp; CIN> temp; If (temp = '#') chess [I] [J] = true;} DFS (1, 0 ); // note that cout 
    
      is incorrect.
    
   
  

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.