fork(1) download
  1. // boj 7569
  2.  
  3. #include <iostream>
  4. #include <queue>
  5. #include <tuple>
  6.  
  7. using namespace std;
  8.  
  9. int tomato[101][101][101];
  10. int dx[6] = {0, 0, 1, -1, 0, 0};
  11. int dy[6] = {1, -1, 0, 0, 0, 0};
  12. int dz[6] = {0, 0, 0, 0, 1, -1};
  13.  
  14. int main()
  15. {
  16. int m, n, h;
  17.  
  18. cin >> m >> n >> h;
  19. queue<tuple<int, int, int>> q;
  20. int max = 0;
  21.  
  22. for (int i = 0; i < h; i++)
  23. {
  24. for (int j = 0; j < n; j++)
  25. {
  26. for (int k = 0; k < m; k++)
  27. {
  28. cin >> tomato[i][j][k];
  29. if (tomato[i][j][k])
  30. {
  31. q.push({i, j, k});
  32. }
  33. }
  34. }
  35. }
  36.  
  37. while (!q.empty())
  38. {
  39. tuple<int, int, int> t = q.front();
  40. q.pop();
  41. int z = get<0>(t);
  42. int x = get<1>(t); // 가로 !!! x가 가로!!
  43. int y = get<2>(t); // 세로 !!! y가 세로!!
  44.  
  45. for (int dir = 0; dir < 6; dir++)
  46. {
  47. int nx = x + dx[dir];
  48. int ny = y + dy[dir];
  49. int nz = z + dz[dir];
  50.  
  51. if (nx < 0 || nx >= n || ny < 0 || ny >= m || nz < 0 || nz >= h)
  52. {
  53. continue;
  54. }
  55. if (tomato[nz][nx][ny] != 0)
  56. {
  57. continue;
  58. }
  59. tomato[nz][nx][ny] = tomato[z][x][y] + 1;
  60. q.push({nz, nx, ny});
  61. }
  62. }
  63.  
  64.  
  65.  
  66. for (int i = 0; i < h; i++)
  67. {
  68. for (int j = 0; j < n; j++)
  69. {
  70. for (int k = 0; k < m; k++)
  71. {
  72. if (tomato[i][j][k] == 0)
  73. {
  74. cout << -1;
  75. return 0;
  76. }
  77. if (max < tomato[i][j][k])
  78. {
  79. max = tomato[i][j][k];
  80. }
  81. }
  82. }
  83. }
  84.  
  85. cout << max - 1;
  86. }
Success #stdin #stdout 0s 5300KB
stdin
5 3 1
0 -1 0 0 0
0 -1 0 1 1
0 0 0 1 1
stdout
2