棋盘问题 POJ1321

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。
n <= 8 , k <= n 当为-1 -1时表示输入结束。 随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, .
表示空白区域(数据保证不出现多余的空白行或者空白列)。

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input
2 1
# .
.#
4 4
…#
..#.
.#..
#…
-1 -1
Sample Output
2
1

新做了一遍。思路清晰多了

新代码

 * File Name: 7_26.cpp
 * Author: Sequin
 * mail: Catherine199787@outlook.com
 * Created Time: 三  7/26 08:27:23 2017
 *************************************************************************/


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <ctype.h>
#include <set>
#include <vector>
#include <cmath>
#include <bitset>
#include <algorithm>
#include <climits>
#include <string>
#include <list>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <sstream>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define pi acos(-1.0)
#define pii pair<int,int>
#define ll long long
#define MAX 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
//ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n, k;
int ret;
bool canset[10];
char mmap[10][10];

int dfs(int chess, int border){
    if(chess > k){
        ret++;
        return 0;
    }

    int i, j;
    UP(i, border, n){
        UP(j, 1, n){
            if(canset[j] && mmap[i][j] == '#'){
                canset[j] = false;
                dfs(chess+1, i+1);
                canset[j] = true;
            }
        }
    }
    return 0;
}


int main(){

    //ios::sync_with_stdio(false);

    while(~scanf("%d%d", &n, &k) && n != -1 && k != -1){
        MEM(canset, 1);
        MEM(mmap, 0);
        ret = 0;
        int i;
        UP(i, 1, n){
            scanf("%s", mmap[i] + 1); //!!!!!
        }
        dfs(1, 1);
        printf("%d\n", ret);
    }
}

二刷的时候答案一直错误,最后才发现是把边界条件判断放在了最后一个棋子之前,但其实用应该用if else而不是if if

旧代码

#include <cstdio>
#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
using namespace std;
char map[8][8]; //图
int col[8]; //列数,记录可以放的列
int chess, count; // chess 当前已放置棋子数目, count 棋子放置方法数
int n, k;

void dfs(int row){

    if(chess == k){ //已经放了k个棋子,说明所有的棋子都已经放好了
        count++; //方式+1
        return;
    }
    if(row >= n){ //超出边界
        return;
    }

    for(int i = 0; i < n; i++){
        if(map[row][i] == '#' && col[i] == 0){ //如果当前row行i列为棋盘区域并且第i列可以放置
            col[i] = 1; //更改列状态
            chess++; //处理下一个棋子
            dfs(row + 1); //搜索下一行
            col[i] = 0; //搜索返回列状态清空
            chess--; //搜索返回开始处理当前棋子
        }
    }
    dfs(row + 1);//此行不能放置,开始下一行
}



int main(){

    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);

    while(cin >> n >> k && n != -1 && k != -1){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                cin >> map[i][j];
            }
        }
        for(int i = 0; i < n; i++){
            col[i] = 0;
        }
        chess = 0;
        count = 0;
        dfs(0);
        cout << count << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值