Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <climits>
  4. using namespace std ;
  5. int board[8][8] ;
  6. map<string, int> m;
  7. int a0, a1, b0, b1, miniC = INT_MAX;
  8. void move(int m, int n, int counter)
  9. {
  10.  
  11. if (m > 7 || n > 7 || n < 0 || m < 0 || board[m][n] < counter || counter >= miniC)
  12. return;
  13.  
  14. board[m][n] = counter;
  15. if (m == b0 && n == b1)
  16. {
  17. miniC = min(counter, miniC);
  18. return;
  19. }
  20.  
  21. move(m + 2, n + 1, counter + 1);
  22.  
  23. move(m - 1, n + 2, counter + 1);
  24.  
  25. move(m + 1, n - 2, counter + 1);
  26.  
  27. move(m - 1, n - 2, counter + 1);
  28.  
  29. move(m + 1, n + 2, counter + 1);
  30.  
  31. move(m - 2, n + 1, counter + 1);
  32.  
  33. move(m + 2, n - 1, counter + 1);
  34.  
  35. move(m - 2, n - 1, counter + 1);
  36.  
  37. return;
  38. }
  39.  
  40. int main ()
  41. {
  42. int n ;
  43. cin >> n;
  44. for (int i = 0; i < n; i++)
  45. {
  46. string s1 , s2 ;
  47. cin >> s1 >> s2;
  48. string s= s1+s2 ;
  49. if (m.count(s))
  50. cout << m[s] << endl ;
  51. else
  52. {
  53. a1 = s[0] - 'a';
  54. a0 = s[1] - '1';
  55. b1 = s[2] - 'a';
  56. b0 = s[3]- '1';
  57. for (int k = 0; k < 8; k++)
  58. for (int j = 0; j < 8; j++)
  59. board[k][j] = INT_MAX;
  60. miniC = INT_MAX;
  61. move(a0, a1, 0);
  62. if (miniC == INT_MAX)
  63. miniC = -1;
  64. cout << miniC << endl ;
  65. m.insert(make_pair(s, miniC));
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement