-
Notifications
You must be signed in to change notification settings - Fork 0
/
NAKANJ.cpp
62 lines (57 loc) · 1.72 KB
/
NAKANJ.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
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
pair<int,int> getCoordsForPosition(string s)
{
return make_pair(s[1]-'0',s[0]-'a'+1);
}
int main() {
int t;
cin >> t;
while(t--)
{
map< pair<int,int>, bool > visited;
queue< pair< pair<int,int> , int> > Q;
string s1,s2;
cin >> s1 >> s2;
pair<int,int> s = getCoordsForPosition(s1);
pair<int,int> d = getCoordsForPosition(s2);
Q.push({s,0});
int steps = 0;
while(!Q.empty())
{
pair< pair<int,int>, int> top = Q.front();
if(top.first.first == d.first && top.first.second == d.second)
{
cout << top.second << endl;
break;
}
Q.pop();
if(!visited[top.first])
{
steps = top.second;
if(top.first.first+2<=8 && top.first.second+1<=8)
Q.push({{top.first.first+2,top.first.second+1},steps+1});
if(top.first.first+2<=8 && top.first.second-1>=1)
Q.push({{top.first.first+2,top.first.second-1},steps+1});
if(top.first.first-2>=1 && top.first.second+1<=8)
Q.push({{top.first.first-2,top.first.second+1},steps+1});
if(top.first.first-2>=1 && top.first.second-1>=1)
Q.push({{top.first.first-2,top.first.second-1},steps+1});
if(top.first.first-1>=1 && top.first.second+2<=8)
Q.push({{top.first.first-1,top.first.second+2},steps+1});
if(top.first.first-1>=1 && top.first.second-2>=1)
Q.push({{top.first.first-1,top.first.second-2},steps+1});
if(top.first.first+1<=8 && top.first.second-2>=1)
Q.push({{top.first.first+1,top.first.second-2},steps+1});
if(top.first.first+1<=8 && top.first.second+2<=8)
Q.push({{top.first.first+1,top.first.second+2},steps+1});
}
visited[top.first] = 1;
}
visited[s] = 1;
}
return 0;
}