-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRottenTomatoes.cpp
More file actions
66 lines (57 loc) · 1.29 KB
/
Copy pathRottenTomatoes.cpp
File metadata and controls
66 lines (57 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
#define R 100
#define C 100
using namespace std;
int getRotTime(int mat[R][C], int r, int c){
int timer = 0;
for(int X=r+c+1; X>=0; X--){
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(mat[i][j] == 2){
if(i>0 && mat[i-1][j] == 1){
mat[i-1][j] = 3;
}
if(j>0 && mat[i][j-1] == 1){
mat[i][j-1] = 3;
}
if(i<r-1 && mat[i+1][j] == 1){
mat[i+1][j] = 3;
}if(j<c-1 && mat[i][j+1] == 1){
mat[i][j+1] = 3;
}
}
}
}
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(mat[i][j] == 3)
mat[i][j] = 2;
}
}
timer++;
}
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(mat[i][j] == 1)
timer = -1;
break;
}
}
return timer;
}
int main(){
int T, r, c;
cin>>T;
while(T){
cin>>r>>c;
int mat[R][C];
for(int i=0; i<r; i++){
for(int j=0;j<c; j++){
cin>>mat[i][j];
}
}
cout<<getRotTime(mat, r, c)<<endl;
T--;
}
return 0;
}