Advertisement
jeff69

Untitled

Apr 6th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. // ConsoleApplication211.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <functional>
  7. #include <algorithm>
  8. #include <math.h>
  9. #include <cmath>
  10. #include <string>
  11. #include <cstring>
  12. #include <vector>
  13. #include<set>
  14. #include<map>
  15. #include <time.h>
  16. #include <fstream>
  17. #include <queue>
  18. typedef long long ll;
  19. using namespace std;
  20. vector<int> v[65];
  21. int sd(int x){
  22. return x / 8;
  23. }
  24. bool ch(int x,int y){
  25. bool h= x >= 0 && x <= 63;
  26. if (!h)return false;
  27. int r = sd(x);
  28. int c = x - 8 * r;
  29. int r2 = sd(y);
  30. int c2 = y - 8*r2;
  31. //cout << r << " " << c << " "<<r2 << " " << c2;
  32. if (abs(c2 - c) == 2 && abs(r2 - r) == 1)return true;
  33. else if (abs(r2 - r) == 2 && abs(c2 -c) == 1)return true;
  34. else{ return false; }
  35. }
  36. int main()
  37. {
  38. //cout << 17 / 8;
  39. //cout << sd(17);
  40. //ch(0, 17);
  41. //int g;
  42. //cin >> g;
  43.  
  44. ll vis[1000];
  45. for (int i = 0; i < 64; i++)
  46. {
  47.  
  48. //if (i % 8== 0&&i)cout<<"\n";
  49. //if (i / 10 == 0)cout << 0;cout << i << " ";
  50. int x;
  51. x = i - 17;//8
  52. if (ch(x, i))
  53. v[i].push_back(x);
  54. x = i - 15;//1
  55. if (ch(x, i))
  56. v[i].push_back(x);
  57. x = i - 10;//2
  58. if (ch(x, i))
  59. v[i].push_back(x);
  60. x = i - 6;//3
  61. if (ch(x, i))
  62. v[i].push_back(x);
  63. x = i + 10;//4
  64. if (ch(x, i))
  65. v[i].push_back(x);
  66. x = i + 6;//5
  67. if (ch(x, i))
  68. v[i].push_back(x);
  69. x = i + 17;//6
  70. if (ch(x, i))
  71. v[i].push_back(x);
  72. x = i + 15;//7
  73. if (ch(x, i))
  74. v[i].push_back(x);
  75.  
  76.  
  77.  
  78. }
  79. int t;
  80. cin >> t;
  81. while (t--)
  82. {
  83.  
  84. string a, b;
  85. cin >> a >> b;
  86. int x = a[1] - '1';
  87. int y = a[0] - 'a';
  88. int beg = y*(8) + x;
  89. int c = b[1] - '1';
  90. int d = b[0] - 'a';
  91. int fin = d * 8 + c;
  92. int s = beg;
  93.  
  94.  
  95. memset(vis, -1, sizeof vis);
  96. queue <int> Q;
  97. Q.push(s);
  98. vis[s] = 0;
  99. int h1, h2;
  100. h1 = beg;
  101. while (!Q.empty())
  102. {
  103.  
  104. h1 = Q.front();
  105. Q.pop();
  106. for (int i = 0; i < v[h1].size(); i++){
  107. int nxt = v[h1][i];
  108. if (vis[nxt] == -1){
  109. vis[nxt] = vis[h1] + 1;
  110. Q.push(nxt);
  111. }
  112. }
  113.  
  114.  
  115. }
  116. if (vis[fin] == -1)cout << 2;
  117. else cout << vis[fin];
  118.  
  119. if (t)cout << endl;
  120.  
  121.  
  122. // c d ::::::::::::::::::x y
  123.  
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement