Se pleaca din (x,y) si se ajunge (z,w). Sa se afiseze drumul minim.
#include<iomanip>
#include<iostream>
#include<fstream>
using namespace std;
int n,m,L[10][10],sol[10][10],nrsol=0,x,y,z,w,drmin[10][10],mi;
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
void citire()
{int i,j;
ifstream f("labi");
f>>n>>m>>x>>y>>z>>w;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
f>>L[i][j];
}
void afisare()
{int i,j;
nrsol++;
cout<<"Solutia nr "<<nrsol<<endl;
for(i=1;i<=n;i++)
{for(j=1;j<=m;j++)
cout<<setw(3)<<sol[i][j];
cout<<endl;}
cout<<endl;
}
void traseu(int i, int j, int pas)
{int inou,jnou,k,ii,jj;
for(k=0;k<4;k++)
{inou=i+dx[k];
jnou=j+dy[k];
if(inou>=1 && inou<=n && jnou>=1 && jnou<=m)
if(L[inou][jnou]==0 && sol[inou][jnou]==0)
{sol[inou][jnou]=pas;
if(inou==z && jnou==w)
{
afisare();
if(pas<mi)
{mi=pas;
for(ii=1;ii<=n;ii++)
for(jj=1;jj<=m;jj++)
drmin[ii][jj]=sol[ii][jj];}
}
traseu(inou,jnou,pas+1);
sol[inou][jnou]=0;}
}
}
int main()
{int i,j;
citire();
mi=n*m;
sol[x][y]=1;
traseu(x,y,2);
cout<<"Sol totale "<<nrsol<<endl;
cout<<endl<<"Drumul 8im"<<endl;
for(i=1;i<=n;i++)
{for(j=1;j<=m;j++)
cout<<setw(3)<<drmin[i][j];
cout<<endl;}
}