zoukankan      html  css  js  c++  java
  • 棋盘问题 ( POJ -1321 )(简单DFS)

    转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82684942   作者:Mercury_Lc

    题目链接

    题解:dfs入门,就是每个点都搜索一下,什么时候够了k个就ans++。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <map>
    #include <vector>
    using namespace std;
    
    int n,k;
    int ans = 0;
    int step = 0;
    char mp[55][55];
    bool vis[55];
    
    void dfs(int x)
    {
        int i;
        if(step == k)
        {
            ans ++;
            return ;
        }
        if(x >= n)
            return ;
        for(i = 0; i < n; i ++)
        {
            if(!vis[i] && mp[x][i] == '#')
            {
                step ++;
                vis[i] = true;
                dfs(x + 1);
                vis[i] = false;
                step --;
            }
        }
        dfs(x + 1);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&k))
        {
            if(n == -1 && k == -1)
                return 0;
            for(int i = 0; i < n; i ++)
            {
                getchar();
                scanf("%s",&mp[i]);
            }
            ans = 0;
            step = 0;
            memset(vis,0,sizeof(vis));
            dfs(0);
            printf("%d
    ",ans);
        }
        return 0;
    }
    

    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

  • 相关阅读:
    php判断远程图片是否防盗链
    php获取远程图片url生成缩略图的方法
    qq zone g_tk
    zend studio aptana
    qq音乐接口
    function https_request
    Eclipse 汉化
    php 邮箱替换*
    获取顶级域名函数
    weixin oauth api 使用
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139539.html
Copyright © 2011-2022 走看看