Skip to content

Instantly share code, notes, and snippets.

@z0marlin
Created October 28, 2018 10:59
Show Gist options
  • Save z0marlin/3a6c7bd5c7190438a8b7a4f8b5012173 to your computer and use it in GitHub Desktop.
Save z0marlin/3a6c7bd5c7190438a8b7a4f8b5012173 to your computer and use it in GitHub Desktop.
WEC Codebuddy week4 sem-3 solution.
int Solution::black(vector<string> &A) {
int n=A.size(),m=A[0].size(),count=0,x,y;
queue<pair<int,int>> q;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(A[i][j]=='X'){
count++;
q.push(make_pair(i,j));
while(!q.empty()){
x=q.front().first;
y=q.front().second;
q.pop();
A[x][y]='0';
if(x+1<n && A[x+1][y]=='X')
q.push(make_pair(x+1,y));
if(x-1>=0 && A[x-1][y]=='X')
q.push(make_pair(x-1,y));
if(y+1<m && A[x][y+1]=='X')
q.push(make_pair(x,y+1));
if(y-1>=0 && A[x][y-1]=='X')
q.push(make_pair(x,y-1));
}
}
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment