Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define valoare 2000000000
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("rover.in");
  7. ofstream fout("rover.out");
  8.  
  9. int n,g,a[503][503],b[503][503];
  10. queue< pair<int,int> >q;
  11. int dx[] = {1, 0, -1, 0};
  12. int dy[] = {0, 1, 0, -1};
  13.  
  14. void Citire1()
  15. {
  16. int i,j;
  17. fin>>n>>g;
  18. for(i=1;i<=n;i++)
  19. for(j=1;j<=n;j++)
  20. fin>>a[i][j];
  21. }
  22.  
  23. void Citire2()
  24. {
  25. int i,j;
  26. fin>>n;
  27. cout<<"n="<<n<<"\n";
  28. for(i=1;i<=n;i++)
  29. for(j=1;j<=n;j++)
  30. fin>>a[i][j];
  31. }
  32.  
  33. void InitMatB()
  34. {
  35. int i,j;
  36. for(i=1;i<=n;i++)
  37. for(j=1;j<=n;j++)
  38. b[i][j] = -1;
  39. }
  40.  
  41. bool inmatr(int i, int j)
  42. {
  43. if(!(1<=i && i<=n)) return false;
  44. if(!(1<=j && j<=n)) return false;
  45. return true;
  46. }
  47.  
  48. void Cerinta1()
  49. {
  50. int i,j,i1,j1,periculos,k;
  51. InitMatB();
  52. q.push(make_pair(1,1));
  53. b[1][1] = 0;
  54. while(!q.empty())
  55. {
  56. i = q.front().first;
  57. j = q.front().second;
  58. q.pop();
  59. for(k=0;k<4;k++)
  60. {
  61. i1 = i + dx[k];
  62. j1 = j + dy[k];
  63. periculos = 0;
  64. if(a[i1][j1] < g)
  65. periculos = 1;
  66. if(inmatr(i1,j1) && (b[i1][j1] == -1 || (b[i1][j1] > b[i][j]+periculos)))
  67. {
  68. b[i1][j1] = b[i][j] + periculos;
  69. q.push(make_pair(i1,j1));
  70. }
  71. }
  72. }
  73. fout<<b[n][n]<<"\n";
  74. }
  75.  
  76. void Lee2(int gr)
  77. {
  78. int i,j,i1,j1,k;
  79. InitMatB();
  80. cout<<gr<<"*\n";
  81. b[1][1] = 1;
  82. q.push(make_pair(1,1));
  83. while(!q.empty())
  84. {
  85. i = q.front().first;
  86. j = q.front().second;
  87. q.pop();
  88. for(k=0;k<4;k++)
  89. {
  90. i1 = i + dx[k];
  91. j1 = j + dy[k];
  92. if(inmatr(i1,j1) && a[i1][j1] >= gr && (b[i1][j1] == -1 || b[i1][j1] > b[i][j] + 1))
  93. {
  94. b[i1][j1] = b[i][j] + 1;
  95. q.push(make_pair(i1,j1));
  96. }
  97. }
  98. }
  99. for(i=1;i<=n;i++)
  100. {
  101. for(j=1;j<=n;j++)
  102. fout<<b[i][j]<<"\t";
  103. fout<<"\n";
  104. }
  105. }
  106.  
  107. void Cerinta2()
  108. {
  109. int gr,ok,x,sol;
  110. ok = 1;
  111. sol = 0;
  112. while(ok == 1)
  113. {
  114. Lee2(gr);
  115. x = b[n][n];
  116. gr++;
  117. if(x == -1) ok = 0;
  118. else sol = max(sol,x);
  119. }
  120. fout<<sol<<"\n";
  121. }
  122.  
  123. int main()
  124. {
  125. int op;
  126. fin>>op;
  127. cout<<"op="<<op<<"\n";
  128. if(op == 1)
  129. {
  130. cout<<"Solutia1 trebuie facuta\n";
  131. Citire1();
  132. Cerinta1();
  133. }
  134. else
  135. {
  136. cout<<"Solutia2 trebuie facuta\n";
  137. Citire2();
  138. ///Cerinta2();
  139. Lee2(2);
  140. }
  141.  
  142. fin.close();
  143. fout.close();
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement