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

帖个贪心的代码

Posted by rxtld1028 at 2012-12-16 08:48:55 on Problem 1088
#include<algorithm>
using namespace std;
#include<stdio.h>
int **N,R,C,num[105][105],T[105][105];
void getxy(int &x, int &y,int n)
{
        x=n/C;
	y=n-n/C*C;
}
bool cmp(int *a,int *b)
{
	return a[0]<b[0];
}
int main()
{
	int i,j;
	N=new int*[10005];
	for(i=0;i<10005;i++)
	{
		N[i]=new int[2];
	}
   scanf("%d %d",&R,&C);
	
    for(i=0;i<R;i++)
		for(j=0;j<C;j++)
		{
			scanf("%d",&N[i*C+j][0]);
            N[i*C+j][1]=i*C+j;
			T[i][j]=N[i*C+j][0];
		}
		sort(N,N+R*C,cmp);
		int max=0;
	for(i=0;i<R*C;i++)
	{
       int x,y;
	   getxy(x,y,N[i][1]);
	   if(x>0)
	   {
		   if(T[x-1][y]>T[x][y]&&num[x-1][y]<=num[x][y])
		   {
			   num[x-1][y]=num[x][y]+1;
			   if(max<num[x-1][y])
				   max=num[x-1][y];
		   }
	   }
	   if(x<R-1)
	   {
		   if(T[x+1][y]>T[x][y]&&num[x+1][y]<=num[x][y])
		   {
			   num[x+1][y]=num[x][y]+1;
			   if(max<num[x+1][y])
				   max=num[x+1][y];
		   }
	   }
	   if(y>0)
	   {
		   if(T[x][y-1]>T[x][y]&&num[x][y-1]<=num[x][y])
		   {
			   num[x][y-1]=num[x][y]+1;
			   if(max<num[x][y-1])
				   max=num[x][y-1];
		   }
	   }
	   if(y<C-1)
	   {
		   if(T[x][y+1]>T[x][y]&&num[x][y+1]<=num[x][y])
		   {
			   num[x][y+1]=num[x][y]+1;
			   if(max<num[x][y+1])
				   max=num[x][y+1];
		   }
	   }
	}
	printf("%d\n",max+1);
	
	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