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;
}
發佈了160 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章