Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- typedef struct Pct {
- int x;
- int y;
- int pas;
- } Pct;
- Pct c[1000000], pi, pf;
- int n,m,i,j,x0,y0,A[1002][1002],xf,yf,k,ok;
- int dl[]= {-1,0,1,0},
- dc[]= {0,1,0,-1};
- ifstream fin("roboti.in");
- ofstream fout("roboti.out");
- void bordare (int n, int m,int A[1002][1002])
- {
- 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 xi, int yi,int A[1002][1002],int xf, int yf, int &ok )
- {
- int u = 0,p = 0;
- bool exit = false;
- c[p].x=xi;
- c[p].y=yi;
- c[p].pas=1;
- A[xi][yi]=1;
- while(p<=u && !exit)
- {
- for(int k=0; k<4; k++)
- {
- if(A[c[p].x+dl[k]][c[p].y+dc[k]]==0)
- {
- u++;
- c[u].x=c[p].x+dl[k];
- c[u].y=c[p].y+dc[k];
- c[u].pas=c[p].pas+1;
- A[c[u].x][c[u].y]=c[u].pas;
- if(c[u].x==xf && c[u].y==yf)
- {
- fout<<c[u].pas-1;
- ok=1;
- exit = true;
- break;
- }
- }
- }
- p++;
- }
- }
- 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