-
Notifications
You must be signed in to change notification settings - Fork 7
/
CCHESS.cpp
47 lines (46 loc) · 1004 Bytes
/
CCHESS.cpp
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
#include "bits/stdc++.h"
using namespace std;
int dx[] = { 2,2,1,-1,1,-1,-2,-2 };
int dy[] = { -1,1,2,2,-2,-2,1,-1 };
bool visit[10][10];
bool isSafe(int i, int j)
{
return (i >= 0) && (i <= 7) && (j >= 0) && (j <= 7);
}
int bfs(int i, int j, int di, int dj)
{
//visit[i][j] = 1;
priority_queue<tuple<int,int,int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>> > pq;
pq.push(make_tuple(0, i, j));
while (!pq.empty())
{
int x = get<1>(pq.top());
int y = get<2>(pq.top());
int z = get<0>(pq.top());
pq.pop();
if (x == di&&y == dj)
{
return z;
}
visit[x][y] = 1;
for (int K = 0; K < 8; K++)
{
if (isSafe(x + dx[K], y + dy[K])&&!visit[x+dx[K]][y+dy[K]])
{
// visit[x + dx[K]][y + dy[K]] = 1;
pq.push(make_tuple(z+x*(x+dx[K])+y*(y+dy[K]),x + dx[K], y + dy[K]));
}
}
}
return -1;
}
int main()
{
int a, b, c, d;
while (scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
{
memset(visit, 0, sizeof visit);
printf("%d\n", bfs(a, b, c, d));
}
return 0;
}