Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication211.cpp : Defines the entry point for the console application.
- //
- #include <iomanip>
- #include <iostream>
- #include <functional>
- #include <algorithm>
- #include <math.h>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <vector>
- #include<set>
- #include<map>
- #include <time.h>
- #include <fstream>
- #include <queue>
- typedef long long ll;
- using namespace std;
- vector<int> v[65];
- int sd(int x){
- return x / 8;
- }
- bool ch(int x,int y){
- bool h= x >= 0 && x <= 63;
- if (!h)return false;
- int r = sd(x);
- int c = x - 8 * r;
- int r2 = sd(y);
- int c2 = y - 8*r2;
- //cout << r << " " << c << " "<<r2 << " " << c2;
- if (abs(c2 - c) == 2 && abs(r2 - r) == 1)return true;
- else if (abs(r2 - r) == 2 && abs(c2 -c) == 1)return true;
- else{ return false; }
- }
- int main()
- {
- //cout << 17 / 8;
- //cout << sd(17);
- //ch(0, 17);
- //int g;
- //cin >> g;
- ll vis[1000];
- for (int i = 0; i < 64; i++)
- {
- //if (i % 8== 0&&i)cout<<"\n";
- //if (i / 10 == 0)cout << 0;cout << i << " ";
- int x;
- x = i - 17;//8
- if (ch(x, i))
- v[i].push_back(x);
- x = i - 15;//1
- if (ch(x, i))
- v[i].push_back(x);
- x = i - 10;//2
- if (ch(x, i))
- v[i].push_back(x);
- x = i - 6;//3
- if (ch(x, i))
- v[i].push_back(x);
- x = i + 10;//4
- if (ch(x, i))
- v[i].push_back(x);
- x = i + 6;//5
- if (ch(x, i))
- v[i].push_back(x);
- x = i + 17;//6
- if (ch(x, i))
- v[i].push_back(x);
- x = i + 15;//7
- if (ch(x, i))
- v[i].push_back(x);
- }
- int t;
- cin >> t;
- while (t--)
- {
- string a, b;
- cin >> a >> b;
- int x = a[1] - '1';
- int y = a[0] - 'a';
- int beg = y*(8) + x;
- int c = b[1] - '1';
- int d = b[0] - 'a';
- int fin = d * 8 + c;
- int s = beg;
- memset(vis, -1, sizeof vis);
- queue <int> Q;
- Q.push(s);
- vis[s] = 0;
- int h1, h2;
- h1 = beg;
- while (!Q.empty())
- {
- h1 = Q.front();
- Q.pop();
- for (int i = 0; i < v[h1].size(); i++){
- int nxt = v[h1][i];
- if (vis[nxt] == -1){
- vis[nxt] = vis[h1] + 1;
- Q.push(nxt);
- }
- }
- }
- if (vis[fin] == -1)cout << 2;
- else cout << vis[fin];
- if (t)cout << endl;
- // c d ::::::::::::::::::x y
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement