int orangesRotting(int** grid, int gridSize, int* gridColSize){
int count=0;
int end=1;
while(1){
end=1;
for(int a=0;a<gridSize;a++){
for(int b=0;b<gridColSize[0];b++){
if(grid[a][b]==1){
if(a-1>=0&&grid[a-1][b]==2){
grid[a][b]=3;
end=0;
}
if(a+1<gridSize&&grid[a+1][b]==2){
grid[a][b]=3;
end=0;
}
if(b-1>=0&&grid[a][b-1]==2){
grid[a][b]=3;
end=0;
}
if(b+1<gridColSize[0]&&grid[a][b+1]==2){
grid[a][b]=3;
end=0;
}
}
if(grid[a][b]==2){
if(a-1>=0&&grid[a-1][b]==1){
grid[a-1][b]=3;
end=0;
}
if(a+1<gridSize&&grid[a+1][b]==1){
grid[a+1][b]=3;
end=0;
}
if(b-1>=0&&grid[a][b-1]==1){
grid[a][b-1]=3;
end=0;
}
if(b+1<gridColSize[0]&&grid[a][b+1]==1){
grid[a][b+1]=3;
end=0;
}
}
}
}
for(int a=0;a<gridSize;a++){
for(int b=0;b<gridColSize[0];b++){
if(grid[a][b]==3){
grid[a][b]=2;
}
}
}
if(end==1){
break;
}
else{
count++;
}
}
for(int a=0;a<gridSize;a++){
for(int b=0;b<gridColSize[0];b++){
if(grid[a][b]==1){
return -1;
}
}
}
return count;
}
|