问题
I was trying to solve the Game of life problem for a test. Rules of that game are:
Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors’ lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overcrowding. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
I tested my work on various patterns like Block, Boat, Blinker and Toad pattern shown below. But my code is not giving expected output for toad pattern as shown...although its working fine for every other pattern.
I am getting this output for TOAD:
X--X
X---
--X-
I checked various websites and they also showing the same output as below, but if we apply the rule, the cell in the 2nd row and last column cannot be alive.
So can anyone please tell me which is the right output? I have to be sure as it is for my test...
Thanks..
**Expected Output**
1. Block Pattern
Input
X X
X X
Output
X X
X X
2. Boat Pattern
Input
X X -
X - X
- X -
Output
X X -
X - X
- X -
3. Blinker Pattern
Input
- X -
- X -
- X -
Output
- - -
X X X
- - -
4. Toad Pattern
Input
- X X X
X X X -
- - X -
Output
X - - X
X - - X
- X - -
回答1:
Your output from your described input for TOAD matches the rules you've stated. The stated expected output doesn't match the rules.
It's not clear you have a problem in your program, but in your interpretation of TOAD, as noted by veredesmarald.
I'll also note that you defined input/expected-output for TOAD as follows:
Input
- X X X
X X X -
- - X -
Output
X - - X
X - - X
- X - -
And that if I move the line "Output" up one line the result matches the standard/expected definition of TOAD input/output:
Input
- X X X
X X X -
Output // swapped with line below
- - X -
X - - X
X - - X
- X - -
It appears something simply got lost in translation, and your program may be fine.
回答2:
All the examples of the toad pattern I found via google look like this:
State 1:
- - - -
- x x x
x x x -
- - - -
State 2:
- - x -
x - - x
x - - x
- x - -
These two states oscillate like so:
Your input appears to be missing the top row and also has an extra live cell in the bottom row. As a side note, the "bloat" pattern you mentioned is actually called "boat", because it looks like an overhead view of a small boat.
See:
- Wikipedia
- Wonders of Math
来源:https://stackoverflow.com/questions/11982217/toad-pattern-in-game-of-life