zoukankan      html  css  js  c++  java
  • poj 1321 棋盘问题

    题目链接http://poj.org/problem?id=1321

    题意: n*n的棋盘,放k个棋子,#可以放棋子  .不可以放棋子  每行每列都只能有一个棋子,求方案数

    分类:DFS

    注意点:2的31次方是2147483648,是超过int范围的

    代码

    ///#include<bits/stdc++.h>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    
    using namespace std;
    
    #define LL __int64
    
    #define PI 3.1415926535898
    
    const LL inf=1000000007;
    
    int n,k;
    
    char ch[10][10];
    
    bool hang[10];
    bool lie[10];
    
    int sum_k,temp_ans;
    
    LL ans;
    
    void dfs(int x,int temp_k)
    {
        if(temp_k==k)
        {
            ans++;
        }
        for(int i=x;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(ch[i][j]=='.')
                    continue;
                if(hang[i]==1||lie[j]==1)
                    continue;
                
                if(ch[i][j]=='#')
                {
                    hang[i]=1;
                    lie[j]=1;
                    temp_k++;
                   
                    dfs(i+1,temp_k);
    
                    temp_k--;
                    hang[i]=0;
                    lie[j]=0;
                    ch[i][j]='#';
                }
            }
        }
    }
    
    int main()
    {
        while(scanf("%d %d",&n,&k)&&!(n==-1&&k==-1))
        {
            ans=0;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    cin>>ch[i][j];
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    memset(hang,0,sizeof(hang));
                    memset(lie,0,sizeof(lie));
                    int sumk=0;
                    if(ch[i][j]=='#')
                    {
                        ch[i][j]='.';
                        hang[i]=1;
                        lie[j]=1;
                        sumk++;
                        dfs(i+1,sumk);
                    }
                }
            }
    
            cout<<ans<<endl;
    
        }
        return 0;
    }
    anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
  • 相关阅读:
    ICMPv6 Type 和 rfc
    Redis学习
    Vue学习(一)
    《一线架构师实践指南》读后感(五)
    《一线架构师实践指南》读后感(四)
    Stream流
    泛型
    《架构漫谈》读后感
    《一线架构师实践指南》读后感(三)
    《一线架构师实践指南》读后感(二)
  • 原文地址:https://www.cnblogs.com/gaoss/p/4909557.html
Copyright © 2011-2022 走看看