zoj DP(一)

zoj1013

#include <stdio.h>
#include <memory.h>

int trade[501][501], carry[501][501];
int min(int a, int b)
{
	return a<b?a:b;
}

int main()
{
	int iCase = 0;
	int n;
	while(scanf("%d", &n) && n)
	{
		if(iCase++) printf("\n");
		int w[4],s[4],d[4];
		int data;
		for (data=1; data<=3; data++)
			scanf("%d%d%d",	&w[data],&s[data],&d[data]);
		int c1,c2,c3,d4;
		scanf("%d%d%d%d", &c1,&c2,&c3,&d4);
		d4 -= c1 * d[1] + c2 * d[2] + c3 * d[3];
		memset(carry, 255, sizeof(carry));
		carry[0][0] = 0;
		int row = 0;
		int col = 0;
		int i, j, k,ja, ka;
		for(i = 0; i < n; i++)
		{
			int weight, size;
			scanf("%d%d", &weight, &size);
			memset(trade, 255, sizeof(trade));
			int newRow = row; 
			int newCol = col;
			int weight1, size1, weight2, size2;
			for(j = 0; j <= row; j++)
			for(k = 0; k <= col; k++)
				if(carry[j][k] >= 0)
					for(ja = j, weight1 = size1 = 0; (weight1 <= weight && size1<=size); weight1 += w[1], size1 += s[1], ja++)
					for(ka = k, weight2 = weight1, size2 = size1; (weight2 <= weight && size2 <= size); weight2 += w[2], size2 += s[2], ka++)
					{
						if(newRow < ja) newRow = ja;
						if(newCol < ka) newCol = ka;
						int bootWeight = (weight - weight2) / w[3];
						int bootSize = (size - size2) / s[3];
						if(bootWeight > bootSize) bootWeight = bootSize;
						bootWeight += carry[j][k];
						if(trade[ja][ka] < bootWeight) trade[ja][ka] = bootWeight;
					}
			memcpy(carry, trade, sizeof(trade));
			row = newRow; 
			col = newCol;
		}
		int iBest = 0;
		for(j = 0; j <= row; j++)
			for(k = 0; k <= col; k++)
				if(carry[j][k] >= 0)
				{
					int defend = j * d[1] + k * d[2] + carry[j][k] * d[3];
					int helms = j / c1; 
					int armors = k / c2; 
					int boots = carry[j][k] / c3;
					if(d4 > 0) defend += d4 * min(helms, min(armors, boots));
					if(iBest < defend) iBest=defend;
				}
		printf("Case %d: %d\n", iCase, iBest);
	}
	return 0;
}

zoj1027

#include<stdio.h> 
#define MaxN 101

char str1[MaxN],str2[MaxN]; 
char map[128];
int gene[MaxN][MaxN]; 

int main() { 
	map['A'] = 0;
	map['C'] = 1;
	map['G'] = 2;
	map['T'] = 3;
	map['-'] = 4;
int score[5][5] =  {{5, -1, -2, -1, -3}, 
					{-1, 5, -3, -2, -4}, 
					{-2, -3, 5, -2, -2},
					{-1, -2, -2, 5, -1}, 
					{-3, -4, -2, -1, 0}}; 
	int iCase;
	int i,j,k;
	int first,second; 
	scanf("%d ", &iCase); 
	for(k = 0 ; k < iCase ; k++) 
	{ 
		scanf("%d%s", &first, str1);
		scanf("%d%s", &second, str2);
		gene[0][0] = 0; 
		for(i = 1 ; i <= second;i++) 
			gene[0][i] = gene[0][i-1]+score[4][map[str2[i-1]]]; 
		for(i = 1 ; i <= first;i++) 
			gene[i][0] = gene[i-1][0]+score[map[str1[i-1]]][4]; 
		int m1,m2,m3;
		for(i = 1 ; i <= first;i++) 
			for(j = 1; j <= second ;j++) {
				m1 = gene[i-1][j] + score[map[str1[i-1]]][4]; 
				m2 = gene[i][j-1] + score[4][map[str2[j-1]]]; 
				m3 = gene[i-1][j-1] + score[map[str1[i-1]]][map[str2[j-1]]]; 
				int max = m1; 
				if(m2 > max) max = m2; 
				if(m3 > max) max = m3; 
				gene[i][j] = max; 
			} 
	printf("%d\n",gene[first][second]);		 
	} 
	return 0; 
}


zoj

#include<stdio.h>
#include<string.h>
#define N 101

int main()
{
	int a[N][N];
	int b[N];
	int n;
	int i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0; i<n; i++)
			for(j=0; j<n; j++)
				scanf("%d",&a[i][j]);
		int max = -32767;
		for(i=0; i<n; i++)
		{
			memset(b, 0, sizeof(b));
			for(j=i; j<n; j++)
			{
				int sum=0;
				for(k=0; k<n; k++)
				{
					b[k] += a[j][k];
					sum += b[k];
					if(sum<0) sum = b[k];
					if(sum>max) max = sum;
				}
			}
		}
		printf("%d\n",max);
	}
	return 0;
}

zoj1093

#include<stdio.h>
#define MAX 9999999
int box[100][3];
int height[100];
int number;

void oriente(int serial,int a,int b,int c)
{
	box[serial][0]=a;
	box[serial][1]=b;
	box[serial][2]=c;
}

int DP(int serial)
{
	if(height[serial]!=-1)
		return height[serial];
	if(serial>number)
		return 0;
	int t=0;
	int i;
	for(i=1;i<=number;++i)
	{
		if((box[serial][0]>box[i][0]&&box[serial][1]>box[i][1])||(box[serial][0]>box[i][1]&&box[serial][1]>box[i][0]))
			t=DP(i)+box[i][2];
		if(t>height[serial])
			height[serial]=t;
	}
	return height[serial];
}


int main()
{
	int n;
	int cases=0;
	int a,b,c;
	while(scanf("%d",&n)&&n)
	{
		box[0][0]=box[0][1]=box[0][2]=MAX;
		number=0;
		int i;
		for(i=0;i<n;++i)
		{
			scanf("%d%d%d",&a,&b,&c);
			oriente(++number,a,b,c);
			oriente(++number,b,c,a);
			oriente(++number,c,a,b);
		}
		for(i=0;i<=number;++i)
			height[i]=-1;
		printf("Case %d: maximum height = %d\n",++cases,DP(0));
	}
	return 0;
}

zoj1100

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

double b[13][2100];
int tran[14000][2];

int H, W, maxMove, nTran;

void dfs (int n, int from, int to)
{
	if (n > W) return;
	if (n == W)
	{
		tran[nTran][0] = from;
		tran[nTran ++][1] = to;
		return;
	}
	dfs (n+2, (from<<2)+3, (to<<2)+3);
	dfs (n+1, (from<<1)+1, to<<1);
	dfs (n+1, from<<1, (to<<1)+1);
}

void dp ()
{
	memset (b, 0x00, sizeof (b));
	b[0][(1<<W) - 1] = 1;
	int i, j;
	for (i = 0; i < H; i ++)
		for (j = 0; j < nTran; j ++)
			b[i+1][tran[j][1]] += b[i][tran[j][0]];
}

int main ()
{
	while (scanf("%d%d", &H, &W) && H)
	{
		int temp;
		if (H < W)
		{
			temp = H; H = W; W = temp;
		}
		nTran = 0;
		dfs (0, 0, 0);
		dp ();
		printf("%.0f\n", b[H][(1<<W) - 1]);
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值