棋盘问题 POJ - 1321(深搜水题)

题目:

Souce
https://vjudge.net/problem/POJ-1321
Problem
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放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

思路:
求方案数,按行搜索,记录已摆放棋子的列,递归至下一行(若有),至行末或在此之前棋子数量达到k,则ans++

详见代码:

//Data  2020:03:04
//Time 16ms
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
const int maxn = 1e4+10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int dz[] = {0, 0, 0, 0, 1,-1};
int dx[] = {1,-1, 0, 0, 0, 0};
int dy[] = {0, 0, 1,-1, 0, 0};
struct P {
    int z, x, y;
    P(int z = 0, int x = 0, int y = 0): z(z), x(x), y(y) {}
};
//int h, r, c, t;
//int s[55][55][55];
//int d[55][55][55];
//inline bool in_border (int mz, int mx, int my) {
//    return mx>=0 && mx<r && my>=0 && my<c && mz>=0 && mz<h;
//}
bool cmp (const void* p1, const void* p2) {
    return true;
}
int n, k, ans;
char s[10][10];
int vy[10];
int dfs(int i, int m) {
    if(m == k) return 1;
    if(i == n) return 0;

    for(int j = 0; j < n; j++)
        if(s[i][j] == '#' && !vy[j]) {
            vy[j] = 1;//标记列
            if(dfs(i+1, m+1)) ans++;
            vy[j] = 0;//记得取消标记
        }

    if(k-m < n-i && dfs(i+1, m))
        return 1;
    return 0;
}
int main() {//int T; cin >> T; getchar();
    while(cin >> n >> k && n > 0) {

        for(int i = 0; i < n; i++) cin >> s[i];
        //memset(vy, 0, sizeof(vy));
        ans = 0; dfs(0, 0);

        cout << ans << endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值