Title Link: http://poj.org/problem?id=1321
Test instructions: Chinese topic, not much to say ...
Ideas:
There are many ways to solve the problem, just start thinking is to choose from N rows containing the "#" K line, and then put K in this K-line pieces, just fine. The time complexity is O (C (n, k) * k!), when the real writing is actually used 2N * k!, reluctantly too. Later thought can start from the first occurrence of the "#" search, after searching to jump directly to the next line to continue, you do not have to do the first time so troublesome.
Code:
(1)
1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <stack>7#include <queue>8#include <vector>9#include <algorithm>Ten#include <string> One AtypedefLong LongLL; - using namespacestd; - Const intMAXN =8; the intMAP[MAXN +3][MAXN +3]; - intROW[MAXN +3]; - intCHOSE[MAXN +3]; - intN, K; + intans; - + voidChessdfs (intt) { A if(T >k) { at++ans; - } - Else { - intCNT =0; - for(inti =1; I <= N; i++) {//from 1 to n rows, locate the row where the first T piece is placed. - if(Chose[i]) + +CNT; in if(cnt = = T &&Chose[i]) { - for(intj =1; J <= N; J + +) {//look for "#" in the line to if(Map[i][j] = ='#'&& Row[j] = =0) { +Chose[i] = j;//The first t piece is placed in the J position of line I -ROW[J] =1; theChessdfs (t +1); *ROW[J] =0; $ }Panax Notoginseng } - } the } + } A } the + intCheckintk) { - for(inti =1; I <= N; i++) { $ if(Map[k][i] = ='#'&& Chose[k])return 1; $ } - return 0; - } the - voidLinedfs (intt) {Wuyi if(T >N) { the intCNT =0; - for(inti =1; I <= N; i++) { Wu if(check (i)) cnt++;//determine the number of 1 in the chose array, and also determine if the selected line contains "#", while satisfying is really selected - } About if(cnt = = k) {//indicates that K row is selected and is eligible $memset (Row,0,sizeof(Row)); -Chessdfs (1);//put the K-pieces in the K-line that has been selected - } - } A Else { + for(inti =0; I <2; i++) {//each row has two states, select and not selected theChose[t] = i;//Chose[t] = 1 is selected for line T -Linedfs (t +1); $ } the } the } the the intMain () { - while(SCANF ("%d%d", &n, &k) = =2&& (n! =-1&& k! =-1)) { inmemset (Map,'!',sizeof(map)); thememset (Row,0,sizeof(Row)); the for(inti =1; I <= N; i++) { About GetChar (); the for(intj =1; J <= N; J + +) { theMAP[I][J] =GetChar (); the } + } -Ans =0; theLinedfs (1);//Select K rows from n rowsBayiprintf"%d\n", ans); the } the return 0; -}
POJ 1321 Checkerboard Issues (DFS + backtracking)