| ||||||||||
| 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 | |||||||||
为什么我的数据都过了,但是wa…… 帮忙# include <iostream>
using namespace std;
int minstep=11;
int step=0;
int a[20][20];
int w,h;
void search(int i,int j,int step);
int main()
{
while(cin>>w>>h)
{
if(w==0&&h==0)
break;
int starti,startj,goali,goalj;
minstep=11;step=0;
for(int i=0;i<w;i++)
for(int j=0;j<h;j++)
{
cin>>a[i][j];
if(a[i][j]==2)
{
starti=i;
startj=j;
a[i][j]=0;
}
if(a[i][j]==3)
{
goali=i;
goalj=j;
}
}
search(starti,startj,step);
if(minstep==11)
cout<<-1<<endl;
else
cout<<minstep<<endl;
}
return 0;
}
void search(int i,int j,int step)
{
int tempi=i,tempj=j;
if(i>0&&a[i-1][j]!=1)
{
while(i>0&&a[i-1][j]==0)
i--;
if(i>0)
{
if(a[i-1][j]==3)
{
step++;
if(step<minstep)
minstep=step;
step--;
}
else
{
a[i-1][j]=0;
step++;
if(step<minstep)
search(i,j,step);
a[i-1][j]=1;
step--;
}
}
}
i=tempi,j=tempj;
if(j>0&&a[i][j-1]!=1)
{
while(j>0&&a[i][j-1]==0)
j--;
if(j>0)
{
if(a[i][j-1]==3)
{
step++;
if(step<minstep)
minstep=step;
step--;
}
else
{
a[i][j-1]=0;
step++;
if(step<minstep)
search(i,j,step);
a[i][j-1]=1;
step--;
}
}
}
i=tempi,j=tempj;
if(i<w-1&&a[i+1][j]!=1)
{
while(i<w-1&&a[i+1][j]==0)
i++;
if(i<w-1)
{
if(a[i+1][j]==3)
{
step++;
if(step<minstep)
minstep=step;
step--;
}
else
{
a[i+1][j]=0;
step++;
if(step<minstep)
search(i,j,step);
step--;
a[i+1][j]=1;
}
}
}
i=tempi,j=tempj;
if(j<h-1&&a[i][j+1]!=1)
{
while(j<h-1&&a[i][j+1]==0)
j++;
if(j<h-1)
{
if(a[i][j+1]==3)
{
step++;
if(step<minstep)
minstep=step;
step--;
}
else
{
a[i][j+1]=0;
step++;
if(step<minstep)
search(i,j,step);
step--;
a[i][j+1]=1;
}
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator