1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<string> using namespace std; typedef long long ll; constexpr int N = 110; typedef pair<int,int> PII; int g[N][N]; int f[N][N]; int n,m; void bfs(int a,int b){ queue<PII> q; q.push({a,b}); f[0][0] = 0; while(!q.empty()){ PII start = q.front(); q.pop(); int dx[4] = {0,1,0,-1}; int dy[4] = {-1,0,1,0}; 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(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 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(1,1); }
|