Advertisement
a53

Rover

a53
Mar 14th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. // prof. Mircea Lupse-Turpan - Liceul Teoretic Grigore Moisil Timisoara - 100 puncte
  2. #include <fstream>
  3. #include <deque>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. ifstream fin("rover.in");
  8. ofstream fout("rover.out");
  9.  
  10. const int NMax = 505;
  11. const int oo = 10000;
  12. int A[NMax][NMax],DP[NMax][NMax];
  13. int V,N,G;
  14. int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
  15. deque <pair <int,int> > Q;
  16.  
  17. void Read()
  18. {
  19. fin >> V >> N;
  20. if(V == 1) fin >> G;
  21. for(int i = 1; i <= N; ++i)
  22. for(int j = 1; j <= N; ++j)
  23. fin >> A[i][j];
  24. }
  25.  
  26. inline bool Inside(int x, int y)
  27. {
  28. return ( (x >= 1) && (x <= N) && (y >= 1) && (y <= N) );
  29. }
  30.  
  31. void Solve1()
  32. {
  33. memset(DP,-1,sizeof(DP));
  34. Q.push_back(make_pair(1,1));
  35. DP[1][1] = 0;
  36.  
  37. while(!Q.empty())
  38. {
  39. int X = Q.front().first, Y = Q.front().second;
  40. Q.pop_front();
  41. for(int k = 0; k < 4; k++)
  42. {
  43. int NewX = X + dx[k], NewY = Y + dy[k];
  44. if(Inside(NewX,NewY) && DP[NewX][NewY] == -1)
  45. {
  46. if(A[NewX][NewY] < G)
  47. {
  48. DP[NewX][NewY] = DP[X][Y] + 1;
  49. Q.push_back(make_pair(NewX,NewY));
  50. }
  51. else
  52. {
  53. DP[NewX][NewY] = DP[X][Y];
  54. Q.push_front(make_pair(NewX,NewY));
  55. }
  56. }
  57. }
  58. }
  59. fout << DP[N][N] << "\n";
  60. }
  61.  
  62. bool Lee(int Value)
  63. {
  64. memset(DP,-1,sizeof(DP));
  65. Q.push_back(make_pair(1,1));
  66. DP[1][1] = 0;
  67. while(!Q.empty())
  68. {
  69. int X = Q.front().first, Y = Q.front().second;
  70. Q.pop_front();
  71. for(int k = 0; k < 4; k++)
  72. {
  73. int NewX = X + dx[k], NewY = Y + dy[k];
  74. if(Inside(NewX,NewY) && DP[NewX][NewY] == -1)
  75. {
  76. if(A[NewX][NewY] >= Value)
  77. {
  78. DP[NewX][NewY] = DP[X][Y] + 1;
  79. Q.push_back(make_pair(NewX,NewY));
  80. }
  81. }
  82. }
  83. }
  84. return (DP[N][N] != -1);
  85. }
  86.  
  87. void Solve2()
  88. {
  89. int Left = 1, Right = oo, Sol = -1;
  90.  
  91. while(Left <= Right)
  92. {
  93. int Mid = (Left + Right) / 2;
  94. if(Lee(Mid))
  95. {
  96. Sol = Mid;
  97. Left = Mid + 1;
  98. }
  99. else
  100. Right = Mid - 1;
  101. }
  102. fout << Sol << "\n";
  103. }
  104.  
  105.  
  106. int main()
  107. {
  108. Read();
  109. if(V == 1)
  110. Solve1();
  111. else
  112. Solve2();
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement