nicuvlad76

Untitled

Feb 18th, 2023
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include<fstream>
  2. using namespace std;
  3. short n,m,xi,yi,xf,yf,b[177][177];
  4. short dx[]={-1,0,1,0};
  5. short dy[]={0,1,0,-1};
  6. short q[30625][2];
  7. struct alee{
  8.         short x,y;
  9. };
  10. alee a[15000];
  11.  
  12. void citire()
  13. {
  14.         ifstream fin("alee.in");
  15.         short i,j,k;
  16.         fin>>n>>m;
  17.         for(k=1;k<=m;k++){
  18.                 fin>>a[k].x>>a[k].y;
  19.                 b[a[k].x][a[k].y]=-1;
  20.         }
  21.         fin>>xi>>yi>>xf>>yf;
  22.         for(i=1;i<=n;i++)
  23.         {
  24.                 for(j=1;j<=n;j++)
  25.                         if(b[i][j]!=-1)
  26.                                 b[i][j]=31000;
  27.         }
  28.         b[xi][yi]=0;
  29. }
  30. void afisare()
  31. {
  32.         ofstream fout("alee.out");
  33.         fout<<b[xf][yf]+1;
  34. }
  35. void lee(short c[177][177])
  36. {
  37.  int i,x,y,xx,yy,st=0,dr=0,ok=0;
  38.  q[0][0]=xi;
  39.  q[0][1]=yi;
  40.  while(st<=dr&&ok==0)
  41.  {
  42.          x=q[st][0];
  43.          y=q[st][1];
  44.          for(i=0;i<4;i++)
  45.          {
  46.                  xx=x+dx[i];
  47.                  yy=y+dy[i];
  48.                  if(c[xx][yy]==31000)
  49.                  {
  50.                          c[xx][yy]=c[x][y]+1;
  51.                          dr++;
  52.                          q[dr][0]=xx;
  53.                          q[dr][1]=yy;
  54.                  }
  55.                  if(xx==xf&&yy==yf)
  56.                          ok=1;
  57.          }
  58.          st++;
  59.   }
  60. }
  61. int main()
  62. {
  63.  
  64.         citire();
  65.         lee(b);
  66.         afisare();
  67.         return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment