fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main() {
  5. int n, m;
  6. std::cin >> n >> m;
  7.  
  8. std::vector<std::vector<int>> matrix(n, std::vector<int>(n));
  9. int deadCells = 0;
  10.  
  11. for (int i = 0; i < n; i++) {
  12. for (int j = 0; j < n; j++) {
  13. std::cin >> matrix[i][j];
  14. }
  15. }
  16.  
  17. for (int t = 0; t < m; t++) {
  18. std::vector<std::vector<int>> tempMatrix = matrix;
  19.  
  20. for (int i = 0; i < n; i++) {
  21. for (int j = 0; j < n; j++) {
  22. if (matrix[i][j] > 0) {
  23. matrix[i][j]--;
  24. } else if (matrix[i][j] == 0) {
  25. if (i > 0 && matrix[i - 1][j] > 0) {
  26. tempMatrix[i - 1][j] = 0;
  27. deadCells++;
  28. }
  29. if (i < n - 1 && matrix[i + 1][j] > 0) {
  30. tempMatrix[i + 1][j] = 0;
  31. deadCells++;
  32. }
  33. if (j > 0 && matrix[i][j - 1] > 0) {
  34. tempMatrix[i][j - 1] = 0;
  35. deadCells++;
  36. }
  37. if (j < n - 1 && matrix[i][j + 1] > 0) {
  38. tempMatrix[i][j + 1] = 0;
  39. deadCells++;
  40. }
  41. }
  42. }
  43. }
  44.  
  45. matrix = tempMatrix;
  46. }
  47.  
  48. std::cout << deadCells << std::endl;
  49.  
  50. return 0;
  51. }
  52.  
  53.  
Success #stdin #stdout 0.01s 5304KB
stdin
2 2
1 1
1 1
stdout
0