Finding the existence of an enclosed space within a 2D M x N grid





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm tasked to find an the existence of an enclosed space within a grid provided by the questions using an algorithm. A space (' ') means that there is a hole while a hash ('#') means that there is a wall. Now suppose I have a two-dimensional M x N grid, how do I find if there's an existence of enclosed space? Examples of grid with enclosed space(s):



########
# #
# #
# #
# #
# #
# #
########

########
# #
# #
########
# #
# #
#
########

########
# # #
# # #
# ######
# #
# #
#
########

########
## #
# # #
# # #
# # #
# #
# #
########


At first, I tried to store this grid into a vector of strings. Then, I proceeded to check if each space is a hash or a space. If it's a space (its position will now be called as initial), I would check the surrounding areas of that space until I find a hole (space) that is reachable on the edges from said initial. If it's a hash, I'd continue with the next 'square' in the grid.



However, my algorithm of trying to just brute force every single possibility seems very tedious and inefficient. Should I learn about some other concepts before proceeding with this task (reading more about maps, paths, trees, etc.). If there is, could you please provide me what to read first? If there isn't, could you please guide me?



And is my approach to solving this task correct?










share|improve this question























  • Do you know about DFS algorithm? If no, then learn and apply it for 2D grid.

    – Shudipta Sharma
    Nov 24 '18 at 12:27











  • Nope, okay I will :-) I knew that I was missing something.

    – Richard W
    Nov 24 '18 at 12:29











  • @ShudiptaSharma hi I'm having trouble implementing DFS to this particular case. Would you mind giving me a nudge to the right path?

    – Richard W
    Nov 24 '18 at 13:45











  • added my answer. let's check it.

    – Shudipta Sharma
    Nov 24 '18 at 15:13


















0















I'm tasked to find an the existence of an enclosed space within a grid provided by the questions using an algorithm. A space (' ') means that there is a hole while a hash ('#') means that there is a wall. Now suppose I have a two-dimensional M x N grid, how do I find if there's an existence of enclosed space? Examples of grid with enclosed space(s):



########
# #
# #
# #
# #
# #
# #
########

########
# #
# #
########
# #
# #
#
########

########
# # #
# # #
# ######
# #
# #
#
########

########
## #
# # #
# # #
# # #
# #
# #
########


At first, I tried to store this grid into a vector of strings. Then, I proceeded to check if each space is a hash or a space. If it's a space (its position will now be called as initial), I would check the surrounding areas of that space until I find a hole (space) that is reachable on the edges from said initial. If it's a hash, I'd continue with the next 'square' in the grid.



However, my algorithm of trying to just brute force every single possibility seems very tedious and inefficient. Should I learn about some other concepts before proceeding with this task (reading more about maps, paths, trees, etc.). If there is, could you please provide me what to read first? If there isn't, could you please guide me?



And is my approach to solving this task correct?










share|improve this question























  • Do you know about DFS algorithm? If no, then learn and apply it for 2D grid.

    – Shudipta Sharma
    Nov 24 '18 at 12:27











  • Nope, okay I will :-) I knew that I was missing something.

    – Richard W
    Nov 24 '18 at 12:29











  • @ShudiptaSharma hi I'm having trouble implementing DFS to this particular case. Would you mind giving me a nudge to the right path?

    – Richard W
    Nov 24 '18 at 13:45











  • added my answer. let's check it.

    – Shudipta Sharma
    Nov 24 '18 at 15:13














0












0








0








I'm tasked to find an the existence of an enclosed space within a grid provided by the questions using an algorithm. A space (' ') means that there is a hole while a hash ('#') means that there is a wall. Now suppose I have a two-dimensional M x N grid, how do I find if there's an existence of enclosed space? Examples of grid with enclosed space(s):



########
# #
# #
# #
# #
# #
# #
########

########
# #
# #
########
# #
# #
#
########

########
# # #
# # #
# ######
# #
# #
#
########

########
## #
# # #
# # #
# # #
# #
# #
########


At first, I tried to store this grid into a vector of strings. Then, I proceeded to check if each space is a hash or a space. If it's a space (its position will now be called as initial), I would check the surrounding areas of that space until I find a hole (space) that is reachable on the edges from said initial. If it's a hash, I'd continue with the next 'square' in the grid.



However, my algorithm of trying to just brute force every single possibility seems very tedious and inefficient. Should I learn about some other concepts before proceeding with this task (reading more about maps, paths, trees, etc.). If there is, could you please provide me what to read first? If there isn't, could you please guide me?



And is my approach to solving this task correct?










share|improve this question














I'm tasked to find an the existence of an enclosed space within a grid provided by the questions using an algorithm. A space (' ') means that there is a hole while a hash ('#') means that there is a wall. Now suppose I have a two-dimensional M x N grid, how do I find if there's an existence of enclosed space? Examples of grid with enclosed space(s):



########
# #
# #
# #
# #
# #
# #
########

########
# #
# #
########
# #
# #
#
########

########
# # #
# # #
# ######
# #
# #
#
########

########
## #
# # #
# # #
# # #
# #
# #
########


At first, I tried to store this grid into a vector of strings. Then, I proceeded to check if each space is a hash or a space. If it's a space (its position will now be called as initial), I would check the surrounding areas of that space until I find a hole (space) that is reachable on the edges from said initial. If it's a hash, I'd continue with the next 'square' in the grid.



However, my algorithm of trying to just brute force every single possibility seems very tedious and inefficient. Should I learn about some other concepts before proceeding with this task (reading more about maps, paths, trees, etc.). If there is, could you please provide me what to read first? If there isn't, could you please guide me?



And is my approach to solving this task correct?







algorithm






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 12:16









Richard WRichard W

437115




437115













  • Do you know about DFS algorithm? If no, then learn and apply it for 2D grid.

    – Shudipta Sharma
    Nov 24 '18 at 12:27











  • Nope, okay I will :-) I knew that I was missing something.

    – Richard W
    Nov 24 '18 at 12:29











  • @ShudiptaSharma hi I'm having trouble implementing DFS to this particular case. Would you mind giving me a nudge to the right path?

    – Richard W
    Nov 24 '18 at 13:45











  • added my answer. let's check it.

    – Shudipta Sharma
    Nov 24 '18 at 15:13



















  • Do you know about DFS algorithm? If no, then learn and apply it for 2D grid.

    – Shudipta Sharma
    Nov 24 '18 at 12:27











  • Nope, okay I will :-) I knew that I was missing something.

    – Richard W
    Nov 24 '18 at 12:29











  • @ShudiptaSharma hi I'm having trouble implementing DFS to this particular case. Would you mind giving me a nudge to the right path?

    – Richard W
    Nov 24 '18 at 13:45











  • added my answer. let's check it.

    – Shudipta Sharma
    Nov 24 '18 at 15:13

















Do you know about DFS algorithm? If no, then learn and apply it for 2D grid.

– Shudipta Sharma
Nov 24 '18 at 12:27





Do you know about DFS algorithm? If no, then learn and apply it for 2D grid.

– Shudipta Sharma
Nov 24 '18 at 12:27













Nope, okay I will :-) I knew that I was missing something.

– Richard W
Nov 24 '18 at 12:29





Nope, okay I will :-) I knew that I was missing something.

– Richard W
Nov 24 '18 at 12:29













@ShudiptaSharma hi I'm having trouble implementing DFS to this particular case. Would you mind giving me a nudge to the right path?

– Richard W
Nov 24 '18 at 13:45





@ShudiptaSharma hi I'm having trouble implementing DFS to this particular case. Would you mind giving me a nudge to the right path?

– Richard W
Nov 24 '18 at 13:45













added my answer. let's check it.

– Shudipta Sharma
Nov 24 '18 at 15:13





added my answer. let's check it.

– Shudipta Sharma
Nov 24 '18 at 15:13












1 Answer
1






active

oldest

votes


















1














The idea is:




  • we start from every non-visited empty cell

  • try to visit all connected empty cells

  • if we can go to the boundary then this is not an enclosed area

  • if no cell of the connected region is boundary cell then the region is enclosed by wall and we increament the count.


Here is sample implementation in c++ that count the number of enclosed area:



#include <string.h>
#include <cstdio>

// m is row_num, n is column_num
int m, n;
// grid
char grid[5005][5005];
// direction arrays
int R = {0, -1, 0, 1};
int C = {1, 0, -1, 0};
// check for weather we reach boundary or not
// and visit array
bool wentToBoundary, vis[5005][5005];

// DFS implementation of 2D grid
void dfs(int x, int y) {
// visit the cell grid[x][y] as true
vis[x][y] = true;

// if the curren cell is a boundary cell, then mark that
// we reach to boundary from an inner cell
if (x == 0 || x == m -1 || y == 0 || y == n - 1)
wentToBoundary = true;

// try to go in all 4 direction (right, up, left, down)
// if the cell is not visited yet and contans ' '
for (int i = 0; i < 4; i++) {
int xx = x + R[i];
int yy = y + C[i];

if (xx >=0 && xx < m && yy >= 0 && yy < n) {
if (!vis[xx][yy] && grid[xx][yy] == ' ')
dfs(xx, yy);
}
}
}

int main() {
// input the grid size;
scanf("%d %d", &m, &n);
getchar();

// input the grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%c", &grid[i][j]);
}
getchar();
}

// initialize
int spaceEnclosedCount = 0;
memset(vis, false, sizeof(vis));

// iterate only for inner cells not the boundary cells
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (!vis[i][j] && grid[i][j] == ' ') {
wentToBoundary = false;
dfs(i, j);

if (!wentToBoundary) {
spaceEnclosedCount++;
}
}
}
}

printf("number of area enclosed by spaces: %dn", spaceEnclosedCount);

return 0;
}





share|improve this answer


























  • Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

    – Richard W
    Nov 28 '18 at 4:32











  • I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

    – Richard W
    Nov 28 '18 at 4:34











  • You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

    – Shudipta Sharma
    Nov 28 '18 at 5:19











  • Ah, you mean that flood-fill alg has the same concept as a DFS alg?

    – Richard W
    Nov 28 '18 at 5:33











  • Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

    – Shudipta Sharma
    Nov 28 '18 at 6:10












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458062%2ffinding-the-existence-of-an-enclosed-space-within-a-2d-m-x-n-grid%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The idea is:




  • we start from every non-visited empty cell

  • try to visit all connected empty cells

  • if we can go to the boundary then this is not an enclosed area

  • if no cell of the connected region is boundary cell then the region is enclosed by wall and we increament the count.


Here is sample implementation in c++ that count the number of enclosed area:



#include <string.h>
#include <cstdio>

// m is row_num, n is column_num
int m, n;
// grid
char grid[5005][5005];
// direction arrays
int R = {0, -1, 0, 1};
int C = {1, 0, -1, 0};
// check for weather we reach boundary or not
// and visit array
bool wentToBoundary, vis[5005][5005];

// DFS implementation of 2D grid
void dfs(int x, int y) {
// visit the cell grid[x][y] as true
vis[x][y] = true;

// if the curren cell is a boundary cell, then mark that
// we reach to boundary from an inner cell
if (x == 0 || x == m -1 || y == 0 || y == n - 1)
wentToBoundary = true;

// try to go in all 4 direction (right, up, left, down)
// if the cell is not visited yet and contans ' '
for (int i = 0; i < 4; i++) {
int xx = x + R[i];
int yy = y + C[i];

if (xx >=0 && xx < m && yy >= 0 && yy < n) {
if (!vis[xx][yy] && grid[xx][yy] == ' ')
dfs(xx, yy);
}
}
}

int main() {
// input the grid size;
scanf("%d %d", &m, &n);
getchar();

// input the grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%c", &grid[i][j]);
}
getchar();
}

// initialize
int spaceEnclosedCount = 0;
memset(vis, false, sizeof(vis));

// iterate only for inner cells not the boundary cells
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (!vis[i][j] && grid[i][j] == ' ') {
wentToBoundary = false;
dfs(i, j);

if (!wentToBoundary) {
spaceEnclosedCount++;
}
}
}
}

printf("number of area enclosed by spaces: %dn", spaceEnclosedCount);

return 0;
}





share|improve this answer


























  • Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

    – Richard W
    Nov 28 '18 at 4:32











  • I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

    – Richard W
    Nov 28 '18 at 4:34











  • You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

    – Shudipta Sharma
    Nov 28 '18 at 5:19











  • Ah, you mean that flood-fill alg has the same concept as a DFS alg?

    – Richard W
    Nov 28 '18 at 5:33











  • Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

    – Shudipta Sharma
    Nov 28 '18 at 6:10
















1














The idea is:




  • we start from every non-visited empty cell

  • try to visit all connected empty cells

  • if we can go to the boundary then this is not an enclosed area

  • if no cell of the connected region is boundary cell then the region is enclosed by wall and we increament the count.


Here is sample implementation in c++ that count the number of enclosed area:



#include <string.h>
#include <cstdio>

// m is row_num, n is column_num
int m, n;
// grid
char grid[5005][5005];
// direction arrays
int R = {0, -1, 0, 1};
int C = {1, 0, -1, 0};
// check for weather we reach boundary or not
// and visit array
bool wentToBoundary, vis[5005][5005];

// DFS implementation of 2D grid
void dfs(int x, int y) {
// visit the cell grid[x][y] as true
vis[x][y] = true;

// if the curren cell is a boundary cell, then mark that
// we reach to boundary from an inner cell
if (x == 0 || x == m -1 || y == 0 || y == n - 1)
wentToBoundary = true;

// try to go in all 4 direction (right, up, left, down)
// if the cell is not visited yet and contans ' '
for (int i = 0; i < 4; i++) {
int xx = x + R[i];
int yy = y + C[i];

if (xx >=0 && xx < m && yy >= 0 && yy < n) {
if (!vis[xx][yy] && grid[xx][yy] == ' ')
dfs(xx, yy);
}
}
}

int main() {
// input the grid size;
scanf("%d %d", &m, &n);
getchar();

// input the grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%c", &grid[i][j]);
}
getchar();
}

// initialize
int spaceEnclosedCount = 0;
memset(vis, false, sizeof(vis));

// iterate only for inner cells not the boundary cells
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (!vis[i][j] && grid[i][j] == ' ') {
wentToBoundary = false;
dfs(i, j);

if (!wentToBoundary) {
spaceEnclosedCount++;
}
}
}
}

printf("number of area enclosed by spaces: %dn", spaceEnclosedCount);

return 0;
}





share|improve this answer


























  • Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

    – Richard W
    Nov 28 '18 at 4:32











  • I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

    – Richard W
    Nov 28 '18 at 4:34











  • You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

    – Shudipta Sharma
    Nov 28 '18 at 5:19











  • Ah, you mean that flood-fill alg has the same concept as a DFS alg?

    – Richard W
    Nov 28 '18 at 5:33











  • Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

    – Shudipta Sharma
    Nov 28 '18 at 6:10














1












1








1







The idea is:




  • we start from every non-visited empty cell

  • try to visit all connected empty cells

  • if we can go to the boundary then this is not an enclosed area

  • if no cell of the connected region is boundary cell then the region is enclosed by wall and we increament the count.


Here is sample implementation in c++ that count the number of enclosed area:



#include <string.h>
#include <cstdio>

// m is row_num, n is column_num
int m, n;
// grid
char grid[5005][5005];
// direction arrays
int R = {0, -1, 0, 1};
int C = {1, 0, -1, 0};
// check for weather we reach boundary or not
// and visit array
bool wentToBoundary, vis[5005][5005];

// DFS implementation of 2D grid
void dfs(int x, int y) {
// visit the cell grid[x][y] as true
vis[x][y] = true;

// if the curren cell is a boundary cell, then mark that
// we reach to boundary from an inner cell
if (x == 0 || x == m -1 || y == 0 || y == n - 1)
wentToBoundary = true;

// try to go in all 4 direction (right, up, left, down)
// if the cell is not visited yet and contans ' '
for (int i = 0; i < 4; i++) {
int xx = x + R[i];
int yy = y + C[i];

if (xx >=0 && xx < m && yy >= 0 && yy < n) {
if (!vis[xx][yy] && grid[xx][yy] == ' ')
dfs(xx, yy);
}
}
}

int main() {
// input the grid size;
scanf("%d %d", &m, &n);
getchar();

// input the grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%c", &grid[i][j]);
}
getchar();
}

// initialize
int spaceEnclosedCount = 0;
memset(vis, false, sizeof(vis));

// iterate only for inner cells not the boundary cells
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (!vis[i][j] && grid[i][j] == ' ') {
wentToBoundary = false;
dfs(i, j);

if (!wentToBoundary) {
spaceEnclosedCount++;
}
}
}
}

printf("number of area enclosed by spaces: %dn", spaceEnclosedCount);

return 0;
}





share|improve this answer















The idea is:




  • we start from every non-visited empty cell

  • try to visit all connected empty cells

  • if we can go to the boundary then this is not an enclosed area

  • if no cell of the connected region is boundary cell then the region is enclosed by wall and we increament the count.


Here is sample implementation in c++ that count the number of enclosed area:



#include <string.h>
#include <cstdio>

// m is row_num, n is column_num
int m, n;
// grid
char grid[5005][5005];
// direction arrays
int R = {0, -1, 0, 1};
int C = {1, 0, -1, 0};
// check for weather we reach boundary or not
// and visit array
bool wentToBoundary, vis[5005][5005];

// DFS implementation of 2D grid
void dfs(int x, int y) {
// visit the cell grid[x][y] as true
vis[x][y] = true;

// if the curren cell is a boundary cell, then mark that
// we reach to boundary from an inner cell
if (x == 0 || x == m -1 || y == 0 || y == n - 1)
wentToBoundary = true;

// try to go in all 4 direction (right, up, left, down)
// if the cell is not visited yet and contans ' '
for (int i = 0; i < 4; i++) {
int xx = x + R[i];
int yy = y + C[i];

if (xx >=0 && xx < m && yy >= 0 && yy < n) {
if (!vis[xx][yy] && grid[xx][yy] == ' ')
dfs(xx, yy);
}
}
}

int main() {
// input the grid size;
scanf("%d %d", &m, &n);
getchar();

// input the grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%c", &grid[i][j]);
}
getchar();
}

// initialize
int spaceEnclosedCount = 0;
memset(vis, false, sizeof(vis));

// iterate only for inner cells not the boundary cells
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (!vis[i][j] && grid[i][j] == ' ') {
wentToBoundary = false;
dfs(i, j);

if (!wentToBoundary) {
spaceEnclosedCount++;
}
}
}
}

printf("number of area enclosed by spaces: %dn", spaceEnclosedCount);

return 0;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 15:12

























answered Nov 24 '18 at 15:05









Shudipta SharmaShudipta Sharma

1,242414




1,242414













  • Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

    – Richard W
    Nov 28 '18 at 4:32











  • I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

    – Richard W
    Nov 28 '18 at 4:34











  • You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

    – Shudipta Sharma
    Nov 28 '18 at 5:19











  • Ah, you mean that flood-fill alg has the same concept as a DFS alg?

    – Richard W
    Nov 28 '18 at 5:33











  • Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

    – Shudipta Sharma
    Nov 28 '18 at 6:10



















  • Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

    – Richard W
    Nov 28 '18 at 4:32











  • I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

    – Richard W
    Nov 28 '18 at 4:34











  • You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

    – Shudipta Sharma
    Nov 28 '18 at 5:19











  • Ah, you mean that flood-fill alg has the same concept as a DFS alg?

    – Richard W
    Nov 28 '18 at 5:33











  • Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

    – Shudipta Sharma
    Nov 28 '18 at 6:10

















Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

– Richard W
Nov 28 '18 at 4:32





Hi, thank you for your answer! And I'm sorry for the very late reply, but I ended up using something called as flood-filled algorithm to find where the 'flood' can go in my rectangular shape by finding a space (' '), which indicates open-area, in the boundary and exploring its adjacent areas using recursive. Then, I will mark the 'visited' or 'flooded' area with some character and finally, I'll check the whole rectangular area. If there's an element that is not marked or not a boundary, then I'll find and enclosed space :-D

– Richard W
Nov 28 '18 at 4:32













I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

– Richard W
Nov 28 '18 at 4:34





I noticed that the flood-fill algorithm has a very similar or almost the same idea as your DFS search. Are they actually the same?

– Richard W
Nov 28 '18 at 4:34













You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

– Shudipta Sharma
Nov 28 '18 at 5:19





You are write. Actually mine is the "flood-fill" alg. We implement this using DFS or BFS alg.

– Shudipta Sharma
Nov 28 '18 at 5:19













Ah, you mean that flood-fill alg has the same concept as a DFS alg?

– Richard W
Nov 28 '18 at 5:33





Ah, you mean that flood-fill alg has the same concept as a DFS alg?

– Richard W
Nov 28 '18 at 5:33













Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

– Shudipta Sharma
Nov 28 '18 at 6:10





Yeah, in "flood-fill" alg, you need to visit the cells or nodes through a DFS/BFS.

– Shudipta Sharma
Nov 28 '18 at 6:10




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458062%2ffinding-the-existence-of-an-enclosed-space-within-a-2d-m-x-n-grid%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini