Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define nmax 505
  3.  
  4. using namespace std;
  5.  
  6. const short dx[ ] = {-1,0,1,0};
  7. const short dy[ ] = {0,1,0,-1};
  8. deque <pair <int, int > > d;
  9. int n, G, q;
  10. int a[ nmax ][ nmax ], v[ nmax ][ nmax ];
  11. ifstream f("rover.in");
  12. ofstream g("rover.out");
  13.  
  14. void citire(){
  15. f >> q;
  16. if (q == 1)
  17. f >> G;
  18. for (int i = 1; i <= n; i++)
  19. for (int j = 1; j <= n; j++)
  20. f >> a[ i ][ j ];
  21. }
  22.  
  23. bool bun( int x, int y ){
  24. return (x >= 0) && (x <= n) && (y >= 0) && (y <= n);
  25. }
  26.  
  27. void q1(){
  28. memset(v, -1, sizeof( v ));
  29. d.push_back(make_pair(1, 1));
  30. v[ 1 ][ 1 ] = 0;
  31. while (!d.empty()){
  32. int x = d.front().first, y = d.front().second;
  33. d.pop_front();
  34. for (int i = 0; i < 4; i++){
  35. int nx = x + dx[ i ];
  36. int ny = y + dy[ i ];
  37. if (bun( nx, ny ) && v[ nx ][ ny ] == -1)
  38. if (a[ nx ][ ny ] < G){
  39. v[ nx ][ ny ] = v[ x ][ y ] + 1;
  40. d.push_back(make_pair( nx, ny ));
  41. }
  42. else{
  43. v[ nx ][ ny ] = v[ x ][ y ];
  44. d.push_front(make_pair( nx, ny ));
  45. }
  46. }
  47. }
  48. g << v[ n ][ n ];
  49. }
  50.  
  51.  
  52.  
  53. int main(){
  54. citire();
  55. //if (q == 1)
  56. q1();
  57. // else
  58. // q2();
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement