Source code for submission s1286

Go to diff to previous submission

grassshop.cpp

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int pole[200][200];
  5. int posSx, posSy;
  6. int posFx, posFy;
  7. int sizex, sizey;
  8.  
  9. void check(int x, int y, int jump)
  10. {
  11. if(x > -1 && y > -1 && x < 200 && y < 200)
  12. {
  13. if(pole[x][y]>jump)
  14. {
  15. pole[x][y] = jump;
  16. check(x+2, y+1,jump+1);
  17. check(x+2, y-1,jump+1);
  18. check(x-2, y+1,jump+1);
  19. check(x-2, y-1,jump+1);
  20. check(x+1, y+2,jump+1);
  21. check(x+1, y-2,jump+1);
  22. check(x-1, y+2,jump+1);
  23. check(x-1, y-2,jump+1);
  24.  
  25.  
  26. }
  27. }
  28.  
  29. }
  30.  
  31. bool backt;
  32. void back(int x, int y, int num)
  33. {
  34. if(x > 100 - posSx && y > 100 - posSy && x < 100 - posSx + sizex && y < 100 - posSy + sizey)
  35. {
  36. if(pole[x][y] == num)
  37. {
  38. if(num == 0)
  39. backt = true;
  40. else
  41. {
  42. back(x+2, y+1,num-1);
  43. back(x+2, y-1,num-1);
  44. back(x-2, y+1,num-1);
  45. back(x-2, y-1,num-1);
  46. back(x+1, y+2,num-1);
  47. back(x+1, y-2,num-1);
  48. back(x-1, y+2,num-1);
  49. back(x-1, y-2,num-1);
  50. }
  51.  
  52. }
  53. }
  54.  
  55. }
  56.  
  57.  
  58.  
  59. int main(void)
  60. {
  61. for(int i = 0; i<200; i++)
  62. for(int j = 0; j<200; j++)
  63. pole[i][j] = 1000;
  64.  
  65. check(100, 100, 0);
  66.  
  67. while(scanf("%d %d %d %d %d %d", &sizex, &sizey, &posSx, &posSy, &posFx, &posFy) == 6)
  68. {
  69. backt = false;
  70. back(posFx - posSx + 100, posFy + 100 - posSy, pole[posFx - posSx + 100][posFy + 100 - posSy]);
  71. if(backt)
  72. printf("%d\n", pole[posFx - posSx + 100][posFy + 100 - posSy]);
  73. else
  74. printf("impossible\n");
  75.  
  76. }
  77.  
  78. return 0;
  79.  
  80. }
  81.  

Diff to submission s1193

grassshop.cpp

--- c4.s1193.cteam107.grasshop.cpp.0.grassshop.cpp
+++ c4.s1286.cteam107.grasshop.cpp.0.grassshop.cpp
@@ -2,5 +2,5 @@
 #include <stdio.h>
 
-int pole[100][100];
+int pole[200][200];
 int posSx, posSy;
 int posFx, posFy;
@@ -9,12 +9,9 @@
 void check(int x, int y, int jump)
 {
-        if(jump > sizex * 3/2 && jump > sizey * 3/2)
-                return;
-        if(x > -1 && y > -1 && x < sizex && y < sizey)
+        if(x > -1 && y > -1 && x < 200 && y < 200)
         {
                 if(pole[x][y]>jump)
-{               
+                {               
                         pole[x][y] = jump;
-                        
                                 check(x+2, y+1,jump+1);
                                 check(x+2, y-1,jump+1);
@@ -32,29 +29,49 @@
 }
 
+bool backt;
+void back(int x, int y, int num)
+{
+        if(x > 100 - posSx && y > 100 - posSy && x < 100 - posSx + sizex && y < 100 - posSy + sizey)
+        {
+                if(pole[x][y] == num)
+                {               
+                        if(num == 0)
+                                backt = true;
+                        else
+                        {
+                                back(x+2, y+1,num-1);
+                                back(x+2, y-1,num-1);
+                                back(x-2, y+1,num-1);
+                                back(x-2, y-1,num-1);
+                                back(x+1, y+2,num-1);
+                                back(x+1, y-2,num-1);
+                                back(x-1, y+2,num-1);
+                                back(x-1, y-2,num-1);
+                        }
+                        
+                }
+        }
+
+}
+
+
 
 int main(void)
 {
+        for(int i = 0; i<200; i++)
+                for(int j = 0; j<200; j++)
+                        pole[i][j] = 1000;
+        
+        check(100, 100, 0);
 
         while(scanf("%d %d %d %d %d %d", &sizex, &sizey, &posSx, &posSy, &posFx, &posFy) == 6)
         {
-                for(int i = 0; i<sizex; i++)
-                        for(int j = 0; j<sizey; j++)
-                                pole[i][j] = 1000;
-
-                check(posSx-1, posSy-1, 0);
-                
-                                
-                /*for(int i = 0; i<sizex; i++)
-                {
-                        for(int j = 0; j<sizey; j++)
-                                printf("%d", pole[i][j]);
-                        printf("\n");
-                }*/
-
-
-                if(pole[posFx-1][posFy-1]==1000)
-                        printf("impossible\n");
+                backt = false;
+                back(posFx - posSx + 100, posFy + 100 - posSy, pole[posFx - posSx + 100][posFy + 100 - posSy]);
+                if(backt)
+                        printf("%d\n", pole[posFx - posSx + 100][posFy + 100 - posSy]);
                 else
-                        printf("%d\n", pole[posFx-1][posFy-1]);
+                        printf("impossible\n");
+
         }