67 lines
1023 B
C++
67 lines
1023 B
C++
#include <bits/stdc++.h>
|
|
#define endl '\n'
|
|
using namespace std;
|
|
|
|
int n, m;
|
|
int arr[1010][1010];
|
|
int bol[1010][1010];
|
|
int s1, s2, e1, e2;
|
|
int fx[] = {0, 0, 1, 0, -1};
|
|
int fy[] = {0, 1, 0, -1, 0};
|
|
|
|
struct Node {
|
|
int x;
|
|
int y;
|
|
int cnt;
|
|
};
|
|
queue<Node> q; //
|
|
|
|
void bfs(int x, int y) {
|
|
Node nd;
|
|
nd.x = x;
|
|
nd.y = y;
|
|
nd.cnt = 0;
|
|
bol[x][y] = 1;
|
|
q.push(nd);
|
|
while(q.size()) {
|
|
Node head = q.front();
|
|
|
|
int cx = head.x;
|
|
int cy = head.y;
|
|
int cnt = head.cnt;
|
|
|
|
int tx, ty;
|
|
for(int i = 1; i <= 4; i++) {
|
|
tx = cx + fx[i];
|
|
ty = cy + fy[i];
|
|
while(tx >= 1 && tx <= n && ty >= 1 && ty <= m && arr[tx][ty] == 0) {
|
|
if(bol[tx][ty] == 0){
|
|
q.push({tx, ty, cnt + 1});
|
|
bol[tx][ty] = 1;
|
|
}
|
|
if(tx == e1 && ty == e2){
|
|
cout << cnt;
|
|
return ;
|
|
}
|
|
tx += fx[i];
|
|
ty += fy[i];
|
|
}
|
|
}
|
|
q.pop();
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
cin >> n >> m;
|
|
for(int i = 1; i <= n; i++) {
|
|
for(int j = 1; j <= m; j++) {
|
|
cin >> arr[i][j];
|
|
}
|
|
}
|
|
cin >> s1 >> s2;
|
|
cin >> e1 >> e2;
|
|
bfs(s1, s2);
|
|
return 0;
|
|
}
|
|
|