Advertisement
Guest User

Untitled

a guest
Aug 25th, 2014
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. pair<int, int> arr[110][110];
  6. pair<int, int> moves[10];
  7. int main() {
  8.     queue<pair<int, int> > q, q1;
  9.     char c, c1;
  10.     int ans = 1e6;
  11.     pair<int, int> p, p1;
  12.     cin >> c >> p.second >> c1 >> p1.second;
  13.     p.first = 8 - c + 'a';
  14.     p1.first = 8 - c1 + 'a';
  15.     q.push(p);
  16.     q1.push(p1);
  17.     moves[0].first = 2;
  18.     moves[0].second = 1;
  19.     moves[1].first = 2;
  20.     moves[1].second = -1;
  21.     moves[2].first = 1;
  22.     moves[2].second = 2;
  23.     moves[3].first = 1;
  24.     moves[3].second = -2;
  25.     moves[4].first = -1;
  26.     moves[4].second = 2;
  27.     moves[5].first = -1;
  28.     moves[5].second = -2;
  29.     moves[6].first = -2;
  30.     moves[6].second = 1;
  31.     moves[7].first = -2;
  32.     moves[7].second = -1;
  33.     while (!q.empty()) {
  34.         pair<int, int> v = q.front();
  35.         q.pop();
  36.         for (int i = 0; i <= 7; ++i) {
  37.             if (v.first + moves[i].first > 0 && v.first + moves[i].first <= 8)
  38.                 if (v.second + moves[i].second > 0 && v.second + moves[i].second <= 8)
  39.                     if (arr[v.first + moves[i].first][v.second + moves[i].second].first == 0) {
  40.                         arr[v.first + moves[i].first][v.second + moves[i].second].first = arr[v.first][v.second].first + 1;
  41.                         pair<int, int> k;
  42.                         k.first = v.first + moves[i].first;
  43.                         k.second = v.second + moves[i].second;
  44.                         q.push(k);
  45.                     }
  46.         }
  47.         v = q1.front();
  48.         q1.pop();
  49.         for (int i = 0; i <= 7; ++i) {
  50.             if (v.first + moves[i].first > 0 && v.first + moves[i].first <= 8)
  51.                 if (v.second + moves[i].second > 0 && v.second + moves[i].second <= 8)
  52.                     if (arr[v.first + moves[i].first][v.second + moves[i].second].second == 0) {
  53.                         arr[v.first + moves[i].first][v.second + moves[i].second].second = arr[v.first][v.second].second + 1;
  54.                         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)
  55.                             if (arr[v.first + moves[i].first][v.second + moves[i].second].second < ans) {
  56.                                 ans = arr[v.first + moves[i].first][v.second + moves[i].second].second;
  57.                                 //cout << v.first + moves[i].first << " " << v.second + moves[i].second;
  58.                             }
  59.                         pair<int, int> k;
  60.                         k.first = v.first + moves[i].first;
  61.                         k.second = v.second + moves[i].second;
  62.                         q1.push(k);
  63.                     }
  64.         }
  65.     }
  66.     if (ans == 1e6) {
  67.         cout << -1;
  68.     }
  69.     else
  70.         cout << ans;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement