Created
June 21, 2018 00:58
-
-
Save ChaiBapchya/1d37417de3dfc4df90311bfec1769a6a to your computer and use it in GitHub Desktop.
surrounding island
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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