Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. #define INF 1000000000
  4. using namespace std;
  5.  
  6. int A[1001][1001],B[1001][1001],n,m,im,jm,ic,jc;
  7. const int di[]={1,-1,0,0},dj[]={0,0,1,-1};// Deplasarea spre magazin
  8. queue<pair<int,int> >Q;// Initializarea cozii
  9.  
  10. void citire()
  11. {
  12. ifstream f("ubuph.in");
  13. f>>n>>m;
  14. for(int i=1;i<=n;++i)
  15. for(int j=1;j<=m;++j)
  16. {
  17. f>>A[i][j];
  18. B[i][j]=INF;
  19. }
  20. f>>im>>jm>>ic>>jc;
  21. f.close();
  22. }
  23.  
  24. bool verif(int i,int j)// Verifica daca pozitia curenta se afla in matrice
  25. {
  26. return 1<=i&&i<=n&&1<=j&&j<=m;
  27. }
  28.  
  29. void Lee()
  30. { B[ic][jc]=A[ic][jc];
  31. while(!Q.empty())
  32. {
  33. pair<int,int> P=Q.front();
  34. for(int k=0;k<4;++k)
  35. {
  36. int i=P.first+di[k],j=P.second+dj[k];
  37. if(verif(i,j)&&B[i][j]>B[P.first][P.second]+A[i][j])
  38. B[i][j]=B[P.first][P.second]+A[i][j],Q.push(make_pair(i,j));
  39. }
  40. Q.pop();
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. citire();
  47. Q.push(make_pair(ic,jc));
  48. Lee();
  49. ofstream g("ubuph.out");
  50. g<<B[im][jm];
  51. g.close();
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement