POJ 1597 Function Run Fun

Title description

Insert picture description here

Typical examples of memoized search

This question is a simple recursive function, but just write it according to the meaning of the question. TLE
will need to create a three-dimensional array to store the results, and use memory search to AC

#include<iostream>
#include<cstring>
using namespace std;
int f[21][21][21];
int w(int a,int b,int c){
    
    
	if(a<=0||b<=0||c<=0)
	return 1;
	else if(a>20||b>20||c>20)
	return w(20,20,20);
	else if(f[a][b][c]) return f[a][b][c];
	else if(a<b&&b<c) return f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
	else return f[a][b][c]=w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
}
int main(){
    
    
	memset(f,0,sizeof(f));
	int a,b,c;
	while(cin>>a>>b>>c){
    
    
		if(a==-1&&b==-1&&c==-1)
		return 0;
		cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
	}
}

Guess you like

Origin blog.csdn.net/qaqaqa666/article/details/112848478