
typedef struct {
int x;
int y;
} position_t;
void BFS(char** board, int x, int y, int row, int col, int** visit)
{
int rear = -1;
int front = -1;
int i, j;
position_t temp;
position_t *queue = (position_t *)calloc(row * col, sizeof(position_t));
temp.x = x;
temp.y = y;
visit[x][y] = 1;
queue[++front] = temp;
while (rear != front) {
temp = queue[++rear];
i = temp.x;
j = temp.y;
if(i + 1 < row && board[i + 1][j] == 'O' && visit[i + 1][j] == 0) {
temp.x = i + 1;
temp.y = j;
visit[i + 1][j] = 1;
board[i + 1][j] = 'A';
queue[++front] = temp;
}
if(i - 1 >= 0 && board[i - 1][j] == 'O' && visit[i - 1][j] == 0) {
temp.x = i - 1;
temp.y = j;
visit[i - 1][j] = 1;
board[i - 1][j] = 'A';
queue[++front] = temp;
}
if(j + 1 < col && board[i][j + 1] == 'O' && visit[i][j + 1] == 0) {
temp.x = i;
temp.y = j + 1;
visit[i][j + 1] = 1;
board[i][j + 1] = 'A';
queue[++front] = temp;
}
if(j - 1 >= 0 && board[i][j - 1] == 'O' && visit[i][j - 1] == 0) {
temp.x = i;
temp.y = j - 1;
visit[i][j - 1] = 1;
board[i][j - 1] = 'A';
queue[++front] = temp;
}
}
free(queue);
}
void solve(char** board, int boardSize, int* boardColSize)
{
if (board == NULL || boardSize <= 0 || *boardColSize <= 0) {
return;
}
int row = boardSize;
int col = *boardColSize;
int** visited = (int**)calloc(row, sizeof(int*));
for (int i = 0; i < row; i++) {
visited[i] = (int*)calloc(col, sizeof(int));
}
for (int i = 0; i < row; i++) {
if (board[i][0] == 'O' && visited[i][0] == 0) {
board[i][0] = 'A';
BFS(board, i, 0, row, col, visited);
}
}
for (int j = 0; j < col; j++) {
if (board[0][j] == 'O' && visited[0][j] == 0) {
board[0][j] = 'A';
BFS(board, 0, j, row, col, visited);
}
}
for (int i = 0; i < row; i++) {
if (board[i][col - 1] == 'O' && visited[i][col - 1] == 0) {
board[i][col - 1] = 'A';
BFS(board, i, col - 1, row, col, visited);
}
}
for (int j = 0; j < col; j++) {
if (board[row - 1][j] == 'O' && visited[row - 1][j] == 0) {
board[row - 1][j] = 'A';
BFS(board, row - 1, j, row, col, visited);
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
if (board[i][j] == 'A') {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < row; i++) {
free(visited[i]);
}
free(visited);
}

typedef struct {
int x;
int y;
int min;
} node_t;
#define MAX_DIRECTION 4
node_t g_direction[MAX_DIRECTION] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int bfs(int** nums, int row, int col)
{
node_t queue[3000] = {0};
int num = 0;
int front = 0;
int rear = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if(nums[i][j] == 2) {
node_t tmp;
tmp.x = i;
tmp.y = j;
tmp.min = 0;
queue[front++] = tmp;
}
}
}
while (rear < front) {
node_t node = queue[rear++];
for (int i = 0; i < MAX_DIRECTION; i++) {
node_t tmp;
tmp.x = node.x + g_direction[i].x;
tmp.y = node.y + g_direction[i].y;
if (tmp.x < 0 || tmp.x >= row || tmp.y < 0 || tmp.y >= col) {
continue;
}
if (nums[tmp.x][tmp.y] == 1) {
nums[tmp.x][tmp.y] = 2;
tmp.min = node.min + 1;
num = tmp.min > num ? tmp.min : num;
queue[front++] = tmp;
}
}
}
return num;
}
int orangesRotting(int** grid, int gridSize, int* gridColSize)
{
if (grid == NULL || gridSize <= 0 || *gridColSize <= 0) {
return -1;
}
int ret = bfs(grid, gridSize, *gridColSize);
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < *gridColSize; j++) {
if (grid[i][j] == 1) {
return -1;
}
}
}
return ret;
}