HELP!! works on my computer, but not on OJ (not due to 'X' or 'O' spelling)


  • 0
    X

    Worked 100% locally, but on OJ seems like the 2nd row in my answer always messes up.......any help?

    vector<vector<int>> neighborOs(vector<vector<char>> &board, int row, int col, int **seen){
    	int coords[4][2] = {{row,col-1},{row,col+1},{row-1,col},{row+1,col}};
    	vector<vector<int>> neighbors;
    	vector<int> pair;
    	
    	if(seen[row][col]==0) seen[row][col]=1;
    	for(int i=0; i<4; i++){
    		if(coords[i][0]<0 || coords[i][0]>=board.size() || coords[i][1]<0 || coords[i][1]>=board[0].size()) continue; //if outside the board
    		if(seen[coords[i][0]][coords[i][1]] != 0) continue; // if already seen
    		if(board[coords[i][0]][coords[i][1]] == 'O'){
    			seen[coords[i][0]][coords[i][1]] = 1;
    			pair = {coords[i][0], coords[i][1]};
    			neighbors.push_back(pair);
    		}
    	}
    	return neighbors;
    }
    
    
    void solve(vector<vector<char>> &board) {
    	//initialize "seen" 2d array
          int *seen[board.size()];
          for(int i=0; i<board.size(); i++){
        	  seen[i] = new int[board[0].size()];
          }
    
          
          vector<vector<int>> region, neighbors;
          vector<int> pair;
          bool surrounded;
          int idx_seen = 0, idx = 0, prevsize;
          
          
          //looks at each point on the board
          for(int row=0; row<board.size(); row++){
        	  for(int col=0; col<board[0].size(); col++){
        		  
        		  //skip if X found
        		  if(board[row][col]=='X' || seen[row][col]!=0) continue;
        		  
        		  //clear previous data
        		  region.clear();
        		  neighbors.clear();
        		  idx_seen = 0;
        		  idx = 0;
        		  prevsize = -1;
        		  surrounded = true;
        		
        		  //insert row-col point into queue if not already seen
        		  if(seen[row][col]==0){
        			  pair = {row, col};
        			  region.push_back(pair);
        			  seen[row][col]==1;
        		  }
        		  
        		  //get all points in connected region, mark them as seen
        		  while(region.size() != prevsize){
        			  prevsize = region.size();   	
            		  
            		  //set everything in region so far to "seen"
        			  for( ; idx_seen<prevsize; idx_seen++){
        				  seen[region[idx_seen][0]][region[idx_seen][1]] = 1;
        			  }
        			  
        			  //add new neighbors to region
        			  for( ; idx<prevsize; idx++){
        				  neighbors = neighborOs(board, region[idx][0], region[idx][1], seen);
        				  for(int i=0; i<neighbors.size(); i++){
        					  region.push_back(neighbors[i]);
    					  }
        			  }
        		  }//while
        		  
        		  for(int i=0; i<region.size(); i++){
        			  if(region[i][0]==0 || region[i][0]==board.size()-1 || region[i][1]==0 || region[i][1]==board[0].size()-1){
        				  surrounded = false;
        				  break;
        			  }
        		  }//for
        		 
        		  if(surrounded){
        			  for(int i=0; i<region.size(); i++){
        				  board[region[i][0]][region[i][1]] = 'X';
        			  }
        		  }//if    	
        	  
        	  }//for col   	  
          }//for row
    
    }

Log in to reply
 

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