您的位置:首页 > 其它

poj 1579 Function Run Fun ( 记忆化搜索 )

2013-02-19 20:00 260 查看
其实这题和北航的oj上的一题一样,北航上的是汉语,题意就不说了,可以看一下北航上的翻译。

http://www.bianchengla.com/course/cpp/practise/problem?id=1243

直接递归会重复计算一些值,这样就会超时,最好的方法就是用记忆化搜索,用数组将值记录下来,当搜到已经计算过的值时直接使用就行了,避免再一次递归计算,这样会节省很多时间。

代码:

View Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#define  N 28
using namespace std ;

typedef long long ll ;

ll f

;

ll w( int a , int b , int c )
{
if ( a <= 0 || b <= 0 || c <= 0 )
return 1 ;
if ( f[a][b][c] > 0 )
return f[a][b][c] ;
else if ( a < b && b < c )
return w(a, b, c-1)+w(a, b-1, c-1)-w(a, b-1, c) ;
else
return w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1) ;
}

void init()
{
int i , j , k ;
memset( f , 0 , sizeof ( f )) ;
for ( i = 0 ; i <= 20 ; i++ )
{
for ( j = 0 ; j <= 20 ; j++ )
{
for ( k = 0 ; k <= 20 ; k++ )
f[i][j][k] = w( i , j , k ) ;
}
}
}

int main()
{
int cas , x , y , z ;

init() ;
while( cin>>x>>y>>z )
{
if ( x == -1 && y == -1 && z == -1 )
break ;
if ( x < 0 || y < 0 || z < 0 )
{
printf ( "w(%d, %d, %d) = %lld\n",x , y , z , f[0][0][0]) ;
}
else if ( x > 20 || y > 20 || z > 20 )
{
printf ( "w(%d, %d, %d) = %lld\n",x , y , z , f[20][20][20]) ;
}
else
{
printf ( "w(%d, %d, %d) = %lld\n",x , y , z , f[x][y][z]) ;
}
}
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: