#include<bits/stdc++.h>
using namespace std;
int dr[]={-2,-2,+2,+2,+1,-1,+1,-1};
int dc[]={+1,-1,+1,-1,-2,-2,+2,+2};
queue<int>q;
int dist[100][100];
int sr,sc,er,ec;
int vis[100][100];
void bfs(int r,int c)
{
vis[r][c]=1;
dist[r][c]=0;
q.push(r);
q.push(c);
while(!q.empty())
{
int u=q.front();
q.pop();
int v=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int row=dr[i]+u;
int col=dc[i]+v;
if(((row>=0&&row<8)&&(col>=0&&col<8))&&vis[row][col]==0)
{
vis[row][col]=1;
dist[row][col]=dist[u][v]+1;
q.push(row);
q.push(col);
}
}
}
}
main()
{
string s,s1;
while(cin>>s>>s1)
{
sr=int(s[0]-96);
sc=s[1]-'0';
er=int(s1[0]-96);
ec=s1[1]-'0';
memset(dist,0,sizeof(dist));
memset(vis,0,sizeof(vis));
bfs(sr-1,sc-1);
cout<<"To get from "<<s<<" to "<<s1<<" takes "<<dist[er-1][ec-1]<<" knight moves."<<endl;
}
}
using namespace std;
int dr[]={-2,-2,+2,+2,+1,-1,+1,-1};
int dc[]={+1,-1,+1,-1,-2,-2,+2,+2};
queue<int>q;
int dist[100][100];
int sr,sc,er,ec;
int vis[100][100];
void bfs(int r,int c)
{
vis[r][c]=1;
dist[r][c]=0;
q.push(r);
q.push(c);
while(!q.empty())
{
int u=q.front();
q.pop();
int v=q.front();
q.pop();
for(int i=0;i<8;i++)
{
int row=dr[i]+u;
int col=dc[i]+v;
if(((row>=0&&row<8)&&(col>=0&&col<8))&&vis[row][col]==0)
{
vis[row][col]=1;
dist[row][col]=dist[u][v]+1;
q.push(row);
q.push(col);
}
}
}
}
main()
{
string s,s1;
while(cin>>s>>s1)
{
sr=int(s[0]-96);
sc=s[1]-'0';
er=int(s1[0]-96);
ec=s1[1]-'0';
memset(dist,0,sizeof(dist));
memset(vis,0,sizeof(vis));
bfs(sr-1,sc-1);
cout<<"To get from "<<s<<" to "<<s1<<" takes "<<dist[er-1][ec-1]<<" knight moves."<<endl;
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন