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)