Advertisement
daskalot

Untitled

Mar 18th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define INF (1 << 27)
  7. #define EPS 0.00000000001
  8. using namespace std;
  9. int mNodes = 5000;
  10. bool** graph;
  11. int N, M;
  12. int needed, built;
  13. int result = 0;
  14. queue < pair<int,int> > marked;
  15. void read(){
  16. cin >> N >> M;
  17. graph = (bool**)malloc(sizeof(bool*)*N);
  18. for(int i=0;i<N;i++) graph[i] = (bool*)malloc(sizeof(bool)*M);
  19. for(int i=0;i<N;i++) for(int j=0;j<M;j++) graph[i][j] = false;
  20. cin >> needed >> built;
  21. for(int i=0;i<built;i++){
  22. pair <int,int> tmp;
  23. cin >> tmp.f >> tmp.s;
  24. tmp.f--, tmp.s--;
  25. marked.push(tmp);
  26. graph[tmp.f][tmp.s] = true;
  27. }
  28. }
  29. void printGraph(){
  30. for(int i=0;i<N;i++,cout<<endl)
  31. for(int j=0;j<M;j++)
  32. if(graph[i][j]) cout<<"x ";
  33. else cout<<"? ";
  34. }
  35. bool checkBounds(int x,int y){
  36. return (x >= 0 && x < N && y >= 0 && y < M);
  37. }
  38. void visitNeighbours(pair <int,int> tmp){
  39. int x = tmp.f, y = tmp.s;
  40. if(checkBounds(x,y+1) && !graph[x][y+1]){ built++; graph[x][y+1] = true; marked.push(mp(x,y+1)); }
  41. if(checkBounds(x+1,y) && !graph[x+1][y]){ built++; graph[x+1][y] = true; marked.push(mp(x+1,y)); }
  42. if(checkBounds(x,y-1) && !graph[x][y-1]){ built++; graph[x][y-1] = true; marked.push(mp(x,y-1)); }
  43. if(checkBounds(x-1,y) && !graph[x-1][y]){ built++; graph[x-1][y] = true; marked.push(mp(x-1,y)); }
  44. }
  45. void solve(){
  46. while(built<=needed){
  47. int qSize = marked.size();
  48. while(qSize > 0 ){
  49. visitNeighbours(marked.front());
  50. marked.pop();
  51. qSize--;
  52. }
  53. result++;
  54. }
  55. cout << result;
  56. }
  57.  
  58. int main(){
  59. read();
  60. solve();
  61. //printGraph();
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement