显示代码纯文本
#include <cstdio>
using namespace std;
int main(void)
{
freopen("bronlily.in","r",stdin);
freopen("bronlily.out","w",stdout);
int RUL[8][2],i,j,m,n,tx,ty,tail=0,head=0,quex[900],quey[900],step[900]={0},map[31][31];
scanf("%d %d %d %d",&m,&n,&tx,&ty);
RUL[0][0]=-tx;
RUL[0][1]=-ty;
RUL[1][0]=tx;
RUL[1][1]=-ty;
RUL[2][0]=-tx;
RUL[2][1]=ty;
RUL[3][0]=tx;
RUL[3][1]=ty;
RUL[4][0]=-ty;
RUL[4][1]=-tx;
RUL[5][0]=ty;
RUL[5][1]=-tx;
RUL[6][0]=-ty;
RUL[6][1]=tx;
RUL[7][0]=ty;
RUL[7][1]=tx;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
if (map[i][j]==3)
{
quex[0]=i;
quey[0]=j;
map[i][j]=0;
}
}
while (tail<=head)
{
for (i=0;i<=7;i++)
{
tx=RUL[i][0]+quex[tail];
ty=RUL[i][1]+quey[tail];
if (tx>=1&&ty>=1&&tx<=m&&ty<=n&&map[tx][ty]!=0&&map[tx][ty]!=2)
{
head++;
quex[head]=tx;
quey[head]=ty;
step[head]=step[tail]+1;
if (map[tx][ty]==4)
{
printf("%d\n",step[head]);
fclose(stdin);
fclose(stdout);
return(0);
}
else
map[tx][ty]=0;
}
}
tail++;
}
printf("%d\n",step[head]);
fclose(stdin);
fclose(stdout);
return(0);
}