| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
~~本人刚接触ACM,哪位大牛帮我看看哪里超时了啊,感激不尽~~#include<iostream>
using namespace std;
#include<stdio.h>
int max_4(int a,int b,int c ,int d)
{
int max=a;
if (b>max)
{
max=b;
}
if (c>max)
{
max=c;
}
if (d>max)
{
max=d;
}
return max;
}
int best_val(int *buf,int *val,int *wall,int row,int col,int i,int j)
{
if(val[i*col+j]>0)
{
return val[i*col+j];
}
if(i==0||j==0||i==row-1||j==col-1)
{
return val[i*col+j];
}
else if (wall[(i-1)*col+j]>=wall[i*col+j]&&wall[(i+1)*col+j]>=wall[i*col+j]&&wall[i*col+j-1]>=wall[i*col+j]&&wall[i*col+j+1]>=wall[i*col+j])
{
int up_val,down_val,l_val,r_val;
if (buf[(i-1)*col+j]!=buf[i*col+j])
{
up_val=best_val(buf,val,wall,row,col,i-1,j);
}
else
{
up_val=-1;
}
if (buf[(i+1)*col+j]!=buf[i*col+j])
{
down_val=best_val(buf,val,wall,row,col,i+1,j);
}
else
{
down_val=-1;
}
if (buf[i*col+j-1]!=buf[i*col+j])
{
l_val=best_val(buf,val,wall,row,col,i,j-1);
}
else
{
l_val=-1;
}
if (buf[i*col+j+1]!=buf[i*col+j])
{
r_val=best_val(buf,val,wall,row,col,i,j+1);
}
else
{
r_val=-1;
}
val[i*col+j]=max_4(up_val,down_val,l_val,r_val)+1;
wall[i*col+j]=10001;
return val[i*col+j];
}
else
{
return 0;
}
}
int main()
{
int row,col;
scanf("%d %d",&row,&col);
int *buf=new int[(row+2)*(col+2)];
int *wall=new int[(row+2)*(col+2)];
int i,j;
for(i=0;i<col+2;i++)
{
buf[i]=10001;
wall[i]=10001;
}
for (i=0;i<row;i++)
{
buf[(i+1)*(col+2)]=10001;
wall[(i+1)*(col+2)]=10001;
for (j=1;j<=col;j++)
{
scanf("%d",&buf[(i+1)*(col+2)+j]);
wall[(i+1)*(col+2)+j]=buf[(i+1)*(col+2)+j];
}
buf[(i+1)*(col+2)+col+1]=10001;
wall[(i+1)*(col+2)+col+1]=10001;
}
for(i=0;i<col+2;i++)
{
buf[(col+2)*(row+1)+i]=10001;
wall[(col+2)*(row+1)+i]=10001;
}
int *val=new int[(row+2)*(col+2)];
memset(val,0,4*(row+2)*(col+2));
int max=0;
int count=0;
while(count<col*row)
{
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
if(val[i*(col+2)+j]==0)
{
int c=val[i*(col+2)+j]=best_val(buf,val,wall,row+2,col+2,i,j);
if (c>0)
{
count++;
}
if (c>max)
{
max=c;
}
}
}
}
}
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
cout<<val[i*(col+2)+j]<<" ";
}
cout<<endl;
}
delete[]buf;
delete[]wall;
delete[]val;
cout<<endl;
cout<<max<<endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator