http://poj.org/problem?id=1321
Watch out for the pieces in the ' # ' place.
Matrix size But 8*8, even 8! The time complexity is enough to withstand, can be directly DFS Solver
The rows and columns at the current point of the label are accessed at DFS, and a new point is searched that has not been accessed by the column, note that the rows and columns of the current point are not accessed after the search is complete
#include <cstdio> #include <cstring>using namespace std;int n,k;char maz[8][9];int e[8][8],len[8];bool r[8] , c[8];void dye (int x,int y) {r[x]=c[y]=true;} void Undye (int x,int y) {r[x]=c[y]=false;} int dfs (int s,int cnt) {if (cnt<=0) return 1; int ans=0; for (int i=s+1;cnt+i<=n;i++) {for (int j=0;j<len[i];j++) {if (r[i]| | C[E[I][J]]) continue; Dye (i,e[i][j]); Ans+=dfs (i,cnt-1); Undye (I,e[i][j]); }} return ans; int main () {while (scanf ("%d%d", &n,&k) ==2&&n!=-1) {for (int i=0;i<n;i++) scanf ("%s", Maz[i]); memset (len,0,sizeof (len)); Memset (r,0,sizeof (R)); Memset (C,0,sizeof (c)); for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (maz[i][j]== ' # ') { E[i][len[i]++]=j; }}} if (k==0) {puts ("0"); Continue } int ans=0; for (int i=0;k+i<=n;i++) {for (int j=0;j<len[i];j++) {dye (i,e[i] [j]); Ans+=dfs (i,k-1); Undye (I,e[i][j]); }} printf ("%d\n", ans); } return 0;}
POJ 1321 Board Problem DFS difficulty: 0