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.