Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int L , W;
  5.  
  6. bool valid(int a, int b)
  7. {
  8. if(a>=0 && b>=0 && a < L && b < W )
  9. return 1;
  10. return 0;
  11. }
  12.  
  13. int main()
  14. {
  15.  
  16. cin >> L >> W;
  17. int N;
  18. cin >> N;
  19. int B;
  20. cin >> B;
  21. queue <pair <int,int> >verts;
  22. int graph[L+1][W+1];
  23. graph[L][W]=999999;
  24. for(int i=0;i<B;i++)
  25. {
  26. int x,y;
  27. cin >> x >> y;
  28. verts.push(make_pair(x-1,y-1));
  29. graph[x-1][y-1]=0;
  30. } int newB=B;
  31.  
  32. while(!verts.empty())
  33. {
  34.  
  35. pair <int,int> vert=verts.front();
  36. verts.pop();
  37. if(valid(vert.first+1,vert.second)&&graph[vert.first+1][vert.second]>graph[vert.first][vert.second]+1)
  38. {
  39. graph[vert.first+1][vert.second]=graph[vert.first][vert.second]+1;
  40.  
  41. verts.push(make_pair(vert.first+1,vert.second));
  42. newB++;
  43.  
  44.  
  45. }
  46. if(valid(vert.first-1,vert.second)&&graph[vert.first-1][vert.second]>graph[vert.first][vert.second]+1)
  47. {
  48. graph[vert.first-1][vert.second]=graph[vert.first][vert.second]+1;
  49.  
  50. verts.push(make_pair(vert.first-1,vert.second));
  51. newB++;
  52.  
  53. }
  54. if(valid(vert.first,vert.second+1)&&graph[vert.first][vert.second+1]>graph[vert.first][vert.second]+1)
  55. {
  56. graph[vert.first][vert.second+1]=graph[vert.first][vert.second]+1;
  57.  
  58. verts.push(make_pair(vert.first,vert.second+1));
  59. newB++;
  60.  
  61. }
  62. if(valid(vert.first,vert.second-1)&&graph[vert.first][vert.second-1]>graph[vert.first][vert.second]+1)
  63. {
  64. graph[vert.first][vert.second-1]=graph[vert.first][vert.second]+1;
  65. verts.push(make_pair(vert.first,vert.second-1));
  66. newB++;
  67.  
  68. }
  69. if(newB>=N)
  70. {
  71. cout << graph[vert.first][vert.second]+1;
  72. return 0;
  73. }
  74.  
  75.  
  76. }
  77.  
  78. //for(int i=0;i<L;i++)
  79. //{
  80. // for(int j=0;j<W;j++)
  81. // {cout << graph[i][j] << " ";
  82. // }
  83. // cout << endl;
  84. //}
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement