Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- ifstream f("matrice.in");
- ofstream g ("matrice.out");
- struct nod
- {
- int x, y;
- nod *urm;
- };
- int l[100][100],n,m;
- int dl[]={-1,0,1,0}, dc[]={0,1,0,-1};
- nod *ps, *pb;
- nod *p=NULL, *u=NULL;
- void citire()
- {
- f>>n>>m;
- ps=new nod;
- pb=new nod;
- f>>ps->x>>ps->y;
- f>>pb->x>>pb->y;
- ps->urm=NULL;
- pb->urm=NULL;
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++)
- f>>l[i][j];
- }
- void adaugare(nod *d)
- {
- if(p==NULL)
- p=u=d;
- else
- {
- u->urm=d;
- u=d;
- }
- }
- void bordare()
- {
- for(int i=0;i<=n+1;i++)
- l[i][0]=l[i][m+1]=-1;
- for(int j=0;j<=m+1;j++)
- l[0][j]=l[n+1][j]=-1;
- }
- void Lee()
- {
- nod *aux;
- adaugare(ps);
- l[ps->x][ps->y]=1;
- while(p!=NULL && l[pb->x][pb->y]==0)
- {
- for(int i=0;i<4;i++)
- {
- aux=new nod;
- aux->x=p->x+dl[i];
- aux->y=p->y+dc[i];
- aux->urm=NULL;
- if(l[aux->x][aux->y]==0)
- {
- nod *z;
- z=p;
- p=p->urm;
- delete z;
- l[aux->x][aux->y]=l[u->x][u->y]+1;
- adaugare(aux);
- }
- }
- }
- }
- int main()
- {
- bordare();
- if(l[pb->x][pb->y]==0)
- g<<"Nu exista drum.";
- else
- g<<l[pb->x][pb->y];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement