poj1321(回溯板子)

来源:互联网 发布:js获取div的内容 编辑:程序博客网 时间:2024/06/04 19:46

该题存为板子

描述:点击打开链接

/*solution:简单回溯note;注意一定要考虑摆放时一定要按照行递增或者列递增顺序进行摆放。否则可能出现解重复的情况。date:2016.8.28*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;bool col_tag[10];int n, k, ans;char G[10][10];void dfs(int r, int cnt) {if(cnt == k)ans++;else {for(int i = r+1; i < n; i++) {for(int j = 0; j < n; j++) {if(G[i][j] == '#' && !col_tag[j]) {col_tag[j] = true;dfs(i, cnt+1);col_tag[j] = false;}}}}}int main(){//freopen("in.txt", "r", stdin);    while(~scanf("%d%d", &n, &k) && n != -1) {memset(col_tag, 0, sizeof(col_tag));for(int i = 0; i < n; i++)scanf("%s", G[i]);ans = 0;for(int i = 0; i <= n-k; i++) {for(int j = 0; j < n; j++) {if(G[i][j] == '#') {col_tag[j] = true;dfs(i, 1);col_tag[j] = false;}}}printf("%d\n", ans);    }    return 0;}



0 0
原创粉丝点击