fork download
  1. # include <iostream>
  2. # include <cstdlib>
  3. # include <cstdio>
  4. # include <limits>
  5. # include <map>
  6. # include <queue>
  7. using namespace std;
  8. struct Node
  9. {
  10. int X, Y;
  11. Node (int x = 0, int y = 0) {X = x; Y = y;}
  12. bool operator < (const Node & node) const
  13. {
  14. if (X == node.X)
  15. {
  16. return (Y < node.Y);
  17. }
  18. return (X < node.X);
  19. }
  20. };
  21. map <Node, int> Distances;
  22. queue <Node> Queue;
  23. Node Start, End;
  24. void Update (Node node, Node node1)
  25. {
  26. if (Distances.find (node1) == Distances.end())
  27. {
  28. Distances [node1] = Distances [node] + 1;
  29. if (node1.X != End.X || node1.Y != End.Y)
  30. Queue.push (node1);
  31. }
  32. else
  33. {
  34. if (Distances [node] + 1 < Distances[node1])
  35. {
  36. Distances [node1] = Distances [node] + 1;
  37. if (node1.X != End.X || node1.Y != End.Y)
  38. Queue.push (node1);
  39. }
  40. }
  41. }
  42. int main(int argc, char const *argv[])
  43. {
  44. //freopen ("439_Knight_Moves_input.txt", "r", stdin);
  45. //freopen ("439_Knight_Moves_output.txt", "w", stdout);
  46. string str1, str2;
  47. while (cin >> str1 >> str2)
  48. {
  49. Start.X = str1[0]-97; Start.Y = str1[1]-49;
  50. End.X = str2[0]-97; End.Y = str2[1]-49;
  51. Distances.clear();
  52. Distances [Start] = 0;
  53. Distances [End] = numeric_limits <int>::max();
  54. if (Start.X != End.X || Start.Y != End.Y)
  55. Queue.push (Start);
  56. else
  57. Distances [End] = 0;
  58. while (Queue.empty() == false)
  59. {
  60. Node node = Queue.front();
  61. Queue.pop();
  62. //cout << "Popping "
  63. if (node.X + 1 < 8 && node.Y + 2 < 8)
  64. Update (node, Node (node.X + 1, node.Y + 2));
  65. if (node.X + 2 < 8 && node.Y + 1 < 8)
  66. Update (node, Node (node.X + 2, node.Y + 1));
  67. if (node.X + 1 < 8 && node.Y - 2 >= 0)
  68. Update (node, Node (node.X + 1, node.Y - 2));
  69. if (node.X + 2 < 8 && node.Y - 1 >= 0)
  70. Update (node, Node (node.X + 2, node.Y - 1));
  71. if (node.X - 1 >= 0 && node.Y + 2 < 8)
  72. Update (node, Node (node.X - 1, node.Y + 2));
  73. if (node.X - 2 >= 0 && node.Y + 1 < 8)
  74. Update (node, Node (node.X - 2, node.Y + 1));
  75. if (node.X - 1 >= 0 && node.Y - 2 >= 0)
  76. Update (node, Node (node.X - 1, node.Y - 2));
  77. if (node.X - 2 >= 0 && node.Y - 1 >= 0)
  78. Update (node, Node (node.X - 2, node.Y - 1));
  79. }
  80. //if (cin.eof())
  81. // cout << "To get from " << str1 << " to " << str2 << " takes " << Distances [End] << " knight moves.";
  82. //else
  83. cout << "To get from " << str1 << " to " << str2 << " takes " << Distances [End] << " knight moves." << endl;
  84. }
  85. return 0;
  86. }
Success #stdin #stdout 0.02s 2868KB
stdin
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
stdout
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.