yoyoboyss

Untitled

Mar 8th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int NMAX=505;
  5.  
  6. int dy[4]={0, 0, -1, 1};
  7. int dx[4]={-1, 1, 0, 0};
  8.  
  9. ifstream fin("rover.in");
  10. ofstream fout("rover.out");
  11. deque <pair <int , int> > Q;
  12.  
  13. int H[NMAX][NMAX], Lee[NMAX][NMAX];
  14. int N, cerinta, G;
  15.  
  16.  
  17. bool OK(int i, int j){
  18. if(i < 0 || j < 0 || i >= N || j >= N){
  19. return false;
  20. }
  21. return true;
  22. }
  23.  
  24. int read(){
  25. fin >> cerinta >> N;
  26. if(cerinta==1){
  27. fin >> G;
  28. }
  29. for(int i = 0; i < N; i++){
  30. for(int j=0; j<N; j++){
  31. fin >> H[i][j];
  32. }
  33. }
  34.  
  35. if (cerinta==1){
  36. for (int i = 0; i < N; i++) {
  37. for (int j = 0; j < N; j++) {
  38. Lee[i][j] = INT_MAX;
  39. }
  40. }
  41. }
  42. }
  43.  
  44. int lee_rez_1(){
  45. int i, j, i_1, j_1;
  46. Lee[0][0]=0;
  47. Q.push_back(make_pair(0, 0));
  48.  
  49. while(!Q.empty()){
  50. i = Q.front().first;
  51. j = Q.front().second;
  52. Q.pop_front();
  53. for(int d=0; d<4; d++){
  54. i_1 = i + dy[d];
  55. j_1 = j + dx[d];
  56. if(H[i_1][j_1] < G){
  57. if(OK(i_1, j_1) && Lee[i_1][j_1] > Lee[i][j] + 1){
  58. Lee[i_1][j_1] = Lee[i][j] + 1;
  59. Q.push_back(make_pair(i_1, j_1));
  60. }
  61. } else {
  62. if(OK(i_1, j_1) && Lee[i_1][j_1] > Lee[i][j]){
  63. Lee[i_1][j_1] = Lee[i][j];
  64. Q.push_front(make_pair(i_1, j_1));
  65. }
  66. }
  67. }
  68. }
  69. }
  70.  
  71. int lee_rez_2(){
  72. int i, j, i_1, j_1;
  73. Lee[0][0]=H[0][0];
  74. Q.push_back(make_pair(0, 0));
  75. while(!Q.empty()){
  76. i = Q.front().first;
  77. j = Q.front().second;
  78. Q.pop_front();
  79. for(int d=0; d<4; d++){
  80. i_1 = i + dy[d];
  81. j_1 = j + dx[d];
  82. if(H[i_1][j_1] < Lee[i][j]){
  83. if(OK(i_1, j_1)){
  84. Lee[i_1][j_1]=H[i_1][j_1];
  85. Q.push_back(make_pair(i_1, j_1));
  86. }
  87. } else if(H[i_1][j_1] >= Lee[i][j] && Lee[i_1][j_1] < Lee[i][j]){
  88. if(OK(i_1, j_1)){
  89. Lee[i_1][j_1] = Lee[i][j];
  90. Q.push_front(make_pair(i_1, j_1));
  91. }
  92. }
  93. }
  94. }
  95. }
  96.  
  97. int main() {
  98. read();
  99.  
  100. if(cerinta==1){
  101. lee_rez_1();
  102. fout << Lee[N-1][N-1];
  103. } else {
  104. lee_rez_2();
  105. fout << Lee[N-1][N-1];
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment