Source code for submission s678

grasshop.c

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int R,C, Lr, Lc, Gr, Gc;
  5.  
  6. int get_dist(int a, int b, int x, int y)
  7. {
  8. return fabs(x-a) + fabs(y-b);
  9. }
  10.  
  11. int main()
  12. {
  13. while(scanf("%d %d %d %d %d %d", &R,&C,&Gr,&Gc,&Lr,&Lc) == 6)
  14. {
  15. int d = get_dist(Gr, Gc, Lr, Lc);
  16. if (R == 1 || C == 1) {
  17. printf("impossible\n");
  18. }
  19. else if (R == 2 || C == 2) {
  20. if (d % 3 == 0) {
  21. printf("%d\n", d/3);
  22. } else printf("impossible\n");
  23. } else {
  24. switch(d) {
  25. case 1:
  26. printf("3\n");
  27. break;
  28. case 2:
  29. printf("2\n");
  30. break;
  31. case 3:
  32. printf("1\n");
  33. break;
  34. case 4:
  35. printf("4\n");
  36. break;
  37. case 5:
  38. printf("3\n");
  39. break;
  40. }
  41. if (d > 5) {
  42. int result = (d % 3) > 0 ? 1 : 0;
  43. result += d/3;
  44. printf("%d\n", result);
  45. }
  46. }
  47. }
  48. return 0;
  49. }
  50.