Source code for submission s744

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. --gr; --gc; --lr; --lc;
  42. FOR(i,0,R)FOR(j,0,C) vis[ i ][ j ] = false;
  43. int res = -1;
  44. queue< State > q;
  45. State st;
  46. st.x = gc;
  47. st.y = gr;
  48. st.d = 0;
  49. q.push( st );
  50. while ( !q.empty() )
  51. {
  52. State s = q.front();
  53. q.pop();
  54.  
  55. if ( s.x == lc && s.y == lr )
  56. {
  57. res = s.d;
  58. break;
  59. }
  60.  
  61. State ns;
  62. FOR(i,0,8)
  63. {
  64. ns.x = s.x + dx[i];
  65. ns.y = s.y + dy[i];
  66. ns.d = s.d + 1;
  67. if ( ns.x < 0 || ns.y < 0 || ns.x >= C || ns.y >= R || vis[ ns.y ][ ns.x ] )
  68. continue;
  69. vis[ ns.y ][ ns.x ] = true;
  70. q.push( ns );
  71. }
  72. }
  73.  
  74. if ( res == -1 )
  75. printf( "impossible\n" );
  76. else
  77. printf( "%d\n", res );
  78. }
  79. return 0;
  80.  
  81. }
  82.  

Diff to submission s726

grasshop.cpp

--- c4.s726.cteam004.grasshop.cpp.0.grasshop.cpp
+++ c4.s744.cteam004.grasshop.cpp.0.grasshop.cpp
@@ -39,4 +39,5 @@
     while ( scanf( "%d %d %d %d %d %d", &R, &C, &gr, &gc, &lr, &lc ) == 6 )
     {
+        --gr; --gc; --lr; --lc;
     FOR(i,0,R)FOR(j,0,C) vis[ i ][ j ] = false;
         int res = -1;