Created
May 7, 2023 23:29
-
-
Save estelabn/c453f1a2b4cda26b2fc425f8586aecc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int maxn = 510; | |
int l,c; | |
int add[5] = {-2, -1, 0, 1, 2}; | |
int m[maxn][maxn], res[maxn][maxn]; | |
bool valid(int x, int y){ | |
if(x > 0 and x <= l and y > 0 and y <= c and m[x][y] == 1) return true; | |
return false; | |
} | |
int main(){ | |
cin >> l >> c; | |
for(int i=1; i <= l; i++) for(int j = 1; j<= c; j++) cin >> m[i][j]; | |
memset(res, -1, sizeof(res)); | |
queue< pair<int,int>> q; | |
res[1][1] = 0; | |
q.push({1,1}); | |
while(!q.empty()){ | |
int x = q.front().first, y = q.front().second; | |
q.pop(); | |
for(int i=0; i<5; i++) for(int j = 0; j<5; j++){ | |
int xx= x + add[i], yy = y + add[j]; | |
if(!valid(xx,yy)) continue; | |
if(res[xx][yy] != -1) continue; | |
res[xx][yy] = res[x][y] + 1; | |
q.push({xx,yy}); | |
} | |
} | |
cout << res[l][c] << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment