fork(6) download
  1. #include<cstdio>
  2. #include<queue>
  3. using namespace std;
  4.  
  5. char a, b;
  6. int nx, ny, tx, ty;
  7. int mx[] = { -1, -1, 1, 1, -2, -2, 2, 2 };
  8. int my[] = { 2, -2, 2, -2, 1, -1, 1, -1 };
  9. int A[8][8];
  10.  
  11. int main() {
  12. while(scanf("%c%d %c%d\n", &a, &ny, &b, &ty) == 4) {
  13. nx = a - 'a';
  14. ny--;
  15. tx = b - 'a';
  16. ty--;
  17. A[nx][ny] = 0;
  18. queue< pair<int, int> > q;
  19. q.push(make_pair(nx, ny));
  20. while(!q.empty()) {
  21. pair<int, int> c = q.front();
  22. int x = c.first, y = c.second;
  23. if(x == tx && y == ty) break;
  24. q.pop();
  25. for(int i = 0; i < 8; i++) {
  26. if(x + mx[i] >= 0 && x + mx[i] < 8 && y + my[i] >= 0 && y + my[i] < 8) {
  27. q.push(make_pair(x + mx[i], y + my[i]));
  28. A[x + mx[i]][y + my[i]] = A[x][y] + 1;
  29. }
  30. }
  31. }
  32. printf("To get from %c%d to %c%d takes %d knight moves.\n", a, ny + 1, b, ty + 1, A[tx][ty]);
  33. }
  34. }
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
Standard output is empty