Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct pct
- {
- int x,y,pas;
- } c[1001];
- int dl[]= {-1,0,1,0};
- int dc[]= {0,1,0,-1};
- ifstream fin("roboti.in");
- ofstream fout("roboti.out");
- void bordare(int n, int m,int A[][1001])
- {
- for(int i=0;i<=n+1;i++)
- {
- A[0][i]=-1;
- A[n+1][i]=-1;
- }
- for(int i=0;i<=m+1;i++)
- {
- A[i][0]=-1;
- A[i][m+1]=-1;
- }
- }
- void lee(int x0, int y0,int A[][1001],int xf, int yf, int&ok )
- {
- int u,p;
- p=u=1;
- c[p].x=x0;
- c[p].y=y0;
- c[p].pas=1;
- A[x0][y0]=1;
- while(p<=u)
- {
- pct nc=c[p];
- for(int k=0;k<4;k++)
- {
- if(A[nc.x+dl[k]][nc.y+dc[k]]==0)
- {
- A[nc.x+dl[k]][nc.y+dc[k]]=nc.pas+1;
- u++;
- c[u].x=nc.x+dl[k];
- c[u].y=nc.y+dc[k];
- c[u].pas=nc.pas+1;
- if(c[u].x==xf && c[u].y==yf)
- {
- fout<<c[u].pas-1;
- ok=1;
- p=u+1;
- }
- }
- }
- p++;
- }
- }
- int n,m,i,j,x0,y0,A[1001][1001],xf,yf,k,ok;
- int main()
- {
- fin>>n>>m;
- for(i=1; i<=n; i++)
- for(j=1; j<=m; j++)
- {
- fin>>A[i][j];
- if(A[i][j]==1)
- A[i][j]=-1;
- }
- fin>>x0>>y0;
- fin>>xf>>yf;
- ok=0;
- bordare(n,m,A);
- lee(x0,y0,A,xf,yf,ok);
- if(ok==0)
- fout<<-1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment