POJ-1321-棋盘问题

来源:互联网 发布:淘宝雪纺衫花色衬衣 编辑:程序博客网 时间:2024/06/02 06:41

解题报告:

1、逐行放置,则仅需考虑列是否冲突。

2、注意在出口处恢复被修改的值。

AC代码:

#include <iostream>#include <cstring>using namespace std;int n, k, ans;int col[10];char chess[10][10];void dfs(int row, int num){if(num == k){ans++;return;}for(int i = row+1; i < n; i++)for(int j = 0; j < n; j++)if(chess[i][j]=='#' && !col[j]){col[j] = 1;dfs(i, num+1);col[j] = 0;}}int main(void){while(cin>>n>>k && n!=-1){ans = 0;memset(col, 0, sizeof(col));for(int i = 0; i < n; i++)cin >> chess[i];dfs(-1, 0);cout << ans << endl;}return 0;}


0 0
原创粉丝点击