Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int L , W;
- bool valid(int a, int b)
- {
- if(a>=0 && b>=0 && a < L && b < W )
- return 1;
- return 0;
- }
- int main()
- {
- cin >> L >> W;
- int N;
- cin >> N;
- int B;
- cin >> B;
- queue <pair <int,int> >verts;
- int graph[L+1][W+1];
- graph[L][W]=999999;
- for(int i=0;i<B;i++)
- {
- int x,y;
- cin >> x >> y;
- verts.push(make_pair(x-1,y-1));
- graph[x-1][y-1]=0;
- } int newB=B;
- while(!verts.empty())
- {
- pair <int,int> vert=verts.front();
- verts.pop();
- if(valid(vert.first+1,vert.second)&&graph[vert.first+1][vert.second]>graph[vert.first][vert.second]+1)
- {
- graph[vert.first+1][vert.second]=graph[vert.first][vert.second]+1;
- verts.push(make_pair(vert.first+1,vert.second));
- newB++;
- }
- if(valid(vert.first-1,vert.second)&&graph[vert.first-1][vert.second]>graph[vert.first][vert.second]+1)
- {
- graph[vert.first-1][vert.second]=graph[vert.first][vert.second]+1;
- verts.push(make_pair(vert.first-1,vert.second));
- newB++;
- }
- if(valid(vert.first,vert.second+1)&&graph[vert.first][vert.second+1]>graph[vert.first][vert.second]+1)
- {
- graph[vert.first][vert.second+1]=graph[vert.first][vert.second]+1;
- verts.push(make_pair(vert.first,vert.second+1));
- newB++;
- }
- if(valid(vert.first,vert.second-1)&&graph[vert.first][vert.second-1]>graph[vert.first][vert.second]+1)
- {
- graph[vert.first][vert.second-1]=graph[vert.first][vert.second]+1;
- verts.push(make_pair(vert.first,vert.second-1));
- newB++;
- }
- if(newB>=N)
- {
- cout << graph[vert.first][vert.second]+1;
- return 0;
- }
- }
- //for(int i=0;i<L;i++)
- //{
- // for(int j=0;j<W;j++)
- // {cout << graph[i][j] << " ";
- // }
- // cout << endl;
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement