Workspace Number Of Enclaves
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Number of Enclaves visualizer

Learn Number of Enclaves with a step-by-step Graphs visualizer, JavaScript source code and a worked example. Practice traversal and connectivity.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 3Execution step
Reference example · JavaScript
Inspect reference input
{
  "a": [
    [
      0,
      0,
      0,
      0
    ],
    [
      1,
      0,
      1,
      0
    ],
    [
      0,
      1,
      1,
      0
    ],
    [
      0,
      0,
      0,
      0
    ]
  ],
  "mode": "count"
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
a,mode
},s)=>{
let value=mode==="capture"?"O":1,q=[],seen=new Set;
for(let i=0;
i<a.length;
i++)for(let j=0;
j<a[i].length;
j++)if((i===0||j===0||i===a.length-1||j===a[i].length-1)&&a[i][j]===value){
q.push([i,j]);
seen.add(i+","+j)
}
while(q.length){
let [i,j]=q.shift();
for(let [x,y] of dirs){
let r=i+x,c=j+y,key=r+","+c;
if(a[r]?.[c]===value&&!seen.has(key)){
seen.add(key);
q.push([r,c])
}
}
s("Mark cells connected to boundary",{
Grid:a,Queue:q,Safe:[...seen]
})
}
let c=0;
for(let i=0;
i<a.length;
i++)for(let j=0;
j<a[i].length;
j++)if(a[i][j]===value&&!seen.has(i+","+j)){
c++;
if(mode==="capture")a[i][j]="X"
}
s("Process enclosed cells",{
Grid:a,Enclosed:[c]
});
return mode==="capture"?a:c
};
function adj(n,edges,directed=false){
let a=Array.from({
length:n
},()=>[]);
for(let [u,v,w=1] of edges){
a[u].push([v,w]);
if(!directed)a[v].push([u,w])
}
return a
}
function dsu(n){
let p=Array.from({
length:n
},(_,i)=>i),z=p.map(()=>1);
function find(x){
while(p[x]!==x){
p[x]=p[p[x]];
x=p[x]
}
return x
}
function union(a,b){
a=find(a);
b=find(b);
if(a===b)return false;
if(z[a]<z[b])[a,b]=[b,a];
p[b]=a;
z[a]+=z[b];
return true
}
return {
p,z,find,union
}
}
const dirs=[[1,0],[-1,0],[0,1],[0,-1]];

Execution canvas

Active Changed
STEP 1

Mark cells connected to boundary

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Grid4 rows
Queue0 items

Empty at this step.

Safe1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Graphs · Traversal and connectivity

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "a": [
    [
      0,
      0,
      0,
      0
    ],
    [
      1,
      0,
      1,
      0
    ],
    [
      0,
      1,
      1,
      0
    ],
    [
      0,
      0,
      0,
      0
    ]
  ],
  "mode": "count"
}

Execution steps

  1. Mark cells connected to boundary
  2. Process enclosed cells

Computed output

3
JavaScript reference implementation
const solve = ({
  a,mode
},s)=>{
  let value=mode==="capture"?"O":1,q=[],seen=new Set;
  for(let i=0;
  i<a.length;
  i++)for(let j=0;
  j<a[i].length;
  j++)if((i===0||j===0||i===a.length-1||j===a[i].length-1)&&a[i][j]===value){
    q.push([i,j]);
    seen.add(i+","+j)
  }
  while(q.length){
    let [i,j]=q.shift();
    for(let [x,y] of dirs){
      let r=i+x,c=j+y,key=r+","+c;
      if(a[r]?.[c]===value&&!seen.has(key)){
        seen.add(key);
        q.push([r,c])
      }
    }
    s("Mark cells connected to boundary",{
      Grid:a,Queue:q,Safe:[...seen]
    })
  }
  let c=0;
  for(let i=0;
  i<a.length;
  i++)for(let j=0;
  j<a[i].length;
  j++)if(a[i][j]===value&&!seen.has(i+","+j)){
    c++;
    if(mode==="capture")a[i][j]="X"
  }
  s("Process enclosed cells",{
    Grid:a,Enclosed:[c]
  });
  return mode==="capture"?a:c
};
function adj(n,edges,directed=false){
  let a=Array.from({
    length:n
  },()=>[]);
  for(let [u,v,w=1] of edges){
    a[u].push([v,w]);
    if(!directed)a[v].push([u,w])
  }
  return a
}
function dsu(n){
  let p=Array.from({
    length:n
  },(_,i)=>i),z=p.map(()=>1);
  function find(x){
    while(p[x]!==x){
      p[x]=p[p[x]];
      x=p[x]
    }
    return x
  }
  function union(a,b){
    a=find(a);
    b=find(b);
    if(a===b)return false;
    if(z[a]<z[b])[a,b]=[b,a];
    p[b]=a;
    z[a]+=z[b];
    return true
  }
  return {
    p,z,find,union
  }
}
const dirs=[[1,0],[-1,0],[0,1],[0,-1]];

Continue learning

Practice related DSA questions in the same topic.