zoukankan      html  css  js  c++  java
  • POJ 1321 棋盘问题(dfs八皇后变形)

    棋盘问题
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 25147   Accepted: 12424

    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

    解题思路:

      类似于八皇后,只不过对于皇后的数目有了要求,八皇后中,一定要去皇后的数目==棋盘的维数,但是这里,棋子的个数!=棋盘的维数,所以,我们

    要采用不同的方式来维护。

      int x = pos/n;

      int y = pos%n;

    这在处理有关二维的dfs问题中,很常见,也很好用,通过这样的方法,我们就能控制每次棋子的移动位置,然后分别判断row[]和col[]和grid[][]来决定

    是不是放棋子,如果能放入棋子的话,我们就说标记下->dfs->回溯。

    代码:

    # include<cstdio>
    # include<iostream>
    # include<algorithm>
    # include<cstring>
    # include<string>
    # include<cmath>
    # include<queue>
    # include<stack>
    # include<set>
    # include<map>
    
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    
    # define inf 999999999
    # define MAX 10
    
    int col[MAX];
    int row[MAX];
    char grid[MAX][MAX];
    int n,k;
    int ans;
    
    void input()
    {
        getchar();
        for ( int i = 0;i < n;i++ )
        {
            for ( int j = 0;j < n;j++ )
            {
                char ch = getchar();
                if ( ch == '#' )
                {
                    grid[i][j] = 1;
                }
                else
                {
                    grid[i][j] = 0;
                }
            }
            getchar();
        }
    }
    
    void dfs( int pos,int step )
    {
        if ( step == k )
        {
            ans++;
            return;
        }
        while ( pos < n*n )
        {
            int x = pos/n;
            int y = pos%n;
            if ( grid[x][y]==1&&col[y]==0&&row[x]==0 )
            {//判断是否可以放棋子
                row[x] = col[y] = 1;//对棋子所放的行和列做标记
                dfs ( pos+1,step+1 );
                row[x] = col[y] = 0;//解除标记,这样才能在新位置上放棋子
            }
            pos++;
        }
    }
    
    
    int main(void)
    {
        while ( cin>>n>>k )
        {
            if ( n==-1&&k==-1 )
            {
                break;
            }
            memset(col,0,sizeof(col));
            memset(row,0,sizeof(row));
            input();
            ans = 0;
            dfs(0,0);
            cout<<ans<<endl;
        }
    
    
    	return 0;
    }
    

      

    代码:

  • 相关阅读:
    vue-router重写push方法,解决相同路径跳转报错
    Vue中的权限管理怎么做
    linux启动过程中建立临时页表
    用redis当作LRU缓存
    用IPV6隧道连接IPV4孤岛
    redis协议规范
    nginx的脚本引擎(二)rewrite
    nginx的脚本引擎(一)
    nginx的变量系统
    Singleton
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4382599.html
Copyright © 2011-2022 走看看