Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. void solve(int rowst, int colst, int rowen, int colen);
  4.  
  5. using namespace std;
  6.  
  7. void ToNumbers(string start,string end)
  8. {
  9. int rowst,colst,rowen,colen;
  10. if(start.at(0)=='a')
  11. colst=0;
  12. else if(start.at(0)=='b')
  13. colst=1;
  14. else if(start.at(0)=='c')
  15. colst=2;
  16. else if(start.at(0)=='d')
  17. colst=3;
  18. else if(start.at(0)=='e')
  19. colst=4;
  20. else if(start.at(0)=='f')
  21. colst=5;
  22. else if(start.at(0)=='g')
  23. colst=6;
  24. else if(start.at(0)=='h')
  25. colst=7;
  26. if(start.at(1)=='8')
  27. rowst=0;
  28. else if(start.at(1)=='7')
  29. rowst=1;
  30. else if(start.at(1)=='6')
  31. rowst=2;
  32. else if(start.at(1)=='5')
  33. rowst=3;
  34. else if(start.at(1)=='4')
  35. rowst=4;
  36. else if(start.at(1)=='3')
  37. rowst=5;
  38. else if(start.at(1)=='2')
  39. rowst=6;
  40. else if(start.at(1)=='1')
  41. rowst=7;
  42. if(end.at(0)=='a')
  43. colen=0;
  44. else if(end.at(0)=='b')
  45. colen=1;
  46. else if(end.at(0)=='c')
  47. colen=2;
  48. else if(end.at(0)=='d')
  49. colen=3;
  50. else if(end.at(0)=='e')
  51. colen=4;
  52. else if(end.at(0)=='f')
  53. colen=5;
  54. else if(end.at(0)=='g')
  55. colen=6;
  56. else if(end.at(0)=='h')
  57. colen=7;
  58. if(end.at(1)=='8')
  59. rowen=0;
  60. else if(end.at(1)=='7')
  61. rowen=1;
  62. else if(end.at(1)=='6')
  63. rowen=2;
  64. else if(end.at(1)=='5')
  65. rowen=3;
  66. else if(end.at(1)=='4')
  67. rowen=4;
  68. else if(end.at(1)=='3')
  69. rowen=5;
  70. else if(end.at(1)=='2')
  71. rowen=6;
  72. else if(end.at(1)=='1')
  73. rowen=7;
  74.  
  75. solve(rowst,colst,rowen,colen);
  76. }
  77.  
  78. void solve(int origX,int origY,int destX,int destY)
  79. {
  80. queue<int>x;
  81. queue<int>y;
  82. int steps=0;
  83. bool visited[8][8];
  84. for(int i=0;i<8;i++)
  85. {
  86. for(int j=0;j<8;j++)
  87. visited[i][j]=false;
  88. }
  89. int row[8]={2,2,-2,-2,1,1,-1,-1};
  90. int col[8]={1,-1,1,-1,2,-2,2,-2};
  91. x.push(origX);y.push(origY);
  92. x.push(-1);y.push(-1);
  93. visited[origX][origY]=true;
  94. while(!x.empty()&&!y.empty())
  95. {
  96. int X=x.front(); x.pop();
  97. int Y=y.front(); y.pop();
  98. if(X==-1&&Y==-1)
  99. {
  100. steps++;
  101. x.push(-1);y.push(-1);
  102. continue;
  103. }
  104. if(X==destX&&Y==destY)
  105. {
  106. cout<<steps;
  107. return;
  108. }
  109. for(int k=0;k<8;k++)
  110. {
  111. int ansx=X+row[k];int ansy=Y+col[k];
  112. if(ansx<8&&ansx>=0&&ansy<8&&ansy>=0)
  113. {
  114. if(!visited[ansx][ansy])
  115. {
  116. visited[ansx][ansy]=true;
  117. x.push(ansx);y.push(ansy);
  118. }
  119. }
  120. }
  121. }
  122.  
  123. }
  124. int main() {
  125.  
  126. // your code here
  127. int testcases;
  128. cin>>testcases;
  129. string start,destination;
  130. for(int i=0;i<testcases;i++)
  131. {
  132. cin>>start>>destination;
  133. ToNumbers(start,destination);
  134. if(i!=testcases-1)
  135. cout<<endl;
  136. }
  137.  
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement