Advertisement
Maria_Teodora

Lee_cu_liste

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