递归 DFS

#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>

int sum = 0;

char ch[3] = {'L', 'R','P'};

void DFS(int x, int n, int t )
{
int i;

if (n == t) {
sum
+= x;
return ;
}
for (i = 0; i < 3; i++)
if( ch[i] == 'L')
DFS(
2 * x , n + 1, t);
else if ( ch[i] == 'R')
DFS(
2 * x + 1, n + 1, t);
else if ( ch[i] == 'P')
DFS(x, n
+ 1, t);
}

int main( )
{
int i, j, len;
char ch[20];
while (scanf("%s", ch) != EOF)
{
len
= strlen(ch);
DFS(
1, 0, len);
printf(
"%d\n",sum);
sum
= 0;
}
return 0;
}

posted on 2011-08-11 17:02  more think, more gains  阅读(208)  评论(0编辑  收藏  举报

导航