Advertisement
Saleh127

UVA 439

Feb 26th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. ll dx[]={2,1,-1,-2,-2,-1,1,2};
  6. ll dy[]={1,2,2,1,-1,-2,-2,-1};
  7. string g,f;
  8. bool vis[10][10];
  9. ll cost[10][10];
  10. void bfs(ll a,ll b,ll c,ll d)
  11. {
  12.  
  13.  
  14. queue<pair<ll,ll>>qq;
  15.  
  16. memset(vis,0,sizeof(vis));
  17.  
  18. qq.push({a,b});
  19.  
  20. vis[a][b]=1;
  21. cost[a][b]=0;
  22.  
  23. while(!qq.empty())
  24. {
  25. ll x=qq.front().first;
  26. ll y=qq.front().second;
  27. qq.pop();
  28. if(x==c && y==d)
  29. {
  30. cout<<"To get from "<<g<<" to "<<f<<" takes "<<cost[x][y]<<" knight moves."<<endl;
  31. return;
  32. }
  33. for(ll i=0;i<8;i++)
  34. {
  35. ll xx=x+dx[i];
  36. ll yy=y+dy[i];
  37. if((xx>0 && xx<=8) && (yy>0 && yy<=8) && vis[xx][yy]==0)
  38. {
  39. vis[xx][yy]=1;
  40. cost[xx][yy]=cost[x][y]+1;
  41. qq.push({xx,yy});
  42. }
  43.  
  44. }
  45. }
  46.  
  47. }
  48.  
  49. int main()
  50. {
  51. ios_base::sync_with_stdio(0);
  52. cin.tie(0);cout.tie(0);
  53.  
  54.  
  55. while(cin>>g)
  56. {
  57. cin>>f;
  58.  
  59. ll ax=g[0]-96;
  60. ll ay=g[1]-'0';
  61. ll bx=f[0]-96;
  62. ll by=f[1]-'0';
  63.  
  64. bfs(ax,ay,bx,by);
  65. }
  66.  
  67.  
  68. return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement