pku 1321 棋盘覆盖问题

准时在11点AC!!hah

 

#include<iostream> char map[9][9]; bool flag[9][9]; int num,n,k; bool place(int r,int c) { if (map[r][c] != '#') { return false; } for(int i = 1; i <= n; i++) { if(flag[r][i] || flag[i][c]) return false; } return true; } void dfs(int r,int count) { if (r > n + 1) { return ; } else{ if(count == k) { num++; return; } //上界剪枝 //前i-1行+当前行+剩下的行都选 if(count + 1 + n - r < k) return; for(int i = r; i <= n; i++) for(int j = 1; j <= n; j++) { if(place(i,j)) { flag[i][j] = true; //printf("(%d , %d) : %c/n",i,j,map[i][j]); dfs(i + 1, count + 1); //回溯 flag[i][j] = false; } } } } int main() { //freopen("input.txt","r",stdin); int i,j; while(1) { scanf("%d%d/n",&n,&k); if(n == -1) break; num=0; memset(flag, false, sizeof(flag)); memset(map, '/0', sizeof(map)); for(i = 1;i <= n; i++){ for (j = 1; j <= n; j++) scanf("%c",&map[i][j]); char temp; scanf("%c",&temp); } /* for(i= 1;i <= n;i++){ for (j = 1; j <= n;j++) printf("%c ",map[i][j]); printf("/n"); } */ dfs(1,0); printf("%d/n",num); } return 0; }

你可能感兴趣的:(pku 1321 棋盘覆盖问题)