zoukankan      html  css  js  c++  java
  • 棋盘问题 分类: 搜索 POJ 2015-08-09 13:02 4人阅读 评论(0) 收藏

    棋盘问题
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 28474 Accepted: 14084

    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

    Source
    蔡错@pku
    对于有限制条件的可以改变一个条件,判断另一个条件,我们可以遍历每一行去判断相应的列是否符合条件

    #include <map>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-9
    #define LL long long
    #define PI acos(-1.0)
    #define INF 0x3f3f3f3f
    #define CRR fclose(stdin)
    #define CWW fclose(stdout)
    #define RR freopen("input.txt","r",stdin)
    #define WW freopen("output.txt","w",stdout)
    
    const int MAX = 10000;
    
    int Dir[][2]= {{1,0},{0,-1},{-1,0},{0,1}};
    
    typedef pair <int ,int >p;
    
    char Map[15][15];
    
    bool H[15];
    
    int n,m;
    
    int sum;
    
    void DFS(int i,int num)
    {
        if(num==m)
        {
            sum++;
            return ;
        }
        if(i==n)
        {
            return;
        }
        for(int j=0;j<n;j++)
        {
            if(Map[i][j]=='#'&&!H[j])
            {
                H[j]=true;
                DFS(i+1,num+1);
                H[j]=false;
            }
        }
        DFS(i+1,num);
    }
    int main()
    {
        while(~scanf("%d %d",&n,&m))
        {
            if(n==-1&&m==-1)
            {
                break;
            }
            for(int i=0;i<n;i++)
            {
                scanf("%s",Map[i]);
    
            }
            memset(H,false,sizeof(H));
            sum=0;
            for(int i=0;i<n;i++)
            {
                if(Map[0][i]=='#')
                {
                    H[i]=true;
                    DFS(1,1);
                    H[i]=false;
                }
    
            }
            DFS(1,0);
            printf("%d
    ",sum);
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    diy_markdown 的 html 显示
    根据 vuex 的 this.$store.dispatch() 返回值 处理逻辑
    vue 项目配置: 局域网 ip 发布
    vue-markdown 之 markdown-it, 以及 table of content 的实现:markdown-it-toc-and-anchor
    程序员面试金典-面试题 08.05. 递归乘法
    程序员面试金典-面试题 08.04. 幂集
    程序员面试金典-面试题 08.03. 魔术索引
    程序员面试金典-面试题 08.02. 迷路的机器人
    程序员面试金典-面试题 08.01. 三步问题
    程序员面试金典-面试题 05.08. 绘制直线
  • 原文地址:https://www.cnblogs.com/juechen/p/4721909.html
Copyright © 2011-2022 走看看