Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int a[100][100], dl[]={-1, 1, 0 ,0}, dc[]={0, 0, 1, -1}, n, x1, y1, x2, y2, p, u;
- struct
- {
- int lin, col;
- }c[100];
- bool ok(int x, int y)
- {
- if(a[x][y]!=0 || x>n || y>n || x<1 || y<1)
- return false;
- return true;
- }
- void citire()
- {
- ifstream f("alee.in");
- f>>n;
- f>>x1>>y1>>x2>>y2;
- int i, j;
- while(f>>i>>j)
- a[i][j]=-1;
- }
- void lee()
- {
- int i, j, i_urm, j_urm, dir;
- p=u=1;
- c[1].lin=x1;
- c[1].col=y1;
- a[x1][y1]=1;
- while(p<=u)
- {
- i=c[p].lin;
- j=c[p].col;
- for(dir=0;dir<4;dir++)
- {
- i_urm=i+dl[dir];
- j_urm=j+dc[dir];
- if(ok(i_urm, j_urm)==true)
- {
- u++;
- c[u].lin=i_urm;
- c[u].col=j_urm;
- a[i_urm][j_urm]=a[i][j]+1;
- }
- }
- p++;
- }
- }
- int main()
- {
- citire();
- lee();
- cout<<a[x2][y2];
- }
- /*
- 8
- 2 1 8 8
- 2 3
- 3 5
- 5 7
- 8 7
- 6 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement