[Blue Bridge Cup] [2015 Sixth Zhenti] robot tower (DFS)

Title Description
Planet X robot has two performances cheerleaders clothing, A and B.
They show that the robots take the tower.

similar:

 A
B B

A B A
A A B B
B B B A B
A B A B B A

Tower Group rules squad is:

A shoulder can only stand of AA or BB.
B can stand on the shoulders of the AB or BA.

Your task is to help the cheerleaders calculate, when the number of B given A, how many tricks can be composed of the tower.

Line input two integers M and N, separated by spaces (0 <M, N <500), respectively, represent the number of A, B, to ensure the number of rationality.

A required output integer representing the number of types of patterns can be generated.
Input
Input line two integers M and N, separated by spaces (0 <M, N <500 ), respectively, represent the number of A, B, to ensure the number of rationality.
Output
required output an integer representing the number of types of patterns can be generated.
Sample input
12
sample output
3
ideas: We start from the top down analysis, for strings the order of the line, if the beginning is determined, then the latter will also determined, and therefore classified discussions on it.
code show as below:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n,m;

inline ll dfs(int a,int b,int len,string s)
{
	if(a==0&&b==0) return 1ll;
	if(len==0) return dfs(a-1,b,1,"A")+dfs(a,b-1,1,"B");
	else
	{
		string t="";
		int c=a,d=b;
		ll sum=0;
		if(s[0]=='A') 
		{
			if(c>=2)
			{
				t+="AA";c-=2;
				for(int i=1;i<s.length();i++) 
				{
					if(s[i]=='A'&&t[i-1]=='A'&&c) t+='A',c--;
					else if(s[i]=='A'&&t[i-1]=='B'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='A'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='B'&&c) t+='A',c--;
				}
				if(t.length()==len+1) sum+=dfs(c,d,len+1,t);
				c=a,d=b,t="";
			}
			if(d>=2)
			{
				t+="BB";d-=2;
				for(int i=1;i<s.length();i++) 
				{
					if(s[i]=='A'&&t[i-1]=='A'&&c) t+='A',c--;
					else if(s[i]=='A'&&t[i-1]=='B'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='A'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='B'&&c) t+='A',c--;
				}
				if(t.length()==len+1) sum+=dfs(c,d,len+1,t);
				c=a,d=b,t="";
			}
		}
		c=a,d=b,t="";
		if(s[0]=='B')
		{
			if(c&&d)
			{
				t+="AB";c--,d--;
				for(int i=1;i<s.length();i++) 
				{
					if(s[i]=='A'&&t[i-1]=='A'&&c) t+='A',c--;
					else if(s[i]=='A'&&t[i-1]=='B'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='A'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='B'&&c) t+='A',c--;
				}
				if(t.length()==len+1) sum+=dfs(c,d,len+1,t);
				c=a,d=b,t="";
				t+="BA";c--,d--;
				for(int i=1;i<s.length();i++) 
				{
					if(s[i]=='A'&&t[i-1]=='A'&&c) t+='A',c--;
					else if(s[i]=='A'&&t[i-1]=='B'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='A'&&d) t+='B',d--;
					else if(s[i]=='B'&&t[i-1]=='B'&&c) t+='A',c--;
				}
				if(t.length()==len+1) sum+=dfs(c,d,len+1,t);
				c=a,d=b,t="";
			}
		}
		return sum;
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		printf("%lld\n",dfs(n,m,0,""));
	}
	return 0;
}

To refuel a ah, ( O ) / ~

Published 599 original articles · won praise 49 · views 40000 +

Guess you like

Origin blog.csdn.net/starlet_kiss/article/details/105224492