Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cmath>
- using namespace std;
- int exchange (char);
- vector <vector <int> > massiv(401);
- vector <int> way(401,-1);
- int BFS (int, int, int, vector <int> , vector <int>);
- int main () {
- int i, n=8, j, begin, end,x1,x2,y1,y2;
- char x11,x22;
- cin >>x11>>y1>>x22>>y2;
- y1--;
- y2--;//x1--;
- // x2--;
- x1=exchange(x11);
- x2=exchange(x22);
- vector <int> check(n*n);
- vector <int> length(n*n, 401);
- for (i = 0; i < n*n; i++) check[i] = 0;
- massiv.resize(n*n);
- for (i = 0; i < n*n; i++) {
- massiv[i].resize(n*n);
- for (j = 0; j < n*n; j++)
- {
- massiv[i][j]=0;
- if((i/n-j/n==1)&&(i%n-j%n==2)) massiv[i][j]=1;
- if((i/n-j/n==1)&&(i%n-j%n==-2)) massiv[i][j]=1;
- if((i/n-j/n==-1)&&(i%n-j%n==2)) massiv[i][j]=1;
- if((i/n-j/n==-1)&&(i%n-j%n==-2)) massiv[i][j]=1;
- if((i/n-j/n==2)&&(i%n-j%n==1)) massiv[i][j]=1;
- if((i/n-j/n==2)&&(i%n-j%n==-1)) massiv[i][j]=1;
- if((i/n-j/n==-2)&&(i%n-j%n==-1)) massiv[i][j]=1;
- if((i/n-j/n==-2)&&(i%n-j%n==1)) massiv[i][j]=1;
- };
- }
- begin=x1*n+y1;
- end=x2*n+y2;
- //cin >> begin >> end;
- if ((x1==x2)&&(y1==y2)) {cout<<0; return 0;};
- int result = BFS(end, begin, n*n, check, length);
- //if (result==101) {cout<<-1; return 0;};
- if(result%2==0)
- cout << result/2<<endl;
- else cout<<-1<<endl;//cout<<x1+1<<" "<<y1+1<<endl;
- //while(begin!=end)
- //{
- //cout<<way[begin]/n+1<<" "<<way[begin]%n+1<<endl;
- // begin=way[begin];
- //}
- return 0;
- }
- int BFS (int b, int e, int n, vector <int> check, vector <int> length) {
- queue <int> vertex;
- int i, cur;
- vertex.push(b);
- //way[b]=0;
- //way.push_back(e);
- length[b]=0;
- check[b]=1;
- while (!vertex.empty()) {
- cur = vertex.front();
- vertex.pop();
- for (int j = 0; j < n; j++) {
- if ((check[j]==0)&&(massiv[cur][j]==1)){
- length[j]=length[cur]+1;
- check[j] = 1;
- vertex.push(j);
- way[j]=cur;
- }
- }
- }
- return length[e];
- }
- int exchange( char coor)
- {
- if (coor=='a') return 0;
- if (coor=='b') return 1;
- if (coor=='c') return 2;
- if (coor=='d') return 3;
- if (coor=='e') return 4;
- if (coor=='f') return 5;
- if (coor=='g') return 6;
- if (coor=='h') return 7;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement