kuangbin专题一 简单搜索 棋盘问题(POJ-1321)

棋盘问题

Time Limit: 1000MS Memory Limit: 10000K

Description

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


解题思路

思路一

这道题和经典的 N皇后 问题很像,而且数据比较小,很容易想到DFS搜索。与其不同的是,没有斜边的限制,但是初始情况下,棋盘上有些地方是可以摆放棋子,有些地方是不可以摆放的。

一开始我的想法是,把所有可以摆放棋子的棋盘区域全部存起来,再进行DFS搜索,开两个数组 row[]col[] 每次判断当前行和列是否已经存在棋子,然后加一个判断当前剩余棋盘区域是否小于还要放置的棋子个数的剪枝即可。

总的来说还是比较基础的DFS模板题,代码如下:

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 9;
char g[N][N];
bool row[N], col[N];
int n, k, res, cnt;   //res记录答案,cnt统计棋盘区域个数
pii p[N * N];         //录入所有的棋盘区域

//cur表示当前用到第cur个棋盘区域  sum表示当前摆放的棋子个数
void dfs(int cur, int sum){
    if(cnt - cur < k - sum) return;   //剪枝 当前剩余的个数小于还需要摆放的棋子个数
    if(sum > k){
        res ++ ;    //摆放合法 更新方案个数
        return;
    }

    int x = p[cur].x, y = p[cur].y;
    if(!row[x] && !col[y]){           //可以摆放
        row[x] = col[y] = true;
        dfs(cur + 1, sum + 1);
        row[x] = col[y] = false;      //回溯
    }

    dfs(cur + 1, sum);   //直接搜索下一个是否可行          
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    while(cin >> n >> k && !(n == -1 && k == -1)){
        res = cnt = 0;
        for(int i = 0; i < n; i ++ )
            for(int j = 0; j < n; j ++ ){
                cin >> g[i][j];
                if(g[i][j] == '#') p[ ++ cnt] = make_pair(i, j);
            }

        dfs(1, 1);

        cout << res << endl;
    }

    return 0;
}

思路二

后来发现其实不需要预先把所有可放棋子的棋盘区域存起来,只需要依次枚举每一列的棋子放在哪一行就可以了,每次判断一下枚举的列是否出界即可,所以有了以下代码。事实上,这种方式比我之前一开始想的要快一些。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 9;
char g[N][N];
bool row[N];
int n, k, res, cnt;   //res记录答案

//cur表示当前用到第cur列  sum表示当前摆放的棋子个数
void dfs(int cur, int sum){
    if(sum > k){
        res ++ ;    //摆放合法 更新方案个数
        return;
    }
    if(cur >= n) return;   //剪枝 出界

    for(int i = 0; i < n; i ++ )   //每一列的棋子放在哪一行
        if(!row[i] && g[i][cur] == '#'){
            row[i] = true;
            dfs(cur + 1, sum + 1);
            row[i] = false;
        }

    dfs(cur + 1, sum);   //直接搜索下一列的棋子,并非每一列都要放棋子          
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    while(cin >> n >> k && !(n == -1 && k == -1)){
        res = 0;
        for(int i = 0; i < n; i ++ ) cin >> g[i];

        dfs(0, 1);

        cout << res << endl;
    }

    return 0;
}
posted @ 2023-04-14 19:53  MarisaMagic  阅读(33)  评论(0编辑  收藏  举报