-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcostlychess.cpp
More file actions
78 lines (73 loc) · 1.63 KB
/
costlychess.cpp
File metadata and controls
78 lines (73 loc) · 1.63 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define vl vector<ll>
#define vi vector<int>
#define lb lower_bound
#define ub upper_bound
// vector<vector<int>> vec( n , vector<int> (m, 0));
// priority_queue<pi, vector<pi>, greater<pi>>q;
typedef int64_t ll;
#define pp pair<int,int>
#define pi pair<ll, pp>
int dx[] = {2, 2, -2, -2, 1, -1, -1, 1};
int dy[] = {1, -1, -1, 1, 2, 2, -2, -2};
void solve(int a)
{
int b, c, d;
cin >> b >> c >> d;
int visited[8][8] = {0};
vector<vector<ll>> dist( 8 , vector<ll> (8, 1e18));
dist[a][b] = 0;
priority_queue<pi, vector<pi>, greater<pi>>q;
q.push({0, {a, b}});
while (!q.empty())
{
ll w = q.top().first;
int x = q.top().second.first;
int y = q.top().second.second;
if (x == c && y == d)
{
cout << w << endl;
return;
}
q.pop();
if (!visited[x][y])
{
visited[x][y] = 1;
for (int i = 0; i < 8; i++)
{
int xn = x + dx[i];
int yn = y + dy[i];
if ((xn >= 0 && xn < 8) && (yn >= 0 && yn < 8))
{
if (dist[x][y] + x * xn + y * yn < dist[xn][yn])
{
dist[xn][yn] = dist[x][y] + x * xn + y * yn;
q.push({dist[xn][yn], {xn, yn}});
}
}
}
}
}
cout << -1 << endl;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
int a;
while (cin >> a)
{
solve(a);
}
return 0;
}