Run time error on empty input?


  • 0
    X

    Hi, I got a run time error on a empty input[ ]. my code works fine with my own test codes. Any idea of what is happening?

    And I use empty char[ ][ ] to test on local machine, it works fine.

    My algorithm is to start from 'o's on the edges of the board, then use a iterative DFS to find 'o' clique on the board. Then mark 'x' to other 'o's that are not connected to the edge 'o's.

    public static void solve(char[][] board) {
    	if(board==null){
    		return;
    	}
    	
    	if(board[0]==null){
    		return;
    	}
    	
    	boolean[][] visited = new boolean[board.length][board.length];
    
    	for (int i = 0; i < board.length; i++) {
    		if (board[i][0] == 'o' && visited[i][0] == false) {
    			visited[i][0] = true;
    			findClique(i, 0, board, visited);
    		}
    		if (board[i][board[0].length - 1] == 'o'
    				&& visited[i][board[0].length - 1] == false) {
    			visited[i][board[0].length - 1] = true;
    			findClique(i, board[0].length - 1, board, visited);
    		}
    
    	}
    
    	for (int i = 0; i < board[0].length; i++) {
    		if (board[0][i] == 'o'&&visited[0][i] ==false) {
    			visited[0][i] = true;
    			findClique(0, i, board, visited);
    		}
    		if (board[board.length - 1][i] == 'o'&&visited[board.length - 1][i] == false) {
    			visited[board.length - 1][i] = true;
    			findClique(board.length - 1, i, board, visited);
    		}
    		
    	}
    	
    	
    	for (int i = 0; i < board.length; i++) {
    		for (int m = 0; m < board[0].length; m++) {
    			if(board[i][m]=='o'&& visited[i][m]==false){
    				board[i][m] = 'x'; 
    			}
    		}
    	}
    
    	return;
    }//
    
    private static void findClique(int x, int y, char[][] board,
    		boolean[][] visited) {
    
    	visited[x][y] = false;
    
    	Stack<Integer> stx = new Stack<Integer>();
    	Stack<Integer> sty = new Stack<Integer>();
    	stx.push(x);
    	sty.push(y);
    
    	int xtmp = 0;
    	int ytmp = 0;
    	while (!stx.isEmpty()) {
    		xtmp = stx.pop();
    		ytmp = sty.pop();
    		visited[xtmp][ytmp] = true;
    
    		if (xtmp > 0) {
    			if (board[xtmp - 1][ytmp] == 'o'
    					&& visited[xtmp - 1][ytmp] == false) {
    				stx.push(xtmp - 1);
    				sty.push(ytmp);
    			}
    		}
    
    		if (xtmp < board.length - 1) {
    			if (board[xtmp + 1][ytmp] == 'o'
    					&& visited[xtmp + 1][ytmp] == false) {
    				stx.push(xtmp + 1);
    				sty.push(ytmp);
    			}
    		}
    
    		if (ytmp > 0) {
    			if (board[xtmp][ytmp - 1] == 'o'
    					&& visited[xtmp][ytmp - 1] == false) {
    				stx.push(xtmp);
    				sty.push(ytmp - 1);
    			}
    		}
    
    		if (ytmp < board[0].length - 1) {
    			if (board[xtmp][ytmp + 1] == 'o'
    					&& visited[xtmp][ytmp + 1] == false) {
    				stx.push(xtmp);
    				sty.push(ytmp + 1);
    			}
    		}
    
    	}
    
    }

  • 0
    N
    This post is deleted!

Log in to reply
 

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