Advertisement
Offuru

Tsunami

Oct 15th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("tsunami.in");
  7. ofstream fout("tsunami.out");
  8.  
  9. const int NMAX=1005;
  10.  
  11. int a[NMAX][NMAX],n,m,h,nr=0;
  12. queue< pair< int, int> >q;
  13.  
  14. void bordare()
  15. {
  16. for(int i=0;i<=n+1;i++)
  17. a[i][0]=a[i][m+1]=11;
  18. for(int j=0;j<=m+1;j++)
  19. a[0][j]=a[n+1][j]=11;
  20. }
  21.  
  22. void citire()
  23. {
  24. fin>>n>>m>>h;
  25.  
  26. for(int i=1;i<=n;i++)
  27. for(int j=1;j<=m;j++)
  28. {
  29. fin>>a[i][j];
  30. if(a[i][j]==0)
  31. q.push({i,j});
  32. }
  33. }
  34.  
  35. void afisare()
  36. {
  37. for(int i=1;i<=n;i++)
  38. {
  39. for(int j=1;j<=m;j++)
  40. fout<<a[i][j]<<" ";
  41. fout<<endl;
  42. }
  43. }
  44.  
  45. int main()
  46. {
  47. bordare();
  48. citire();
  49.  
  50. int di[4]={0,1,0,-1};
  51. int dj[4]={1,0,-1,0};
  52.  
  53. while(!q.empty())
  54. {
  55.  
  56. int i=q.front().first;
  57. int j=q.front().second;
  58.  
  59. for(int d=0;d<4;d++)
  60. {
  61. if(a[i+di[d]][j+dj[d]]<h && a[i+di[d]][j+dj[d]]!=0)
  62. {
  63. nr++;
  64. q.push({i+di[d],j+dj[d]});
  65. a[i+di[d]][j+dj[d]]=0;
  66. }
  67. }
  68. q.pop();
  69. }
  70.  
  71. afisare();
  72.  
  73. fout<<endl<<nr;
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement