【POJ 2279】Mr. Young’s Picture Permutations【线性DP】

题目:

有N个学生合影,站成左端对齐的k排,每排有 \(N-1,N_2,…N_k\)个人,第一排在最后面。学生的身高互不相同,分别为\(1-N\),并且合影时要求每一排从左往右身高递减,每一列从后往前身高递减,问有多少种安排合影的方案 。\(N <=30, k <=5\)


【POJ 2279】Mr. Young’s Picture Permutations【线性DP】_第1张图片

//此题默认 —— N1 >= N2 >= N3 >= ... >= Nk
#include 
#include 
#include 
#include 
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define LOG1(x1,x2) cout << x1 << ": " << x2 << endl;
#define LOG2(x1,x2,y1,y2) cout << x1 << ": " << x2 << " , " << y1 << ": " << y2 << endl;
typedef long long ll;
typedef double db;
const db EPS = 1e-9;
using namespace std;

int row[10],n;
ll dp[32][32][32][32][32];

int main()
{
	while(~scanf("%d",&n)){
		if(!n) break;
		memset(row,0,sizeof row);
		rep(i,1,n) scanf("%d",&row[i]);
		rep(a1,0,row[1])
			rep(a2,0,row[2])
				rep(a3,0,row[3])
					rep(a4,0,row[4])
						rep(a5,0,row[5]) dp[a1][a2][a3][a4][a5] = 0;
		dp[0][0][0][0][0] = 1;
		rep(a1,0,row[1])
			rep(a2,0,row[2]){
				if(a1 != row[1] && a2 > a1) continue;
				rep(a3,0,row[3]){
					if(a2 != row[2] && a3 > a2) continue;
					rep(a4,0,row[4]){
						if(a3 != row[3] && a4 > a3) continue;
						rep(a5,0,row[5]){
							if(a4 != row[4] && a5 > a4) continue;
							if(a1 < row[1])
								dp[a1+1][a2][a3][a4][a5] += dp[a1][a2][a3][a4][a5];
							if(a2 < row[2] && ((a2 < a1) || (a1 == row[1])))
								dp[a1][a2+1][a3][a4][a5] += dp[a1][a2][a3][a4][a5];
							if(a3 < row[3] && ((a3 < a2) || (a2 == row[2])))
								dp[a1][a2][a3+1][a4][a5] += dp[a1][a2][a3][a4][a5];
							if(a4 < row[4] && ((a4 < a3) || (a3 == row[3])))
								dp[a1][a2][a3][a4+1][a5] += dp[a1][a2][a3][a4][a5];
							if(a5 < row[5] && ((a5 < a4) || (a4 == row[4])))
								dp[a1][a2][a3][a4][a5+1] += dp[a1][a2][a3][a4][a5];
						}
					}
				}
			}
		printf("%lld\n",dp[row[1]][row[2]][row[3]][row[4]][row[5]]);
	}
	return 0;
}

你可能感兴趣的:(【POJ 2279】Mr. Young’s Picture Permutations【线性DP】)