[Blue Bridge Cup] [2015 Sixth Zhenti] robot tower
Time limit: 1Sec Memory Limit: 128MB submitted: 2 Solution: 2
Title Description
Planet X robot has two performances cheerleaders clothing, A and B.
They show that the robots take the tower.
Similarly:
A
BB
the ABA
the AABB
BBBAB
ABABBA
set of rules column squad is:
A can standing on the shoulders 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.
Entry
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.
Export
A required output integer representing the number of types of patterns can be generated.
Sample input
1 2
Sample Output
3
Ideas:
DFS programming skills, violence enumerate all cases.
#include<iostream>
#include<cstring>
using namespace std;
char map[509][509];
int ans;
void dfs( int row , int col ,int A,int B ){
if( A<0 || B<0 ){
return ;
}
if( A==0 && B==0 ){
ans++;
}
if( row == 1 ){
map[row][col] = 'A';
dfs( row+1,1,A-1,B);
map[row][col] = 'B';
dfs( row+1,1,A,B-1);
}
else {
//一个一个地枚举呗。
if( col ==1 ){
map[row][col] = 'A';
dfs(row,col+1,A-1,B);
map[row][col] = 'B';
dfs(row,col+1,A,B-1);
}
else {
if( map[row-1][col-1] =='A' ){
if( map[row][col-1] == 'A'){
map[row][col] = 'A';
if( col == row )
dfs(row+1,1,A-1,B);
else if( col < row ){
dfs( row,col+1,A-1,B);
}
}
else if( map[row][col-1] == 'B'){
map[row][col] = 'B';
if( col == row )
dfs(row+1,1,A,B-1);
else if( col < row ){
dfs( row,col+1,A,B-1);
}
}
}
else if(map[row-1][col-1] =='B' ){
if( map[row][col-1] == 'A'){
map[row][col] = 'B';
if( col == row )
dfs(row+1,1,A,B-1);
else if( col < row ){
dfs( row,col+1,A,B-1);
}
}
else if( map[row][col-1] == 'B'){
map[row][col] = 'A';
if( col == row )
dfs(row+1,1,A-1,B);
else if( col < row ){
dfs( row,col+1,A-1,B);
}
}
}
}
}
}
int main(void){
int n,m;
while( scanf("%d%d",&m,&n)!=EOF){
memset(map,0,sizeof(map));
ans=0;
dfs(1,1,m,n);
printf("%d\n",ans);
}
return 0;
}