Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int q[10000][6],qb,qe,sx,sy,ex,ey,x,y,sn;
- bool marked[10][10],found;
- void put(int x,int y,int sn) {
- qe++;
- q[qe][1]=x; q[qe][2]=y; q[qe][3]=sn;
- //cout << x << " " << y << " " << sn << endl;
- marked[x][y]=true;
- }
- void get(int& x,int& y,int& sn) {
- x=q[qb][1]; y=q[qb][2]; sn=q[qb][3];
- qb++;
- }
- void startprocess() {
- string s,f;
- cin >> s >> f;
- sx=s[0]-'a'+1; sy=s[1]-'0';
- ex=f[0]-'a'+1; ey=f[1]-'0';
- for (int i=1;i<=8;i++)
- for (int j=1;j<=8;j++)
- marked[i][j]=false;
- found=false;
- put(sx,sy,0);
- }
- void putall(int x,int y,int sn,bool& found) {
- int h1=(sn+1)/2; int h2=sn/2;
- //cout << h1 << " " << h2 << endl;
- int steps[8][2] = {{h1,h2}, {h1,-h2}, {-h1,h2}, {-h1,-h2}, {h2,h1}, {h2,-h1}, {-h2,h1}, {-h2,-h1}};
- int i=0;
- while (i<8 && !found) {
- int cx=x+steps[i][0];
- int cy=y+steps[i][1];
- //cout << x << " " << y << " -> " << cx << " " << cy << " " << sn << endl;
- found=(cx==ex && cy==ey);
- if (cx>0 && cx<=8 && cy>0 && cy<=8)
- put(cx,cy,sn);
- i++;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0);
- freopen("cheval.in","r",stdin);
- freopen("cheval.out","w",stdout);
- startprocess();
- while (qb<=qe && !found) {
- get(x,y,sn);
- sn++;
- if (sn>16) break;
- putall(x,y,sn,found);
- }
- if (!found) {
- cout << "-1";
- } else
- cout << sn;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement