Get Runtime Error, but works fine with the example


  • 0
    F

    My thought is dfs visit cells with 'O', if rearched edge, return false, not flipper, else flipper

    void solve(vector<vector<char>> &board) {
    	if (board.empty() || board[0].size() == 1 || board.size() == 1){
    		return;
    	}
    	
    	int m = board.size();
    	int n = board[0].size();
    	vector<vector<bool>> visited(m, vector<bool>(n, false));
    	vector<int> path;
    	for (int i = 1; i < m-1; i++){
    		for (int j = 1; j < n-1; j++){
    			path.clear();
    			if (board[i][j] == 'O' && !visited[i][j] && checkCell(board, visited,path, i, j, m, n)){
    				//fliper
    				for (int k = 0; k < path.size(); k++){
    					int x = path[k] / m;
    					int y = path[k] % m;
    					board[x][y] = 'X';
    				}
    			}
    		}
    	}
    	
    }
    
    bool checkCell(vector<vector<char>> &board, vector<vector<bool>> &visited, vector<int>& path, int x, int y, int m, int n){
    	if (visited[x][y]){
    		return true;
    	}
    
    	if (board[x][y] == 'X'){
    		return true;
    	}
    	//edge
    	if (x == 0 || x == m-1 || y == 0 || y == n-1){
    		return false;
    	}
    
    	visited[x][y] = true;
    	path.push_back(x*m + y);
    	//check other
    	return checkCell(board, visited,path, x - 1, y, m, n) && checkCell(board, visited,path, x + 1, y, m, n) 
    		&& checkCell(board, visited,path, x, y-1, m, n) && checkCell(board, visited,path, x, y+1, m, n);
    }

Log in to reply
 

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