Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. int q[10000][6],qb,qe,sx,sy,ex,ey,x,y,sn;
  7. bool marked[10][10],found;
  8.  
  9. void put(int x,int y,int sn) {
  10. qe++;
  11. q[qe][1]=x; q[qe][2]=y; q[qe][3]=sn;
  12. //cout << x << " " << y << " " << sn << endl;
  13. marked[x][y]=true;
  14. }
  15.  
  16. void get(int& x,int& y,int& sn) {
  17. x=q[qb][1]; y=q[qb][2]; sn=q[qb][3];
  18. qb++;
  19. }
  20.  
  21. void startprocess() {
  22. string s,f;
  23. cin >> s >> f;
  24. sx=s[0]-'a'+1; sy=s[1]-'0';
  25. ex=f[0]-'a'+1; ey=f[1]-'0';
  26. for (int i=1;i<=8;i++)
  27. for (int j=1;j<=8;j++)
  28. marked[i][j]=false;
  29.  
  30. found=false;
  31. put(sx,sy,0);
  32. }
  33.  
  34. void putall(int x,int y,int sn,bool& found) {
  35. int h1=(sn+1)/2; int h2=sn/2;
  36. //cout << h1 << " " << h2 << endl;
  37. int steps[8][2] = {{h1,h2}, {h1,-h2}, {-h1,h2}, {-h1,-h2}, {h2,h1}, {h2,-h1}, {-h2,h1}, {-h2,-h1}};
  38. int i=0;
  39. while (i<8 && !found) {
  40. int cx=x+steps[i][0];
  41. int cy=y+steps[i][1];
  42. //cout << x << " " << y << " -> " << cx << " " << cy << " " << sn << endl;
  43. found=(cx==ex && cy==ey);
  44. if (cx>0 && cx<=8 && cy>0 && cy<=8)
  45. put(cx,cy,sn);
  46. i++;
  47. }
  48. }
  49.  
  50. int main()
  51. {
  52. ios_base::sync_with_stdio(0); cin.tie(0);
  53. freopen("cheval.in","r",stdin);
  54. freopen("cheval.out","w",stdout);
  55. startprocess();
  56. while (qb<=qe && !found) {
  57. get(x,y,sn);
  58. sn++;
  59. if (sn>16) break;
  60. putall(x,y,sn,found);
  61. }
  62. if (!found) {
  63. cout << "-1";
  64. } else
  65. cout << sn;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement