Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cmath>
  5. using namespace std;
  6. int exchange (char);
  7. vector <vector <int> > massiv(401);
  8. vector <int> way(401,-1);
  9. int BFS (int, int, int, vector <int> , vector <int>);
  10.  
  11. int main () {
  12. int i, n=8, j, begin, end,x1,x2,y1,y2;
  13. char x11,x22;
  14.  
  15. cin >>x11>>y1>>x22>>y2;
  16.  
  17. y1--;
  18. y2--;//x1--;
  19. // x2--;
  20. x1=exchange(x11);
  21. x2=exchange(x22);
  22. vector <int> check(n*n);
  23. vector <int> length(n*n, 401);
  24. for (i = 0; i < n*n; i++) check[i] = 0;
  25.  
  26. massiv.resize(n*n);
  27. for (i = 0; i < n*n; i++) {
  28. massiv[i].resize(n*n);
  29. for (j = 0; j < n*n; j++)
  30. {
  31. massiv[i][j]=0;
  32. if((i/n-j/n==1)&&(i%n-j%n==2)) massiv[i][j]=1;
  33. if((i/n-j/n==1)&&(i%n-j%n==-2)) massiv[i][j]=1;
  34. if((i/n-j/n==-1)&&(i%n-j%n==2)) massiv[i][j]=1;
  35. if((i/n-j/n==-1)&&(i%n-j%n==-2)) massiv[i][j]=1;
  36. if((i/n-j/n==2)&&(i%n-j%n==1)) massiv[i][j]=1;
  37. if((i/n-j/n==2)&&(i%n-j%n==-1)) massiv[i][j]=1;
  38. if((i/n-j/n==-2)&&(i%n-j%n==-1)) massiv[i][j]=1;
  39. if((i/n-j/n==-2)&&(i%n-j%n==1)) massiv[i][j]=1;
  40. };
  41. }
  42. begin=x1*n+y1;
  43. end=x2*n+y2;
  44. //cin >> begin >> end;
  45. if ((x1==x2)&&(y1==y2)) {cout<<0; return 0;};
  46. int result = BFS(end, begin, n*n, check, length);
  47. //if (result==101) {cout<<-1; return 0;};
  48. if(result%2==0)
  49. cout << result/2<<endl;
  50. else cout<<-1<<endl;//cout<<x1+1<<" "<<y1+1<<endl;
  51. //while(begin!=end)
  52. //{
  53. //cout<<way[begin]/n+1<<" "<<way[begin]%n+1<<endl;
  54. // begin=way[begin];
  55. //}
  56.  
  57. return 0;
  58. }
  59.  
  60. int BFS (int b, int e, int n, vector <int> check, vector <int> length) {
  61.  
  62. queue <int> vertex;
  63.  
  64. int i, cur;
  65. vertex.push(b);
  66. //way[b]=0;
  67. //way.push_back(e);
  68. length[b]=0;
  69. check[b]=1;
  70. while (!vertex.empty()) {
  71. cur = vertex.front();
  72. vertex.pop();
  73.  
  74. for (int j = 0; j < n; j++) {
  75. if ((check[j]==0)&&(massiv[cur][j]==1)){
  76. length[j]=length[cur]+1;
  77. check[j] = 1;
  78. vertex.push(j);
  79. way[j]=cur;
  80. }
  81. }
  82. }
  83. return length[e];
  84. }
  85. int exchange( char coor)
  86. {
  87. if (coor=='a') return 0;
  88. if (coor=='b') return 1;
  89. if (coor=='c') return 2;
  90. if (coor=='d') return 3;
  91. if (coor=='e') return 4;
  92. if (coor=='f') return 5;
  93. if (coor=='g') return 6;
  94. if (coor=='h') return 7;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement