| ||||||||||
| 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 | |||||||||
超时了C++大家帮忙看一下,谢谢#include<cstdio>
using namespace std;
int a[20][20],b[8]={-1,0,1,0,0,-1,0,1};
bool f;
void dfs(int x,int y,int step,int h,int w)
{
int xx,yy,i;
if(f==true)
{
return;
}
for( i=0;i<8;i=i+2)
{
f=true;
xx=x+b[i];
yy=y+b[i+1];
if((a[xx][yy]==0||a[xx][yy]==3)&&xx>0&&xx<=h&&yy>0&&yy<=w)
{
f=false;
break;
}
}
if(f==true)
{
printf("-1\n");
return;
}
for( i=0;i<8&&f==false;i=i+2)
{
xx=x+b[i];
yy=y+b[i+1];
while(xx>0&&xx<=h&&yy>0&&yy<=w&&f==false)
{
if(a[xx][yy]==0)
{
xx=xx+b[i];
yy=yy+b[i+1];
}
if(a[xx][yy]==1)
{
a[xx][yy]=0;
if(step+1>10)
{
printf("-1\n");
f=true;
return;
}
dfs(xx-b[i],yy-b[i],step+1,h,w);
a[xx][yy]=1;
step=step-1;
}
if(a[xx][yy]==3)
{
/*if(step+1>10)
{
printf("-1\n");
}
else
{*/
printf("%d\n",step+1);
//}
f=true;
return;
}
}
}
}
int main()
{
int w,h,i,j,starth,startl,finishh,finishl;
while(scanf("%d%d",&w,&h)!=EOF)
{
if(w==0&&h==0)
{
break;
}
f=false;
for(i=1;i<=h;i++)
{
for(j=1;j<=w;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==2)
{
starth=i;
startl=j;
a[i][j]=0;
}
if(a[i][j]==3)
{
finishh=i;
finishl=j;
}
}
}
dfs(starth,startl,0,h,w);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator