Advertisement
Maria_Teodora

Afisare_Lee_fara_vector

Oct 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f ("lee.in");
  6. ofstream g ("lee.out");
  7.  
  8. struct pozitie
  9. {
  10.     int lin,col;
  11. };
  12.  
  13. pozitie c[100],ps,pb;
  14. int n,m,L[100][100],st[20],numar;
  15. int dl[]={-1,0,1,0},dc[]={0,1,0,-1};
  16.  
  17. void citire()
  18. {
  19.     f>>n>>m;
  20.     f>>ps.lin>>ps.col;
  21.     f>>pb.lin>>pb.col;
  22.     for(int i=1;i<=n;i++)
  23.         for(int j=1;j<=m;j++)
  24.             f>>L[i][j];
  25. }
  26.  
  27. void bordare()
  28. {
  29.     for(int i=0;i<=n+1;i++)
  30.         L[i][0]=L[i][m+1]=-1;
  31.     for(int j=0;j<=m+1;j++)
  32.         L[0][j]=L[n+1][j]=-1;
  33. }
  34.  
  35. void Lee()
  36. {
  37.     pozitie pc,pv;
  38.     int p,u;
  39.     p=u=1;
  40.     c[p]=ps;
  41.     L[ps.lin][ps.col]=1;
  42.     while(p<=u && !L[pb.lin][pb.col])
  43.     {
  44.         pc=c[p++];
  45.         for(int i=0;i<4;i++)
  46.         {
  47.             pv.lin=pc.lin+dl[i];
  48.             pv.col=pc.col+dc[i];
  49.             if(L[pv.lin][pv.col]==0)
  50.             {
  51.                 c[++u]=pv;
  52.                 L[pv.lin][pv.col]=1+L[pc.lin][pc.col];
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. void drum(int x, int y)
  59. {
  60.     if(x!=ps.lin || y!=ps.col)
  61.         {
  62.             int vx,vy;
  63.             for(int i=0;i<4;i++)
  64.                 {
  65.                     vx=x+dl[i];
  66.                     vy=y+dc[i];
  67.                     if(L[vx][vy]==L[x][y]-1)
  68.                         drum(vx,vy);
  69.                 }
  70.         }
  71.     g<<"("<<x<<","<<y<<")"<<'\n';
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77.     citire();
  78.     bordare();
  79.     Lee();
  80.     if(L[pb.lin][pb.col]!=0)
  81.         g<<L[pb.lin][pb.col];
  82.     else
  83.         g<<"Nu exista drum.";
  84.     g<<'\n';
  85.     drum(pb.lin,pb.col);
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement