The "Surrounded Region" problem states:
"Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region."
I'm confused as to what the task is for this problem. I'm not clear on what dictates when a region is 'surrounded' based on all the examples found online(which happen to all be the same example).
The example given.
input output
X X X X X X X X
X O O X X X X X
X X O X X X X X
X O X X X O X X
Both groups of O's look surrounded by X's to me. Is the rule that all four sides need to be surrounded by X's? and since the bottom O doesn't have a X below it it's not 'captured'?
what happens if this is the input? is nothing captured at all?
X X X X
X O O O
X X O X
X O X X
According to the definition, if 'O' cell is surrounded by 'X' cells, i.e. up/down/left/right cells are 'X'.
The first thought could be for each 'O' cell, add it to an array, check its up/down/left/right and if it is another 'O' cell, continue until, it hits all 'X' cells or it hits boundary. In the former case, cells in the array can all be flipped to'X'; while in the latter case, cells in the array cannot be flipped.
Yes, exactly.
This surrounding and capturing is in fact, like a game (of GO). The edges cannot be captured, that's it. If you put your dots to edges, they will be yours till the end of the game. Also, surrounding means, if O's are surrounded by X's, then X's will form a cycle around O's. And whenever such a cycle completes, all the inside O's will be flipped to X's and vice-versa.
definition of cycle:
A cycle of X's or O's is any connected region where you start from a cell and return to it, without repeating (revisiting) a cell, and each step you can take a chess piece king's move to complete the path.
So, in your input example, the path:
(1,0)->(0,1)->(0,2)->(1,3)->(2,3)->(3,2)->(2,1)->(1,0) forms a cycle.
Related
Can anyone explain me how would scanline fill algorithm turn the blue positions into pink? I wanted to know how the iterations will change when an obstruction is observed. Take for example in the third row, the algorithm will easily consider positions 40 to 46 being part of the same group. But how and when will the algorithm iterate the positions 52 to 59?
If possible, please explain from left to right instead of top to bottom. Also mention an optimised method to iterate.
Here is the simple code from wikipedia:
fn fill(x, y):
if not Inside(x, y) then return
let s = new empty stack or queue
add (x, y) to s
while s is not empty:
Remove an (x, y) from s
let lx = x
while Inside(lx - 1, y):
Set(lx - 1, y)
lx = lx - 1
while Inside(x, y):
Set(x, y)
x = x + 1
scan(lx, x - 1, y + 1, s)
scan(lx, x - 1, y - 1, s)
fn scan(lx, rx, y, s):
let added = false
for x in lx .. rx:
if not Inside(x, y):
added = false
else if not added:
Add (x, y) to s
added = true
So "s" is a seed list, basically you put some random point (must be empty) to start off the algorithm.
while s is not empty:
Remove an (x, y) from s
This is a basic loop where we are consuming seeds and when there are no seeds left in "s" then the algorithm is finished.
let lx = x
while Inside(lx - 1, y):
Set(lx - 1, y)
lx = lx - 1
while Inside(x, y):
Set(x, y)
x = x + 1
This is keeping track of the current scan line where lx is the leftmost empty pixel and (x-1) is the rightmost empty pixel (note as this may be confusing, x will be incremented at least once no matter what since all seed points are assured to be inside points). The first loop steps to the left and decrements lx every time it finds an empty pixel. The second loop steps to the right and increments x every time it finds an empty pixel. You can set pixel by pixel as stated, but depending on your library (if drawing lines is cheaper than pixel by pixel editing) you can instead draw a line after passing both of these loops, extending the line between (lx,y) and (x-1,y).
scan(lx, x - 1, y + 1, s)
scan(lx, x - 1, y - 1, s)
This is the "re-seeding" step. Basically the above steps consume seeds, this is the step that produces new seeds based on the results of the seed that was just consumed. "y+1" and "y-1" are single pixel offsets above and below the line that was just drawn, while "lx" and "x-1" describe the line that was just computed this step.
fn scan(lx, rx, y, s):
let added = false
for x in lx .. rx:
if not Inside(x, y):
added = false
else if not added:
Add (x, y) to s
added = true
All that is happening here is that any empty pixels on the scan line (remember this is above and below the last line checked based on arguments) is being added to the seed array to be later tested (this is not incrementing forward and backwards but exhaustively searching every point along the line). Keep in mind that s needs to be a byref argument here since you will be editing it.
One additional note: when I say "empty pixel" I mean two things (though they generally are handled the same in this aglo): 1 the pixel is not an edge point, 2 the pixel has not already be filled by the algo.
As can be seen in the graphic below, any seed can be pulled from the seed list in any order and the algo still functions properly.
(λy.x z)c
I think a answer about this problem is x z.
If it is correct, why (λy.x z)c = x c is incorrect?
In this case, I refer to (λy.x z) = (λy.x)z = x. So I calculate it in the parenthesis first.
(λy.x z) c is not a problem, it is a λ-term.
You refer to λy.x z = (λy.x) z but there is no way to move the parentheses, otherwise it would mean they were useless.
λy. x z
Means the function which takes y as argument and returns x applied to z.
While (λy.x) z means the function which takes y as argument and returns x, the whole thing applied to z. Why would those two things be the same?
(They are not.)
Am a right or not.
The predicate logic for the following Sentences.
A cup is above a book.
\x exists \y exists cup(x) and book(y) and above(x,y)
A cup is touching a book.
\x exists \y exists cup(x) and book(y) and touches(x,y).
if x is on top of y then y supports x.
for all x For all y on(x,y) implies support(y,x).
If x is above y and they are touching each other then x is on top of y.
for all x for all y above(x,y) and touches(x,y) implies on(x,y).
Am i right?
All your formulae seem reasonable representations to me.
I would add extra brackets around above(x,y) and touches(x,y) to ensure that both together form the antecedent of the implication. Otherwise it could be incorrectly interpretted as above(x,y) and (touches(x,y) implies on(x,y))
What is the language generated by this language? i would say its all words with exactly 2 or 3 b's but i'm not quite sure.
Any number of 'a's before, after and between either 2 or 3 'b's.
It is progressive... any number of S, followed by any number of X, followed by any number of Y, with, optionally, any number of Z. Each of these elements can be any number of the character 'a'. S, X, and Y all move on to the next element when a 'b' is encountered. Y can terminate before a 'b' is seen (thus, a 'b' from S and a 'b' from X are guaranteed, but not one from Y).
Definition : A relation on set S and T is any subset of the Cartesian product S x T.
Example:
The "larger than" relation for real numbers is the set:
L = {(x, y) | x, y E R, x > y}
Here the part I don't understand it says :
"Here S and T both equal R".
What does it mean?
The elements of set S is equal R and also the elements of set T is eqaul to R.
so S and T is also equal?
The elements of set S is equal R and also the elements of set T is eqaul to R. so S and T is also equal?
Exactly. As both x and y are real numbers, they are taken from the very same set, therefore they are sharing the same domain.