YorKnEz

Untitled

Feb 7th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct pct
  5. {
  6. int x,y,pas;
  7. } c[1001];
  8. int dl[]= {-1,0,1,0};
  9. int dc[]= {0,1,0,-1};
  10. ifstream fin("roboti.in");
  11. ofstream fout("roboti.out");
  12. void bordare(int n, int m,int A[][1001])
  13. {
  14. for(int i=0;i<=n+1;i++)
  15. {
  16. A[0][i]=-1;
  17. A[n+1][i]=-1;
  18. }
  19. for(int i=0;i<=m+1;i++)
  20. {
  21. A[i][0]=-1;
  22. A[i][m+1]=-1;
  23. }
  24. }
  25. void lee(int x0, int y0,int A[][1001],int xf, int yf, int&ok )
  26. {
  27. int u,p;
  28. p=u=1;
  29. c[p].x=x0;
  30. c[p].y=y0;
  31. c[p].pas=1;
  32. A[x0][y0]=1;
  33. while(p<=u)
  34. {
  35. pct nc=c[p];
  36. for(int k=0;k<4;k++)
  37. {
  38. if(A[nc.x+dl[k]][nc.y+dc[k]]==0)
  39. {
  40. A[nc.x+dl[k]][nc.y+dc[k]]=nc.pas+1;
  41. u++;
  42. c[u].x=nc.x+dl[k];
  43. c[u].y=nc.y+dc[k];
  44. c[u].pas=nc.pas+1;
  45. if(c[u].x==xf && c[u].y==yf)
  46. {
  47. fout<<c[u].pas-1;
  48. ok=1;
  49. p=u+1;
  50. }
  51. }
  52.  
  53. }
  54. p++;
  55. }
  56.  
  57. }
  58. int n,m,i,j,x0,y0,A[1001][1001],xf,yf,k,ok;
  59. int main()
  60. {
  61. fin>>n>>m;
  62. for(i=1; i<=n; i++)
  63. for(j=1; j<=m; j++)
  64. {
  65. fin>>A[i][j];
  66. if(A[i][j]==1)
  67. A[i][j]=-1;
  68. }
  69. fin>>x0>>y0;
  70. fin>>xf>>yf;
  71. ok=0;
  72. bordare(n,m,A);
  73. lee(x0,y0,A,xf,yf,ok);
  74. if(ok==0)
  75. fout<<-1;
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment