include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
const int N = 110;
int g[N][N],f[N][N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int n,m;
typedef pair[HTML_REMOVED] PII;
queue[HTML_REMOVED] q;
void bfs(){
q.push({1,1});
f[1][1]=0;
while(!q.empty()){
PII start = q.front();
q.pop();
for(int i=0;i<4;i++){
int x = start.first +dx[i];
int y = start.second +dy[i];
if(g[x][y]==0){
g[x][y]=1;
f[x][y]=f[start.first][start.second]+1;
q.push({x,y});
}
}
}
cout<<f[n][m];
}
int main(){
memset(g,-1,sizeof(g));
cin>>n>>m;
for(int i=1;i<=n;i)
{
for(int j=1;j<=m;j){
cin>>g[i][j];
}
}
bfs();
return 0;
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla