Advertisement
Bulboaca_Eugen

Untitled

Jun 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int matrix[501][501];
  4. bool verified[501][501];
  5. int distances[501][501];
  6. int dx[4]={1, 0, -1, 0};
  7. int dy[4]={0, -1, 0, 1};
  8. deque <pair<int,int>> coada;
  9. queue <pair<int,int>> C;
  10. int isInside(int x, int y, int n, int m){
  11. return 1<=x and x<=n and 1<=y and y<=m;}
  12. void leeAlg(int g, int n, int m){
  13. distances[1][1]=0;
  14. coada.push_front({1, 1});
  15. while(coada.size()){
  16. pair <int, int> Element=coada.front();
  17. coada.pop_front();
  18. int currentx=Element.first;
  19. int currenty=Element.second;
  20. for(int i=0; i <= 3; i++){
  21. int newx=currentx+dx[i];
  22. int newy=currenty+dy[i];
  23. if(verified[newx][newy]==true)
  24. continue;
  25. if(isInside(newx, newy, n, m)==false)
  26. continue;
  27. if(matrix[newx][newy]<g){
  28. distances[newx][newy]=distances[currentx][currenty]+1;
  29. coada.push_back({newx, newy});
  30. }
  31. else {
  32. coada.push_front({newx, newy});
  33. distances[newx][newy]=distances[currentx][currenty];
  34. }
  35. verified[newx][newy]=true;
  36. }
  37. }
  38. }
  39. bool lee2(int g, int n, int m){
  40. for(int i=1; i <= n; i++)
  41. for(int j=1; j <= m; j++){
  42. verified[i][j]=0;
  43. distances[i][j]=0;
  44. }
  45. C.push({1,1});
  46. while(C.size()){
  47. pair<int, int> elem=C.front();
  48. C.pop();
  49. int acmx=elem.first;
  50. int acmy=elem.second;
  51. for(int i=1; i <= 3; i++){
  52. int noux=acmx+dx[i];
  53. int nouy=acmy+dy[i];
  54. if(isInside(noux, nouy, n, m) && verified[noux][nouy]==false ){
  55. if(matrix[noux][nouy]>=g){
  56. distances[noux][nouy]=1;
  57. verified[noux][nouy]=1;
  58. C.push({noux, nouy});
  59. }
  60. else verified[noux][nouy]=1;
  61. }
  62. }
  63. }
  64. return distances[n][n];
  65. }
  66. int cautbin(int n){
  67. int st=0, dr=5000, mij=0;
  68. while(st<dr){
  69. mij=(st+dr+1)/2;
  70. if(lee2(mij, n, n))
  71. st=mij;
  72. else dr=mij-1;
  73. }
  74. return st;
  75. }
  76. ifstream fin("rover.in");
  77. ofstream fout("rover.out");
  78. int main()
  79. {
  80. int v, n, g;
  81. fin >> v >> n ;
  82. if(v==1)
  83. fin >> g;
  84. for(int i=1; i <= n; i++)
  85. for(int j=1; j <= n; j++)
  86. fin >> matrix[i][j];
  87. if(v==1){
  88. leeAlg(g, n, n);
  89. fout << distances[n][n];
  90. }
  91. else{
  92. fout << cautbin(n);
  93. }
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement