YorKnEz

Untitled

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