Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #define dmax 1005
  4. using namespace std;
  5.  
  6. ifstream fin ("traversare.in");
  7. ofstream fout ("traversare.out");
  8.  
  9.  
  10. int n, m, mat[dmax][dmax], mat_copie[dmax][dmax];
  11.  
  12. void citire(){
  13. fin >> n >> m;
  14. for (int i = 1; i <= n; ++i)
  15. for (int j = 1; j <= m; ++j)
  16. fin >> mat[i][j];
  17. }
  18.  
  19. struct Pos{
  20. int lin, col;
  21. };
  22.  
  23. const int dL[] = {0, 0, 1, -1},
  24. dC[] = {1, -1, 0, 0};
  25.  
  26. Pos s, f;
  27.  
  28. queue <int> q;
  29.  
  30. bool OK(int i, int j){
  31. if (i > n || j > m || i < 1 || j < 1)
  32. return false;
  33. if (mat[i][j] == 1)
  34. return false;
  35. return true;
  36. }
  37.  
  38. void lee();
  39.  
  40. void reset(){
  41. for (int i = 1; i <= n; ++i)
  42. for (int j = 1; j <= m; ++j)
  43. mat_copie[i][j] = mat[i][j];
  44. }
  45.  
  46. int main(){
  47. int minim = 1000;
  48. for (int i = 1; i <= n; ++i){
  49. for (int j = 1; j <= m; ++j){
  50. if (mat[1][j] == 0 && mat[n][j]){
  51. s.lin = 1; s.col = j;
  52. lee();
  53. int nr = mat_copie[f.lin][f.col];
  54. reset();
  55. if (minim > nr)
  56. minim = nr;
  57. }
  58. }
  59. }
  60. fout << minim;
  61.  
  62. return 0;
  63. }
  64.  
  65. void lee(){
  66. mat[s.lin][s.col] = 1;
  67. q.push(s);
  68.  
  69. while (!q.empty() && !mat_copie[f.lin][f.col]){
  70. Pos pos;
  71. q.pop();
  72.  
  73. for (int k = 0; k < 4; ++k){
  74. Pos ngh;
  75. ngh.lin = pos.lin + dL[k];
  76. ngh.col = pos.col + dC[k];
  77.  
  78. if (OK(ngh.lin, ngh.col) && !mat_copie[ngh.lin][ngh.col]){
  79. mat_copie[ngh.lin][ngh.col] = mat_copie[ngh.lin][ngh] + 1;
  80. q.push(ngh);
  81. }
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement