#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[10][10];
int b[10][10];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int mx,my;
void bfs(int x,int y)
{
queue<pair<int,int>>q;
queue<int>t;
queue<int>h;
q.push(make_pair(x,y));
t.push(0);
h.push(6);
while(!q.empty())
{
x=q.front().first;
y=q.front().second;
if(x==mx&&y==my)
{
printf("%d",t.front());
exit(0);
}
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(xx>=1&&yy>=1&&xx<=n&&yy<=m&&h.front()-1>0&&a[xx][yy]!=0&&b[xx][yy]==0)
{
b[xx][yy]=1;
if(a[xx][yy]==4)
{
h.push(6);
}
else
{
h.push(h.front()-1);
}
t.push(t.front()+1);
q.push(make_pair(xx,yy));
}
}
h.pop();
t.pop();
q.pop();
}
return;
}
int main()
{
int xx,yy;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==2)
{
xx=i;
yy=j;
a[i][j]=0;
}
if(a[i][j]==3)
{
mx=i;
my=j;
a[i][j]=1;
}
}
}
bfs(xx,yy);
printf("-1");
return 0;
}
#10 #12 WA
不知为何
猜测为有重复走的路
求改正