Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- int main()
- {
- int L, W;
- cin >> L >> W;
- int treba;
- cin >> treba;
- int n;
- cin >> n;
- vector<pair<int, int>> v(n);
- vector<vector<bool>> visited(L, vector<bool>(W, false));
- for(int i = 0; i < n; i++) {
- cin >> v[i].first >> v[i].second;
- v[i].first--;
- v[i].second--;
- visited[v[i].first][v[i].second] = true;
- }
- queue<vector<pair<int, int>>> q;
- queue<int> q_mesec;
- treba -= n;
- q.push(v);
- q_mesec.push(0);
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- while(!q.empty()) {
- vector<pair<int, int>> c = q.front();
- q.pop();
- int mesec = q_mesec.front();
- q_mesec.pop();
- if(treba <= 0) {
- cout << mesec << endl;
- break;
- }
- vector<pair<int, int>> t;
- for(int i = 0; i < c.size(); i++) {
- for(int j = 0; j < 4; j++) {
- int ti = c[i].first + di[j];
- int tj = c[i].second + dj[j];
- if(ti >= 0 and ti < L and tj >= 0 and tj < W and !visited[ti][tj]) {
- visited[ti][tj] = true;
- treba--;
- t.push_back(make_pair(ti, tj));
- }
- }
- }
- q.push(t);
- q_mesec.push(mesec + 1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement