Paths through the Hourglass UVA10564


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
 
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;
using std::endl;

vector<int> st[45][25];
long long vis[45][25][501];
int arr[45][25];

int main()
{
	int n, S;
	while(scanf("%d%d", &n, &S), n || S)
	{
		int tn = n*2;
		memset(vis+1, 0, sizeof(vis[1])*tn);
		for(int i = 1; i <= n; ++i)
		{
			int lim = n-i+1;
			for(int j = 1; j <= lim; ++j)
			{
				scanf("%d", arr[i]+j);
				st[i][j].clear();
			}
		}
		for(int i = n+1; i < tn; ++i)
		{
			int lim = i-n+1;
			for(int j = 1; j <= lim; ++j)
			{
				scanf("%d", arr[i]+j);
				st[i][j].clear();
			}
		}
		for(int i = 1; i <= n; ++i)
			if(arr[tn-1][i] <= S)
			{
				++vis[tn-1][i][arr[tn-1][i]];
				st[tn-1][i].push_back(arr[tn-1][i]);
			}
		for(int i = tn-2; i >= n; --i)
		{
			int lim = i-n+1;
			int ti = i+1;
			for(int j = 1; j <= lim; ++j)
			{
				int ts = st[ti][j].size();
				for(int k = 0; k < ts; ++k)
				{
					int temp = st[ti][j][k];
					int t2 = temp+arr[i][j];
					if(t2 <= S)
					{
						if(!vis[i][j][t2])
							st[i][j].push_back(t2);
						vis[i][j][t2] += vis[ti][j][temp];
					}
				}
				int tj = j+1;
				ts = st[ti][tj].size();
				for(int k = 0; k < ts; ++k)
				{
					int temp = st[ti][tj][k];
					int t2 = temp+arr[i][j];
					if(t2 <= S)
					{
						if(!vis[i][j][t2])
							st[i][j].push_back(t2);
						vis[i][j][t2] += vis[ti][tj][temp];
					}
				}
			}
		}
		for(int i = n; i > 1; --i)
		{
			int lim = n-i+1;
			for(int j = 1; j <= lim; ++j)
			{
				int ts = st[i][j].size();
				for(int k = 0; k < ts; ++k)
				{
					int t1 = st[i][j][k];
					int t2 = t1+arr[i-1][j];
					if(t2 <= S)
					{
						if(!vis[i-1][j][t2])
							st[i-1][j].push_back(t2);
						vis[i-1][j][t2] += vis[i][j][t1];
					}
					t2 = t1+arr[i-1][j+1];
					if(t2 <= S)
					{
						if(!vis[i-1][j+1][t2])
							st[i-1][j+1].push_back(t2);
						vis[i-1][j+1][t2] += vis[i][j][t1];
					}
				}
			}
		}
		long long ans = 0LL;
		int s;
		for(int i = n; i >= 1; --i)
			if(vis[1][i][S])
			{
				s = i;
				ans += vis[1][i][S];
			}
		printf("%lld\n", ans);
		if(ans)
		{
			printf("%d ", s-1);
			int temp = S;
			for(int i = 1; i < n; ++i)
			{
				temp -= arr[i][s];
				if(s == 1)
				{
					putchar('R');
					continue;
				}
				if(s == n-i+1)
				{
					putchar('L');
					--s;
					continue;
				}
				if(vis[i+1][s-1][temp])
				{
					putchar('L');
					--s;
				}
				else
				{
					putchar('R');
				}
			}
			for(int i = n; i < tn-1; ++i)
			{
				temp -= arr[i][s];
				if(vis[i+1][s][temp])
				{
					putchar('L');
				}
				else
				{
					putchar('R');
					++s;
				}
			}
			putchar('\n');
		}
		else
			putchar('\n');
	}
	return 0;
}


你可能感兴趣的:(Paths through the Hourglass UVA10564)