-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva439.cpp
83 lines (63 loc) · 1.39 KB
/
uva439.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
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
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
#include <queue>
#define table_size 8
#define BFS_WHITE -1
using namespace std;
int dist[20][20]; /* BFS layer index */
queue<pair<int, int> > q;
int row[] = {-1, 1, -2, 2, -2, 2, -1, 1};
int col[] = {-2, -2, -1, -1, 1, 1, 2, 2};
void bfs(pair<int, int> knight)
{
queue<pair<int, int> > empty;
q = empty;
dist[knight.first][knight.second] = 0;
q.push(knight);
while(!q.empty())
{
pair<int, int> top = q.front();
int l = top.first, c = top.second;
q.pop();
for(int i = 0; i < 8; i++)
{
if(1 <= l + row[i] && l + row[i] <= table_size)
{
if(1 <= c + col[i] && c + col[i] <= table_size)
{
if(dist[l+row[i]][c+col[i]] == BFS_WHITE)
{
dist[l+row[i]][c+col[i]] = dist[l][c] + 1;
q.push(make_pair(l+row[i], c+col[i]));
}
}
}
}
}
}
int main()
{
string src, dest;
int r1, r2, c1, c2;
while(true)
{
cin >> src;
if(cin.eof())
break;
cin >> dest;
r1 = src[0] - 'a' + 1; c1 = src[1] - '0';
r2 = dest[0] - 'a' + 1; c2 = dest[1] - '0';
memset(dist, BFS_WHITE, sizeof(dist));
bfs(make_pair(r1, c1));
int rez = dist[r2][c2] - dist[r1][c1];
cout << "To get from " << src << " to " << dest << " takes " << rez << " knight moves." << endl;
}
return 0;
}