nicuvlad76

Untitled

Dec 17th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 15
  3. using namespace std;
  4. ifstream fin("soarece1.in");
  5. ofstream fout("soarece1.out");
  6. int a[N][N],n,m,xs,ys,xb,yb;
  7. int dl[]={1,-1,0,0};
  8. int dc[]={0,0,1,-1};
  9. char c;
  10. void Citire()
  11. {
  12. int i,j;
  13. fin>>n>>m;
  14. for(i=1;i<=n;i++)
  15. for(j=1;j<=m;j++)
  16. {
  17. fin>>c;
  18. if(c=='#')a[i][j]=-1;
  19. if(c=='_')a[i][j]=0;
  20. if(c=='S'){xs=i,ys=j; a[i][j]=0;}
  21. if(c=='B'){xb=i,yb=j; a[i][j]=0;}
  22. }
  23. }
  24.  
  25. bool Inside(int i, int j)
  26. {return 1<=i &&i<=n &&1<=j&&j<=m;}
  27.  
  28. void Afisare()
  29. {
  30. int i,j;
  31. for(i=1;i<=n;i++)
  32. {
  33. for(j=1;j<=m;j++)
  34. if(a[i][j]<0)fout<<"0 ";
  35. else fout<<a[i][j]<<" ";
  36. fout<<'\n';
  37. }
  38. }
  39. void Back(int l, int c, int k)
  40. {
  41. int ln,cn;
  42. if(a[l][c]==0)
  43. {
  44. a[l][c]=k;
  45. if(l==xb && c==yb){Afisare(); return;}
  46. else
  47. {
  48. for(int i=0;i<4;i++)
  49. {
  50. ln=l+dl[i];
  51. cn=c+dc[i];
  52. if(Inside(ln,cn)==1)Back(ln,cn,k+1);
  53. }
  54. }
  55. a[l][c]=0;
  56. }
  57. }
  58. void Zero()
  59. {int i,j;
  60. for(i=1;i<=n;i++)
  61. {
  62. for(j=1;j<=m;j++) fout<<"0 ";
  63. fout<<'\n';
  64. }
  65.  
  66. }
  67.  
  68. int main()
  69. {
  70. Citire();
  71. Back(xs,ys,1);
  72. Zero();
  73. return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment