Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define N 15
- using namespace std;
- ifstream fin("soarece1.in");
- ofstream fout("soarece1.out");
- int a[N][N],n,m,xs,ys,xb,yb;
- int dl[]={1,-1,0,0};
- int dc[]={0,0,1,-1};
- char c;
- void Citire()
- {
- int i,j;
- fin>>n>>m;
- for(i=1;i<=n;i++)
- for(j=1;j<=m;j++)
- {
- fin>>c;
- if(c=='#')a[i][j]=-1;
- if(c=='_')a[i][j]=0;
- if(c=='S'){xs=i,ys=j; a[i][j]=0;}
- if(c=='B'){xb=i,yb=j; a[i][j]=0;}
- }
- }
- bool Inside(int i, int j)
- {return 1<=i &&i<=n &&1<=j&&j<=m;}
- void Afisare()
- {
- int i,j;
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- if(a[i][j]<0)fout<<"0 ";
- else fout<<a[i][j]<<" ";
- fout<<'\n';
- }
- }
- void Back(int l, int c, int k)
- {
- int ln,cn;
- if(a[l][c]==0)
- {
- a[l][c]=k;
- if(l==xb && c==yb){Afisare(); return;}
- else
- {
- for(int i=0;i<4;i++)
- {
- ln=l+dl[i];
- cn=c+dc[i];
- if(Inside(ln,cn)==1)Back(ln,cn,k+1);
- }
- }
- a[l][c]=0;
- }
- }
- void Zero()
- {int i,j;
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++) fout<<"0 ";
- fout<<'\n';
- }
- }
- int main()
- {
- Citire();
- Back(xs,ys,1);
- Zero();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment