Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- bool visited[1005][1005];
- int main()
- {
- int a,b;
- cin>>a>>b;
- int n;
- cin>>n;
- int s;
- cin>>s;
- int x,y;
- vector<pair<int,int>>v;
- for(int i=0;i<a;i++)
- {
- for(int j=0;j<b;j++)
- {
- visited[i][j]=false;
- }
- }
- for(int i=0;i<s;i++)
- {
- cin>>x>>y;
- x--;
- y--;
- v.push_back(make_pair(x,y));
- visited[x][y]=1;
- }
- queue<vector<pair<int,int>>>Q;
- Q.push(v);
- int kolkuM=0;
- int kolkuZ=0;
- while(!Q.empty())
- {
- vector<pair<int,int>>p=Q.front();
- Q.pop();
- vector<pair<int,int>>p2;
- kolkuZ+=p.size();
- if(kolkuZ>=n)
- {
- cout<<kolkuM;
- return 0;
- }
- kolkuM++;
- for(int i=0;i<p.size();i++)
- {
- int ci=p[i].first;
- int cj=p[i].second;
- if(ci+1<a && visited[ci+1][cj]==false)
- {
- p2.push_back(make_pair(ci+1,cj));
- visited[ci+1][cj]=1;
- }
- if(ci-1>=0 && visited[ci-1][cj]==false)
- {
- p2.push_back(make_pair(ci-1,cj));
- visited[ci-1][cj]=1;
- }
- if(cj+1<b && visited[ci][cj+1]==0)
- {
- p2.push_back(make_pair(ci,cj+1));
- visited[ci][cj+1]=1;
- }
- if(cj-1>=0 && visited[ci][cj-1]==0)
- {
- p2.push_back(make_pair(ci,cj-1));
- visited[ci][cj-1]=1;
- }
- }
- Q.push(p2);
- }
- return 0;
- }
- /*
- ??XXX
- ?XXXX
- ??X??
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement