-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing's Path.cpp
More file actions
51 lines (44 loc) · 1.75 KB
/
King's Path.cpp
File metadata and controls
51 lines (44 loc) · 1.75 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
47
48
49
50
51
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll dirRow[] = {-1, 1, 0, 0, -1, 1, -1, 1};
ll dirCol[] = {0, 0, 1, -1, -1, 1, 1, -1};
ll bfs(map< pair<ll, ll>, ll > &availableGrids, map< pair<ll, ll>, ll > &distance, ll startRow, ll startColumn, ll destinationRow, ll destinationColumn)
{
queue<ll> rowQ, columnQ;
rowQ.push(startRow); columnQ.push(startColumn);
distance[{startRow, startColumn}] = 1;
while(!rowQ.empty())
{
ll r = rowQ.front(), c = columnQ.front();
rowQ.pop(); columnQ.pop();
for(int i=0; i<8; i++)
{
if(r + dirRow[i] >= 10e9 + 1 || r + dirRow[i] <= 0 || c + dirCol[i] >= 10e9 + 1 || c + dirCol[i] <= 0) continue;
if(distance[{r + dirRow[i], c + dirCol[i]}] == 0 && availableGrids[{r + dirRow[i], c + dirCol[i]}] == 1)
{
distance[{r + dirRow[i], c + dirCol[i]}] = distance[{r, c}] + 1;
rowQ.push(r + dirRow[i]); columnQ.push(c + dirCol[i]);
}
if(distance[{destinationRow, destinationColumn}] != 0) return distance[{destinationRow, destinationColumn}] - 1;
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr);
// memset(dp, -1, sizeof(dp));
ll startRow, startColumn, destinationRow, destinationColumn, n; cin>>startRow>>startColumn>>destinationRow>>destinationColumn>>n;
map< pair<ll, ll>, ll > availableGrids;
map< pair<ll, ll>, ll > distance;
while(n--)
{
ll row, from, to; cin>>row>>from>>to;
for(ll i=from; i<=to; i++){
availableGrids[{row, i}] = 1;
}
}
cout<<bfs(availableGrids, distance, startRow, startColumn, destinationRow, destinationColumn)<<endl;
}