Poj1321 board Problems

Source: Internet
Author: User

After thinking about this question for a long time, I read the eight queens question. This question is more troublesome than the eight queens question: when the number of chess pieces is less than the number of Checker lines, we must continue to recursion and backtracking. Code directly (without any pruning)

# Include <iostream> <br/> using namespace STD; <br/> int Board [9] [9], Col [9], count; <br/> void DFS (int n, int K, int line) // K indicates the number of currently remaining pieces, line is the row to be tested. <br/>{< br/> int I, j; <br/> for (j = 1; j <= N; j ++) <br/> If (Board [Line] [J] &! Col [J]) <br/>{< br/> If (k = 1) <br/>{< br/> count ++; <br/> continue; <br/>}< br/> Col [J] = 1; <br/> for (I = line + 1; I <= N; I ++) <br/> DFS (n, K-1, I); <br/> Col [J] = 0; <br/>}< br/> int main () <br/>{< br/> int B, num, C, R; <br/> char CHR; <br/> while (scanf ("% d", & B, & num )) <br/>{< br/> If (B =-1 & num =-1) <br/> break; <br/> getchar (); <br/> for (r = 1; r <= B; r ++) <br/> {<br/> for (C = 1; C <= B; c ++) <br/>{< br/> scanf ("% C", & CHR); <br/> If (CHR = '#') <br/> Board [r] [c] = 1; <br/> else <br/> Board [r] [c] = 0; <br/>}< br/> getchar (); <br/>}< br/> If (num = 0 | B = 0) <br/>{< br/> printf ("0/N"); <br/> continue; <br/>}< br/> COUNT = 0; <br/> for (r = 1; r <= B-num + 1; r ++) <br/> {<br/> memset (COL, 0, sizeof (COL); <br/> DFS (B, num, R); <br/>}< br/> printf ("% d/N", count ); <br/>}< br/> return 0; <br/>}


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.