Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- void solve(int rowst, int colst, int rowen, int colen);
- using namespace std;
- void ToNumbers(string start,string end)
- {
- int rowst,colst,rowen,colen;
- if(start.at(0)=='a')
- colst=0;
- else if(start.at(0)=='b')
- colst=1;
- else if(start.at(0)=='c')
- colst=2;
- else if(start.at(0)=='d')
- colst=3;
- else if(start.at(0)=='e')
- colst=4;
- else if(start.at(0)=='f')
- colst=5;
- else if(start.at(0)=='g')
- colst=6;
- else if(start.at(0)=='h')
- colst=7;
- if(start.at(1)=='8')
- rowst=0;
- else if(start.at(1)=='7')
- rowst=1;
- else if(start.at(1)=='6')
- rowst=2;
- else if(start.at(1)=='5')
- rowst=3;
- else if(start.at(1)=='4')
- rowst=4;
- else if(start.at(1)=='3')
- rowst=5;
- else if(start.at(1)=='2')
- rowst=6;
- else if(start.at(1)=='1')
- rowst=7;
- if(end.at(0)=='a')
- colen=0;
- else if(end.at(0)=='b')
- colen=1;
- else if(end.at(0)=='c')
- colen=2;
- else if(end.at(0)=='d')
- colen=3;
- else if(end.at(0)=='e')
- colen=4;
- else if(end.at(0)=='f')
- colen=5;
- else if(end.at(0)=='g')
- colen=6;
- else if(end.at(0)=='h')
- colen=7;
- if(end.at(1)=='8')
- rowen=0;
- else if(end.at(1)=='7')
- rowen=1;
- else if(end.at(1)=='6')
- rowen=2;
- else if(end.at(1)=='5')
- rowen=3;
- else if(end.at(1)=='4')
- rowen=4;
- else if(end.at(1)=='3')
- rowen=5;
- else if(end.at(1)=='2')
- rowen=6;
- else if(end.at(1)=='1')
- rowen=7;
- solve(rowst,colst,rowen,colen);
- }
- void solve(int origX,int origY,int destX,int destY)
- {
- queue<int>x;
- queue<int>y;
- int steps=0;
- bool visited[8][8];
- for(int i=0;i<8;i++)
- {
- for(int j=0;j<8;j++)
- visited[i][j]=false;
- }
- int row[8]={2,2,-2,-2,1,1,-1,-1};
- int col[8]={1,-1,1,-1,2,-2,2,-2};
- x.push(origX);y.push(origY);
- x.push(-1);y.push(-1);
- visited[origX][origY]=true;
- while(!x.empty()&&!y.empty())
- {
- int X=x.front(); x.pop();
- int Y=y.front(); y.pop();
- if(X==-1&&Y==-1)
- {
- steps++;
- x.push(-1);y.push(-1);
- continue;
- }
- if(X==destX&&Y==destY)
- {
- cout<<steps;
- return;
- }
- for(int k=0;k<8;k++)
- {
- int ansx=X+row[k];int ansy=Y+col[k];
- if(ansx<8&&ansx>=0&&ansy<8&&ansy>=0)
- {
- if(!visited[ansx][ansy])
- {
- visited[ansx][ansy]=true;
- x.push(ansx);y.push(ansy);
- }
- }
- }
- }
- }
- int main() {
- // your code here
- int testcases;
- cin>>testcases;
- string start,destination;
- for(int i=0;i<testcases;i++)
- {
- cin>>start>>destination;
- ToNumbers(start,destination);
- if(i!=testcases-1)
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement