Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int peta[150][150];
- int n,m;
- int mn = 99999;
- int main()
- {
- cin >> n >> m;
- for (int i=0; i<n; i++){
- for (int j=0; j<m; j++){
- cin >> peta[i][j];
- }
- }
- int a,b;
- cin >> a >> b;
- a--; b--;
- queue<int> bfsr;
- queue<int> bfsc;
- queue<int> bfsstepcount;
- bfsr.push(a);
- bfsc.push(b);
- bfsstepcount.push(1);
- while(!bfsr.empty()){
- int r = bfsr.front();
- int c = bfsc.front();
- int sc = bfsstepcount.front();
- bfsr.pop();
- bfsc.pop();
- bfsstepcount.pop();
- if ((r <= 0) || (r >= n-1) || (c <= 0) || (c >= m-1)){
- mn = sc;
- break;
- }
- if (peta[r-1][c] == 0){
- bfsr.push(r-1);
- bfsc.push(c);
- bfsstepcount.push(sc+1);
- }
- if (peta[r+1][c] == 0){
- bfsr.push(r+1);
- bfsc.push(c);
- bfsstepcount.push(sc+1);
- }
- if (peta[r][c-1] == 0){
- bfsr.push(r);
- bfsc.push(c-1);
- bfsstepcount.push(sc+1);
- }
- if (peta[r][c+1] == 0){
- bfsr.push(r);
- bfsc.push(c+1);
- bfsstepcount.push(sc+1);
- }
- }
- cout << mn << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement