Algorithm: Determine shape of two sectors delineated by an arbitrary path, and then fill one.
Posted
by Arseniy Banayev
on Stack Overflow
See other posts from Stack Overflow
or by Arseniy Banayev
Published on 2010-05-15T21:07:36Z
Indexed on
2010/05/15
21:14 UTC
Read the original article
Hit count: 235
NOTE: This is a challenging problem for anybody who likes logic problems, etc.
Consider a rectangular two-dimensional grid of height H and width W. Every space on the grid has a value, either 0
1
or 2
. Initially, every space on the grid is a 0
, except for the spaces along each of the four edges, which are initially a 2
.
Then consider an arbitrary path of adjacent (horizontally or vertically) grid spaces. The path begins on a 2
and ends on a different 2
. Every space along the path is a 1
.
The path divides the grid into two "sectors" of 0
spaces. There is an object that rests on an unspecified 0
space. The "sector" that does NOT contain the object must be filled completely with 2
.
Define an algorithm that determines the spaces that must become 2
from 0
, given an array (list) of values (0
, 1
, or 2
) that correspond to the values in the grid, going from top to bottom and then from left to right. In other words, the element at index 0 in the array contains the value of the top-left space in the grid (initially a 2
). The element at index 1 contains the value of the space in the grid that is in the left column, second from the top, and so forth. The element at index H contains the value of the space in the grid that is in the top row but second from the left, and so forth.
Once the algorithm finishes and the empty "sector" is filled completely with 2
s, the SAME algorithm must be sufficient to do the same process again. The second (and on) time, the path is still drawn from a 2
to a different 2
, across spaces of 0
, but the "grid" is smaller because the 2
s that are surrounded by other 2
s cannot be touched by the path (since the path is along spaces of 0
).
I thank whomever is able to figure this out for me, very very much. This does not have to be in a particular programming language; in fact, pseudo-code or just English is sufficient. Thanks again! If you have any questions, just leave a comment and I'll specify what needs to be specified.
© Stack Overflow or respective owner