Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define obs -1
- using namespace std;
- int n,m,a[180][180];
- int startx,starty,stopx,stopy;
- queue <pair <int, int> > tail;
- const int di[]={-1,0,1,0};
- const int dj[]={0,1,0,-1};
- ifstream fin("alee.in");
- ofstream fout("alee.out");
- void Read()
- {
- int x,y;
- fin >> n >> m;
- for (int i=0; i<m; i++)
- {
- fin >> x >> y;
- a[x][y]=obs;
- }
- fin >> startx >> starty >> stopx >> stopy;
- a[startx][starty]=1;
- return;
- }
- inline bool isOk(int i,int j)
- {
- if (i<1 || j<1 || j>n || i>n || a[i][j]==obs)
- return false;
- return true;
- }
- void Lee(){
- int i,j,i_next,j_next;
- tail.push(make_pair(startx,starty));
- while (!tail.empty())
- {
- i=tail.front().first;
- j=tail.front().second;
- tail.pop();
- for (int dir=0;dir<4;dir++)
- {
- i_next=i+di[dir];
- j_next=j+dj[dir];
- if (isOk(i_next,j_next) && a[i_next][j_next]<1)
- {
- a[i_next][j_next]=a[i][j]+1;
- tail.push(make_pair(i_next,j_next));
- }
- }
- }
- return;
- }
- int main()
- {
- Read();
- Lee();
- fout<<a[stopx][stopy];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement