您的答案為: 18 正確答案為: 17
#include<iostream>
#include<queue>
using namespace std;
int main(){
int N,M,i,j,x,y,dx,dy,st;queue<pair<pair<int,int>,int>>p;
int ax[4]={1,-1,0,0},ay[4]={0,0,1,-1};
cin>>N>>M;int grid[N][M]={0};bool visited[N][M]={0},no=1;
for(i=0;i<N;i++){
for(j=0;j<M;j++)
cin>>grid[i][j];
}
p.push({{0,0},0});
visited[0][0]=1;
while(!p.empty()){
x=p.front().first.first;y=p.front().first.second;st=p.front().second;
p.pop();
if(grid[x][y]==1){cout<<st<<'\n';no=0;}
for(i=0;i<=3;i++){
dx=x+ax[i];dy=y+ay[i];
if(dx>=0&&dx<N&&dy>=0&&dy<M&&visited[dx][dy]==0&&grid[dx][dy]!=2){
p.push({{dx,dy},st+1});
visited[dx][dy]=1;
}
}
}
if(no)cout<<"嘉油!";
return 0;
}