Skip to content

Instantly share code, notes, and snippets.

@ChaiBapchya
Created June 21, 2018 00:58
Show Gist options
  • Save ChaiBapchya/1d37417de3dfc4df90311bfec1769a6a to your computer and use it in GitHub Desktop.
Save ChaiBapchya/1d37417de3dfc4df90311bfec1769a6a to your computer and use it in GitHub Desktop.
surrounding island
class Solution {
int n,m;
public void solve(char[][] board) {
n= board.length;
if(n==0)return;
m=board[0].length;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(board[i][j]=='X')
dfsmark(board,i,j);
}
}
}
public void dfsmark(char[][] board,int i,int j){
if(i<0||j<0||i>=n||j>=m)return;
if(board[i][j]=='O'){
if(i!=0&&j!=0&&i!=n&&j!=m)
board[i][j]='X';
else
return;
}
if(board[i][j]=='X')
{
if(i+1<n)
dfsmark(board,i+1,j);
if(i-1>0)
dfsmark(board,i-1,j);
if(j+1<m)
dfsmark(board,i,j+1);
if(j-1>0)
dfsmark(board,i,j-1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment