fork download
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int tomato[1000][1000];
  8. int is_searched[1000][1000];
  9. queue <int> x;
  10. queue <int> y;
  11. vector <int> other;
  12. int di[4] = {1, -1, 0, 0};
  13. int dj[4] = {0, 0, 1, -1};
  14. int M, N;
  15. int maximum = 0;
  16.  
  17. void bfs(int i, int j){
  18. is_searched[i][j] = 1;
  19.  
  20. x.push(i);
  21. y.push(j);
  22.  
  23. while(! x.empty()){
  24. for(int a = 0; a < 4; a++){
  25. int ni = x.front() + di[a];
  26. int nj = y.front() + dj[a];
  27. if(ni < 0 || ni >= N || nj < 0 || nj >=M){
  28. continue;
  29. }
  30. if(tomato[ni][nj] == -1){
  31. continue;
  32. }
  33. if(is_searched[ni][nj] == 0){
  34. is_searched[ni][nj] = is_searched[x.front()][y.front()] + 1;
  35. if(is_searched[ni][nj] > maximum){
  36. maximum = is_searched[ni][nj];
  37. }
  38. x.push(ni);
  39. y.push(nj);
  40. }
  41.  
  42. }
  43. x.pop();
  44. y.pop();
  45. }
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51. cin >> M >> N;
  52.  
  53. for(int i = 0; i < N; i++){
  54. for(int j = 0; j < M; j++){
  55. cin >> tomato[i][j];
  56. }
  57. }
  58.  
  59. for(int i = 0; i < N; i++){
  60. for(int j = 0; j < M; j++){
  61. if(tomato[i][j] == 1 && is_searched[i][j] == 0){
  62. bfs(i, j);
  63. }
  64. }/*일단 숙성이 완료되었을 때의 날짜 구하기*/
  65. }
  66. cout << maximum << "\n";
  67. for(int i = 0; i < N; i++){
  68. for(int j = 0; j < M; j++){
  69. cout << is_searched[i][j];
  70. }
  71. cout << "\n";
  72. }
  73.  
  74. for(int i = 0; i < N; i++){
  75. for(int j = 0; j < M; j++){
  76. if(is_searched[i][j] == 0 && tomato[i][j] != -1){
  77. cout << -1;
  78. return 0;
  79. }
  80. }
  81. }/*토마토가 모두 익지 못하는 상황*/
  82.  
  83. cout << 1;
  84.  
  85. int count = 0;
  86. for(int i = 0; i < N; i++){
  87. for(int j = 0; j < M; j++){
  88. if(tomato[i][j] == 1 || tomato[i][j] == -1){
  89. count += 1;
  90. }
  91. }
  92. }
  93. if(count == N * M){
  94. cout << 0;
  95. return 0;
  96. }/* 토마토가 첨부터 다 익음*/
  97.  
  98. cout << 2;
  99.  
  100. if(maximum == 0){
  101. cout << 0;
  102. }else{
  103. cout << maximum - 1;
  104. }
  105.  
  106. cout << 3;
  107. return 0;/*토마토가 모두 익었을 때, 날짜를 구하기*/
  108. }
  109.  
Success #stdin #stdout 0s 23048KB
stdin
6 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 1
stdout
9
987654
876543
765432
654321
1283