虽然一堆pair我没看懂,但是当我看到没有`bool vis[]`数组的时候,我就已经开始感觉不对劲了...
by _venti @ 2023-08-07 20:18:25
过了,没加vis,广搜不加vis是会寄的(
```
#include <bits/stdc++.h>
using namespace std;
int m,n;
short dx[4]={0,0,1,-1};
short dy[4]={1,-1,0,0};
int sx,sy,ex,ey;
int c[10][10];
bool vis[10][10][10];//!!
void bfs(){
queue<pair<pair<short,short>,pair<int,short> > > q;
q.push(make_pair(make_pair(sy,sx),make_pair(0,6)));
while(!q.empty()){
pair<pair<short,short>,pair<int,short>> p=q.front();
q.pop();
if(p.second.second==0){
continue;
}
if(vis[p.first.first][p.first.second][p.second.second])//!!
continue;
vis[p.first.first][p.first.second][p.second.second]=1;//!!
if(c[p.first.first][p.first.second]==4){
p.second.second=6;
}
p.second.second--;
if(c[p.first.first][p.first.second]==3){
printf("%d",p.second.first);
exit(0);
}
if(p.second.first>=n*m-2){
break;
}
p.second.first++;
for(int i=0;i<4;i++){
short xx=p.first.second+dx[i];
short yy=p.first.first+dy[i];
if(xx>=0&&xx<m&&yy>=0&&yy<n&&c[yy][xx]!=0){
q.push(make_pair(make_pair(yy,xx),make_pair(p.second.first,p.second.second)));
}
}
}
printf("-1");
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&c[i][j]);
if(c[i][j]==2){
sx=j;
sy=i;
}
}
}
bfs();
return 0;
}
```
by _venti @ 2023-08-07 20:27:06
@[_venti](/user/1033727)
谢谢。我一开始开的是二维数组,原来要开三维的呀。
by User586768 @ 2023-08-08 19:37:12