Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- pair<int, int> arr[110][110];
- pair<int, int> moves[10];
- int main() {
- queue<pair<int, int> > q, q1;
- char c, c1;
- int ans = 1e6;
- pair<int, int> p, p1;
- cin >> c >> p.second >> c1 >> p1.second;
- p.first = 8 - c + 'a';
- p1.first = 8 - c1 + 'a';
- q.push(p);
- q1.push(p1);
- moves[0].first = 2;
- moves[0].second = 1;
- moves[1].first = 2;
- moves[1].second = -1;
- moves[2].first = 1;
- moves[2].second = 2;
- moves[3].first = 1;
- moves[3].second = -2;
- moves[4].first = -1;
- moves[4].second = 2;
- moves[5].first = -1;
- moves[5].second = -2;
- moves[6].first = -2;
- moves[6].second = 1;
- moves[7].first = -2;
- moves[7].second = -1;
- while (!q.empty()) {
- pair<int, int> v = q.front();
- q.pop();
- for (int i = 0; i <= 7; ++i) {
- if (v.first + moves[i].first > 0 && v.first + moves[i].first <= 8)
- if (v.second + moves[i].second > 0 && v.second + moves[i].second <= 8)
- if (arr[v.first + moves[i].first][v.second + moves[i].second].first == 0) {
- arr[v.first + moves[i].first][v.second + moves[i].second].first = arr[v.first][v.second].first + 1;
- pair<int, int> k;
- k.first = v.first + moves[i].first;
- k.second = v.second + moves[i].second;
- q.push(k);
- }
- }
- v = q1.front();
- q1.pop();
- for (int i = 0; i <= 7; ++i) {
- if (v.first + moves[i].first > 0 && v.first + moves[i].first <= 8)
- if (v.second + moves[i].second > 0 && v.second + moves[i].second <= 8)
- if (arr[v.first + moves[i].first][v.second + moves[i].second].second == 0) {
- arr[v.first + moves[i].first][v.second + moves[i].second].second = arr[v.first][v.second].second + 1;
- if (arr[v.first + moves[i].first][v.second + moves[i].second].second == arr[v.first + moves[i].first][v.second + moves[i].second].first)
- if (arr[v.first + moves[i].first][v.second + moves[i].second].second < ans) {
- ans = arr[v.first + moves[i].first][v.second + moves[i].second].second;
- //cout << v.first + moves[i].first << " " << v.second + moves[i].second;
- }
- pair<int, int> k;
- k.first = v.first + moves[i].first;
- k.second = v.second + moves[i].second;
- q1.push(k);
- }
- }
- }
- if (ans == 1e6) {
- cout << -1;
- }
- else
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement