NYOJ--491--dfs(打表水过)--幸运三角形

/*
    Name: NYOJ--491--幸运三角形
    Author: shen_渊 
    Date: 15/04/17 16:26
    Description: DFS超时,深搜出第一行的所有情况,计算之后打表水过 
                0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757
*/
#include<iostream>
#include<cstring>
using namespace std;
const char ADD = '+';
const char SUB = '-';
void dfs(int);
void cal();
int n,counter;
string str;
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    while(cin>>n){
        counter = 0;
        str = "";
        dfs(0);
        cout<<counter<<endl;
    }
    return 0;
}
void dfs(int ct){
    if(ct == n){
        cal();
        return;
    }else{
        str[ct] = ADD;
        dfs(ct+1);
        str[ct] = SUB;
        dfs(ct+1);
    }
}
void cal(){
    int t = n;
    int suma=0,sums=0;
    while(t){
        int i;
        for(i=0; i<t; ++i){
            if(str[i] == ADD)suma++;
            else sums++;
        }
        for(i=0; i<t-1; ++i){
            if(str[i] == str[i+1])str[i] = ADD;
            else str[i] = SUB;
        }
        t--;
    }
    if(suma == sums)counter++;
} 
#include<iostream>
using namespace std;
int arr[20] = {0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757};
int main()
{
    int n;
    ios::sync_with_stdio(false);
    while(cin>>n){
        cout<<arr[n]<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/slothrbk/p/7251884.html