0% found this document useful (0 votes)
11 views1 page

BFS

Uploaded by

ldbntroll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

BFS

Uploaded by

ldbntroll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <bits/stdc++.

h>
using namespace std;
#define ll long long
#pragma clang diagnostic ignored "-Wc++17-extensions"

int d[1000][1000];
int dx[4] = {-1,0,0,1};
int dy[4] = {0,-1,1,0};
int n,m,x,y,z,h;
char a[1000][1000];

void bfs(int i,int j){


queue<pair<int,int>> q;
q.push({i,j});
a[i][j] = '0';
d[i][j] = 0;
while (!q.empty()){
pair<int,int> top = q.front(); q.pop();
for (int k = 0;k<4;k++){
int i1 = top.first + dx[k];
int j1 = top.second + dy[k];
if (i1 >= 1 && i1<=n && j1 >= 1 && j1 <= m && a[i1][j1] != '1'){
d[i1][j1] = d[top.first][top.second] + 1;
if (a[i1][j1] == 'B') return;
q.push({i1,j1});
a[i1][j1] = '0';
}
}

}
}

int main() {
(void)freopen("task05.inp", "r", stdin);
(void)freopen("task05.out", "w", stdout);

cin >> n >> m;


cin >> x >> y >> z >> h;
for (int i = 1;i<=n;i++){
for (int j = 1;j<=m;j++){
cin >> a[i][j];
}
}
a[x][y] = 'A';
a[z][h] = 'B';
bfs(x,y);
cout << d[z][h]+1;
}

You might also like