SnakeCount
SRM 284 · 2006-01-21 · by dgoodman
Problem Statement
- A snake is never adjacent to a snake-colored pixel that is not part of itself, not even diagonally.
- There is a sequence of orthogonally adjacent pixels that traces out the entire snake from its head to its tail.
- Two pixels of a snake cannot be orthogonally adjacent unless they are connected in the snake (i.e., are neighbors in the sequence that traces out the snake).
- A snake occupies between 3 and 20 pixels, inclusive.
11110
01110
Create a class SnakeCount that contains a method number that is given a
Notes
- A pixel is "orthogonally" adjacent to another if it is directly above, below, left or right of the other pixel.
Constraints
- image will contain between 1 and 50 elements, inclusive.
- Each element of image will contain exactly n characters, where n is between 1 and 50, inclusive.
- Each character in each element of image will be '0' (zero) or '1' (one).
{"11111111",
"00000010",
"11100000",
"00010001",
"10110011"}
Returns: 1
The possible snake at the top does not have a head to tail connection that includes all its pixels. The 2 possible snakes of length 3 in the middle of the image are eliminated because each is adjacent to a snake-colored pixel diagonally. The possible snake in the lower left corner is too short. There is a snake in the lower right corner.
{"110111",
"110101",
"000110"}
Returns: 1
The four pixels in the upper left do not make a snake -- the head of a snake cannot be orthogonally adjacent to its tail. The 7 snake-colored pixels in the right 3 columns do make a legitimate snake.
{"111","101","111"}
Returns: 0
This is not a snake since the head cannot be orthogonally adjacent to the tail.
{"111","111","101"}
Returns: 0
{"110000",
"001111",
"000000"}
Returns: 0
{
"11111111111111111111111111111111111111111111111111",
"00000000000000000000000000000000000000000000000001",
"11111111111111111111111111111111111111111111111111"
}
Returns: 0
A snake can not occupy more than 20 pixels.
{
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"10101010101010101010101010101010101010101010101010",
"00000000000000000000000000000000000000000000000000",
"11011011011011011011011011011011011011011011011011",
"10010010010010010010010010010010010010010010010001"
}
Returns: 317
A lot of snakes.
Submissions are judged against all 79 archived test cases, of which 7 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SnakeCount with a public method int number(vector<string> image) · 79 test cases · 2 s / 256 MB per case