Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <iostream>
- #include <climits>
- using namespace std ;
- int board[8][8] ;
- map<string, int> m;
- int a0, a1, b0, b1, miniC = INT_MAX;
- void move(int m, int n, int counter)
- {
- if (m > 7 || n > 7 || n < 0 || m < 0 || board[m][n] < counter || counter >= miniC)
- return;
- board[m][n] = counter;
- if (m == b0 && n == b1)
- {
- miniC = min(counter, miniC);
- return;
- }
- move(m + 2, n + 1, counter + 1);
- move(m - 1, n + 2, counter + 1);
- move(m + 1, n - 2, counter + 1);
- move(m - 1, n - 2, counter + 1);
- move(m + 1, n + 2, counter + 1);
- move(m - 2, n + 1, counter + 1);
- move(m + 2, n - 1, counter + 1);
- move(m - 2, n - 1, counter + 1);
- return;
- }
- int main ()
- {
- int n ;
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- string s1 , s2 ;
- cin >> s1 >> s2;
- string s= s1+s2 ;
- if (m.count(s))
- cout << m[s] << endl ;
- else
- {
- a1 = s[0] - 'a';
- a0 = s[1] - '1';
- b1 = s[2] - 'a';
- b0 = s[3]- '1';
- for (int k = 0; k < 8; k++)
- for (int j = 0; j < 8; j++)
- board[k][j] = INT_MAX;
- miniC = INT_MAX;
- move(a0, a1, 0);
- if (miniC == INT_MAX)
- miniC = -1;
- cout << miniC << endl ;
- m.insert(make_pair(s, miniC));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement