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 hawk at 2005-03-02 12:23:57 on Problem 1088
In Reply To:想不出我的代码为什么会超时...希望大家多多指教 Posted by:lc86 at 2005-03-02 12:15:02
> #include <stdio.h>
> #include <memory.h>
> #include <stdlib.h>
> #include <malloc.h>
> #define map(x,y) *(map+(x)*n+(y));
> int *map;
> int n,m;
> int main()
> {
>  int *value,*map,real;
>  int i,j,x,y,ii,jj,b=0,k;
>  scanf("%d%d",&n,&m);
>  map=(int *)malloc(n*m*sizeof(int));
>  value=(int *)malloc(n*m*sizeof(int));
>  memset(value,0,n*m*sizeof(int));
>  for(i=0;i<n*m;i++)
>    scanf("%d",map+i);
>  b=1;
>  while(b)
>  {
>   b=0;ii=0;
>   for(i=0;i<m;i++)
>   {
>    for(j=0;j<n;j++,ii++)
>       for(k=0;k<4;k++)
> 	{
> 	 x=j;y=i;
> 	 switch(k)
> 	 {
> 	  case 0:y++;jj=ii+n;break;
> 	  case 1:x++;jj=ii+1;break;
> 	  case 2:y--;jj=ii-n;break;
> 	  case 3:x--;jj=ii-1;break;
> 	  default: return 1;
> 	 }
> 	 if((x<0)||(y<0)||(x>=n)||(y>=m))continue;
> 	 if(*(map+ii)<*(map+jj)&&(*(value+jj)+1>*(value+ii)))
> 	 {
> 	  value[ii]=value[jj]+1;
> 	  b=1;
> 	 }
> 	}
>   }
>  }
>  for(i=0;i<n*m;i++)
>  {
>   if(*(value+i)>b)b=*(value+i);
>  }
>  printf("%d",b+1);
>  free(map);
>  free(value);
>  return 1;
> }

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