Source code for submission s837

Go to diff to previous submission

grasshopper.cpp

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <queue>
  4. #define INF 999999
  5.  
  6. using namespace std;
  7.  
  8. unsigned int map[200][200];
  9.  
  10. unsigned int dir[8][2] = {{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}};
  11.  
  12. typedef struct point{
  13. point( int a, int b ) : x(a), y(b){}
  14. int x, y;
  15. } t_point;
  16.  
  17. bool canHop( int r, int s, int x, int y ) {
  18. return (x < r) && ( y < s) && ( x >= 0) && ( y >= 0) && (map[x][y]==INF);
  19. }
  20.  
  21. int main( int argc, char ** argv ) {
  22. int r,c,gr,gc,lr,lc;
  23.  
  24. while( scanf("%d %d %d %d %d %d",&r,&c,&gr,&gc,&lr,&lc) == 6) {
  25. for( int i = 0; i < r; i++)
  26. for( int j = 0; j < c; j++ )
  27. map[i][j] = INF;
  28. queue<t_point> q;
  29. //q.clean();
  30. //r--;c--;
  31. gr--;gc--;
  32. lr--;lc--;
  33.  
  34. map[gr][gc] = 0;
  35. t_point point(gr,gc);
  36. q.push( point );
  37.  
  38. bool found = false;
  39.  
  40. while( !q.empty() ) {
  41. point = q.front();
  42. q.pop();
  43. for( int i = 0; i < 8; i++) {
  44. t_point tmp( point.x + dir[i][0], point.y + dir[i][1]);
  45. if( canHop(r,c,tmp.x,tmp.y) ) {
  46. //if( (tmp.x == lr) && (tmp.y == lc)) {
  47. // printf()
  48. // }
  49. map[ tmp.x][ tmp.y ] = map[ point.x ][ point.y ] + 1;
  50. // printf("%d %d\n", tmp.x + 1, tmp.y + 1);
  51. q.push( tmp );
  52. }
  53. }
  54. }
  55.  
  56. if( map[lr][lc] == INF ) {
  57. printf("impossible\n");
  58. } else {
  59. printf("%d\n",map[lr][lc]);
  60. }
  61.  
  62. }
  63. }

Diff to submission s814

grasshopper.cpp

--- c4.s814.cteam005.grasshop.cpp.0.grasshopper.cpp
+++ c4.s837.cteam005.grasshop.cpp.0.grasshopper.cpp
@@ -6,5 +6,5 @@
 using namespace std;
 
-unsigned int map[100][100];
+unsigned int map[200][200];
 
 unsigned int dir[8][2] = {{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}};
@@ -15,5 +15,5 @@
 } t_point;
 
-inline bool canHop( int r, int s, int x, int y ) {
+bool canHop( int r, int s, int x, int y ) {
    return (x < r) && ( y < s) && ( x >= 0) && ( y >= 0) && (map[x][y]==INF);
 }
@@ -21,11 +21,11 @@
 int main( int argc, char ** argv ) {
   int r,c,gr,gc,lr,lc;
-  queue<t_point> q;
-  while( scanf("%d %d %d %d %d %d",&r,&c,&gr,&gc,&lr,&lc) ) {
+ 
+  while( scanf("%d %d %d %d %d %d",&r,&c,&gr,&gc,&lr,&lc) == 6) {
     for( int i = 0; i < r; i++) 
       for( int j = 0; j < c; j++ ) 
         map[i][j] = INF;
-     
-      //q.clear();
+      queue<t_point> q;
+      //q.clean();
       //r--;c--;
       gr--;gc--;
@@ -36,4 +36,6 @@
         q.push( point );
         
+        bool found = false;
+        
         while( !q.empty() ) {
            point = q.front();
@@ -42,4 +44,7 @@
              t_point tmp( point.x + dir[i][0], point.y + dir[i][1]);
             if( canHop(r,c,tmp.x,tmp.y) ) {
+              //if( (tmp.x == lr) && (tmp.y == lc)) {
+        //      printf()
+          //    }
               map[ tmp.x][ tmp.y ] = map[ point.x ][ point.y ] + 1;
              // printf("%d %d\n", tmp.x + 1, tmp.y + 1);