Source code for submission s722

Go to diff to previous submission

grasshop.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <ctype.h>
  6. #include <math.h>
  7.  
  8. #include <string>
  9. #include <vector>
  10. #include <map>
  11. #include <set>
  12. #include <algorithm>
  13. #include <queue>
  14.  
  15. using namespace std;
  16.  
  17. typedef pair <int, int> PII;
  18. typedef long long int LL;
  19. typedef vector <int> VI;
  20. typedef vector <LL> VLL;
  21.  
  22. #define FOR(i, a, b) for ( int i = a; i < b; ++i )
  23. #define FORD(i, a, b) for ( int i = a-1; i >= 0; --i )
  24.  
  25. #define FILL(x, v, n) for ( int _i = 0; _i < n; ++_i ) x[_i] = v;
  26.  
  27. int dx[ 8 ] = { 2, 2, -2, -2, 1, 1, -1, -1 };
  28. int dy[ 8 ] = { 1, -1, 1, -1, 2, -2, 2, -2 };
  29.  
  30. bool vis[ 101 ][ 101 ];
  31. struct State
  32. {
  33. int x, y, d;
  34. };
  35.  
  36. int main()
  37. {
  38. int R, C, gr, gc, lr, lc;
  39. while ( scanf( "%d %d %d %d %d %d", &R, &C, &gr, &gc, &lr, &lc ) = = 6 )
  40. {
  41. FOR(i,0,R)FOR(j,0,C) vis[ i ][ j ] = false;
  42. int res = -1;
  43. queue< State > q;
  44. State st;
  45. st.x = gc;
  46. st.y = gr;
  47. st.d = 0;
  48. q.push( st );
  49. while ( !q.empty() )
  50. {
  51. State s = q.front();
  52. q.pop();
  53.  
  54. if ( s.x == lc && s.y == lr )
  55. {
  56. res = s.d;
  57. break;
  58. }
  59.  
  60. State ns;
  61. FOR(i,0,8)
  62. {
  63. ns.x = s.x + dx[i];
  64. ns.y = s.y + dy[i];
  65. ns.d = s.d + 1;
  66. if ( ns.x < 0 || ns.y < 0 || ns.x >= C || ns.y >= R || vis[ ns.y ][ ns.x ] )
  67. continue;
  68. vis[ ns.y ][ ns.x ] = true;
  69. q.push( ns );
  70. }
  71. }
  72.  
  73. if ( res == -1 )
  74. printf( "impossible\n" );
  75. else
  76. printf( "%d\n", res );
  77. }
  78. return 0;
  79.  
  80. }
  81.  

Diff to submission s704

grasshop.cpp

--- c4.s704.cteam004.grasshop.cpp.0.grasshop.cpp
+++ c4.s722.cteam004.grasshop.cpp.0.grasshop.cpp
@@ -37,6 +37,7 @@
 {
     int R, C, gr, gc, lr, lc;
-    while ( scanf( "%d %d %d %d %d %d", &R, &C, &gr, &gc, &lr, &lc ) == 6 )
+    while ( scanf( "%d %d %d %d %d %d", &R, &C, &gr, &gc, &lr, &lc ) = = 6 )
     {
+    FOR(i,0,R)FOR(j,0,C) vis[ i ][ j ] = false;
         int res = -1;
         queue< State > q;
@@ -57,5 +58,4 @@
             }
 
-            FOR(i,0,R)FOR(j,0,C) vis[ i ][ j ] = false;
             State ns;
             FOR(i,0,8)