-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquan_ma.cpp
More file actions
46 lines (45 loc) · 1.13 KB
/
quan_ma.cpp
File metadata and controls
46 lines (45 loc) · 1.13 KB
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
#include <bits/stdc++.h>
using namespace std;
int n, s, t, u, v;
int a[1005][1005];
int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int d[1005][1005]; // so buoc di chuyen toi thieu tu s, t den u, v
bool visited[1005][1005];
int bfs(int i, int j)
{
queue<pair<int, int>> q;
q.push({i, j});
visited[i][j] = true;
while(!q.empty())
{
pair<int, int> x = q.front();
q.pop();
if(x.first == u && x.second == v) return d[u][v];
int i1 = x.first, j1 = x.second;
for(int i = 0; i < 8; i++)
{
int inew = i1 + dx[i];
int jnew = j1 + dy[i];
if(inew >= 1 && inew <= n && jnew >= 1 && jnew <= n && a[inew][jnew] && !visited[inew][jnew])
{
q.push({inew, jnew});
visited[inew][jnew] = true;
d[inew][jnew] = d[i1][j1] + 1;
}
}
}
return -1;
}
int main()
{
cin >> n >> s >> t >> u >> v;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
cout << bfs(s, t) << endl;
}