nicuvlad76

Untitled

Jan 13th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 1005
  3. using namespace std;
  4. ifstream fin("roboti.in");
  5. ofstream fout("roboti.out");
  6. ///Lee -864
  7. int a[N][N],n,m;
  8. int li,ci,ls,cs;
  9. int dl[]= {-1,0,1,0};
  10. int dc[]= {0,-1,0,1};
  11. ///coada
  12. queue<pair <int,int> > q;
  13. void Citire()
  14. {
  15. fin>>n>>m;
  16. for(int i=1; i<=n; i++)
  17. for(int j=1; j<=m; j++)
  18. {
  19. fin>>a[i][j];
  20. a[i][j]=-a[i][j];
  21.  
  22. }
  23. fin>>li>>ci>>ls>>cs;
  24. }
  25. bool Inside(int i, int j)
  26. {
  27. return i>=1 && i<=n && j>=1 && j<=m;
  28. }
  29. void Lee(int li, int ci)
  30. {
  31. int l,c, k,lv,cv;
  32. q.push({li,ci});
  33. a[li][ci]=1;
  34. while(!q.empty()&&a[ls][cs]==0)
  35. {
  36. l=q.front().first;
  37. c=q.front().second;
  38. q.pop();
  39. for(k=0;k<4;k++)
  40. {
  41. lv=l+dl[k];
  42. cv=c+dc[k];
  43. if(Inside(lv,cv)&&a[lv][cv]==0)
  44. {
  45. q.push({lv,cv});
  46. a[lv][cv]=a[l][c]+1;
  47. }
  48. }
  49. }
  50.  
  51.  
  52. }
  53. int main()
  54. {
  55. Citire();
  56. Lee(li,ci);
  57. if(a[ls][cs]==0)fout<<-1;
  58. else fout<<a[ls][cs]-1;
  59. return 0;
  60. }
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment