Source code for submission s653

Go to diff to previous submission

grasshop.cpp

  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <iostream>
  4.  
  5. int ** ARRAY;
  6. int SIZEX;
  7. int SIZEY;
  8. int MINCOUNT;
  9. int leafx, leafy;
  10. /*
  11. int min(int & X, int & Y)
  12. {
  13. if(X>Y) return Y;
  14. else return X;
  15. }
  16. */
  17. void jump(int X, int Y, int count)
  18. {
  19. if(X==(leafx-1) && Y==(leafy-1))
  20. {
  21. if(count < MINCOUNT)
  22. {
  23. ARRAY[X][Y]=count;
  24. MINCOUNT=count;
  25. }
  26. return;
  27. }
  28.  
  29. if(X<0 || X>=SIZEX || Y<0 || Y>=SIZEY || count >= ARRAY[X][Y] || count>=MINCOUNT)
  30. return;
  31.  
  32. ARRAY[X][Y]=count;
  33.  
  34. jump(X+1,Y+2, count+1);
  35. jump(X+2,Y+1, count+1);
  36. jump(X+2,Y-1, count+1);
  37. jump(X+1,Y-2, count+1);
  38. jump(X-1,Y-2, count+1);
  39. jump(X-2,Y-1, count+1);
  40. jump(X-2,Y+1, count+1);
  41. jump(X-1,Y+2, count+1);
  42.  
  43. }
  44.  
  45. int main()
  46. {
  47. int grex, grey;
  48.  
  49. while(scanf("%d %d %d %d %d %d\n", &SIZEX, &SIZEY, &grex, &grey, &leafx, &leafy)==6)
  50. {
  51. MINCOUNT=INT_MAX;
  52. ARRAY=new int *[SIZEX];
  53. for (int i=0; i<SIZEX; i++)
  54. {
  55. ARRAY[i]=new int [SIZEY];
  56. for (int j=0; j<SIZEY; j++)
  57. {
  58. ARRAY[i][j]=INT_MAX;
  59. }
  60. }
  61. jump(grex-1,grey-1,0);
  62. if(ARRAY[leafx-1][leafy-1]==INT_MAX)
  63. printf("impossible\n");
  64. else printf("%d\n", ARRAY[leafx-1][leafy-1]);
  65. }
  66.  
  67. return 0;
  68. }
  69.  

Diff to submission s621

grasshop.cpp

--- c4.s621.cteam009.grasshop.cpp.0.grasshop.cpp
+++ c4.s653.cteam009.grasshop.cpp.0.grasshop.cpp
@@ -1,13 +1,33 @@
 #include <stdio.h>
 #include <limits.h>
+#include <iostream>
 
 int ** ARRAY;
 int SIZEX;
 int SIZEY;
-
+int MINCOUNT;
+int leafx, leafy;
+/*
+int min(int & X, int & Y)
+{
+        if(X>Y) return Y;
+        else return X;
+}
+*/
 void jump(int X, int Y, int count)
 {
-if(X<0 || X>=SIZEX || Y<0 || Y>=SIZEY || count >= ARRAY[X][Y])
+if(X==(leafx-1) && Y==(leafy-1))
+{
+        if(count < MINCOUNT)
+        {
+                ARRAY[X][Y]=count;
+                MINCOUNT=count;
+        }
+        return;
+}
+
+if(X<0 || X>=SIZEX || Y<0 || Y>=SIZEY || count >= ARRAY[X][Y] || count>=MINCOUNT)
 return;
+
 ARRAY[X][Y]=count;
 
@@ -25,8 +45,9 @@
 int main()
 {
-        int grex, grey, leafx, leafy;
+        int grex, grey;
 
         while(scanf("%d %d %d %d %d %d\n", &SIZEX, &SIZEY, &grex, &grey,  &leafx, &leafy)==6)
         {
+                MINCOUNT=INT_MAX;
                 ARRAY=new int *[SIZEX];
                 for (int i=0; i<SIZEX; i++)