Poj 1321 board problem (DFS)

Source: Internet
Author: User

Link: poj 1321

Question: Given a n * n board, # represents the board area and can be used as a pawn,

. Represents the blank area (do not place chess pieces). Now K chess pieces are to be placed, and the number of solutions is calculated.

Requirement: any two chess pieces cannot be placed in the same row or column of the Board.

Idea: Because any two pawns cannot be in the same row or column, you can perform a row-by-row DFS and mark the accessed rows.

# Include <stdio. h> # include <string. h> char s [10] [10] ;__ int64 CNT; int sum, n, m, vis [10]; void DFS (int r) {int I, J; for (I = R + 1; I <n; I ++) // downstream DFS from this row (j = 0; j <n; j ++) if (s [I] [J] = '#'&&! Vis [J]) {sum ++; vis [J] = 1; // mark it as 1 If (sum = m) CNT ++ after access; else DFS (I); sum --; // trace vis [J] = 0 ;}} int main () {int I, J; while (scanf ("% d", & N, & M )! = EOF) {If (M =-1 & n =-1) break; CNT = 0; for (I = 0; I <n; I ++) scanf ("% s", s [I]); for (I = 0; I <n; I ++) for (j = 0; j <N; j ++) {memset (VIS, 0, sizeof (VIS); // first initialize to 0 if (s [I] [J] = '#') {// perform DFS sum = 1; vis [J] = 1; if (sum = m) {CNT ++; continue;} DFS (I) ;}} printf ("% i64d \ n", CNT);} return 0 ;}


Poj 1321 board problem (DFS)

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.