Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的改进版,0MS~~

Posted by majuncheng at 2009-08-23 16:45:54 on Problem 1088
/*
#include <cstdio>



int T[11],Coins[11],n;

int c[20002],c1[20002];


int main()
{
	int m,i,j,sum=0;
	scanf("%d%d",&n,&m);
		for (i=0;i<n;i++)
			scanf("%d",&T[i]);
		for (i=0;i<n;i++)
		{
			scanf("%d",&Coins[i]);
			sum+=Coins[i]*T[i];
		}
		for (i=1;i<=sum;i++)
		{
			c[i]=0xfffffff;
			c1[i]=0xfffffff;
		}
		c[0]=0;c1[0]=0;
		for ( i=0;i<n;i++)
			for ( j=1;j<=Coins[i];++j)
				for (int k=sum;k>=0;--k)
					if (k-T[i]>=0)
					if (c[k-T[i]]+1 < c[k])
					{ c[k]=c[k-T[i]]+1; }

		int min=0xfffffff;

		for ( i=0;i<n;i++)
			for ( j=1;j<=Coins[i];++j)
				for (int k=0;k<=sum-m;++k)
					while (k-T[i]>=0 && c1[k-T[i]]+1 < c1[k])
					{ c1[k]=c1[k-T[i]]+1; }
       

		for (i=m;i<sum;i++)
			if (c[i] != 0xfffffff && c1[i-m]!= 0xfffffff)
				if (c[i]+c1[i-m]< min)
				{min=c[i]+c1[i-m];}
			
		if (min==0xfffffff) printf("-1\n");
		else printf("%d\n",min);
		
		


	return 0;
}*/

#include <cstdio>

int height[102][102]; //高度
int wt[102][102];  //记录各点到达的最长长度
int r,c;  //行数和列数

int DP(int i,int j)
{
	if (wt[i][j])
		return wt[i][j];    //如果已经递归过,长度值就不为初始值(0)了,避免重复运算
	int max=0;
	int p,h=height[i][j];     //利用递归找到邻近点中长度最长的
	if ((p=i-1)>=0 && height[p][j]<h)
		if ( DP(p,j)>max)
			max=DP(p,j);
	if ((p=i+1)<r && height[p][j]<h)
		if (DP(p,j)>max)
			max=DP(p,j);
	if ((p=j-1)>=0 && height[i][p]<h)
		if (DP(i,p)>max)
			max=DP(i,p);
	if ((p=j+1)<c && height[i][p]<h)
		if (DP(i,p)>max)
			max=DP(i,p);
	wt[i][j]=max+1;      //这里使用了和上面楼主一样的技巧,如果四个更新后max仍为0,会被赋以初始值
	return wt[i][j];
}

int main()
{
	scanf ("%d%d",&r,&c);
	int max=0;
	for (int i=0;i<r;++i)
		for (int j=0;j<c;++j)
		{
			scanf("%d",&height[i][j]);
			wt[i][j]=0;
		}
	for (int i=0;i<r;++i)
		for (int j=0;j<c;++j)
			if (DP(i,j)>max)
				max=DP(i,j);
	printf("%d\n",max);
	
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator