Surrounded Regions why Runtime Error


  • 0
    M

    void travel(int i, int j, int size, int colSize, vector<vector<bool> > &mark,
    vector<vector<char>> &board)
    {
    if (i >= 0 && i < size && j >= 0 && j < colSize && board[i][j] == 'O' && mark[i][j] == false)
    {
    mark[i][j] = true;
    board[i][j] = 'A';
    travel(i + 1, j, size, colSize, mark, board);
    travel(i, j + 1, size, colSize, mark, board);
    travel(i - 1, j, size, colSize, mark, board);
    travel(i, j - 1, size, colSize, mark, board);
    }
    }

    void solve(vector<vector<char>> &board)
    {
    int size = board.size();
    if (0 == size)
    {
    return;
    }
    int colSize = board[0].size();
    vector<vector<bool> > mark(size, vector<bool>(colSize, false));
    for (int i = 0; i < size; i++)
    {
    if (board[i][0] == 'O')
    {
    board[i][0] = 'A';
    mark[i][0] = true;
    travel(i, 1, size, colSize, mark, board);
    }

    	if (board[i][colSize - 1] == 'O')
    	{
    		board[i][colSize - 1] = 'A';
    		mark[i][colSize - 1] = true;
    		travel(i, colSize - 2, size, colSize, mark, board);
    	}
    }
    
    for (int i = 1; i < colSize - 1; i++)
    {
    	if (board[0][i] == 'O')
    	{
    		board[0][i] = 'A';
    		mark[0][i] = true;
    		travel(1, i, size, colSize, mark, board);
    	}
    
    	if (board[size - 1][i] == 'O')
    	{
    		board[size - 1][i] = 'A';
    		mark[size - 1][i] = true;
    		travel(size - 2, i, size, colSize, mark, board);
    	}
    }
    
    for (int i = 0; i < size; i++)
    {
    	for (int j = 0; j < colSize; j++)
    	{
    		board[i][j] = board[i][j] == 'A' ? 'O' : 'X';
    	}
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.