Counting sand islands
Statement
There is a photo of a river mouth taken from above. The photo is divided into N rows and N columns, and each square is either under water or sand that stands above the water. In the photo a sand square is written as 1 and a water square as 0. Row numbers are counted from the top and column numbers from the left, starting at 1.
Sand squares directly next to each other across or down can be walked between, so they count as one sand island. When sand squares next to each other run on in a chain, the whole chain is one sand island. Two sand squares next to each other only on a diagonal cannot be walked between, on the other hand, so they belong to different sand islands. The size of a sand island is the number of squares in it.
Write a program that, given the photo, finds the number of sand islands in the photo and the size of each sand island. The sizes are given from the smallest upward.
For example, suppose a photo with N=5 is as in the table below.
| Square | Column 1 | Column 2 | Column 3 | Column 4 | Column 5 |
|---|---|---|---|---|---|
| Row 1 | 0 | 1 | 1 | 0 | 1 |
| Row 2 | 1 | 1 | 1 | 0 | 1 |
| Row 3 | 1 | 1 | 0 | 1 | 1 |
| Row 4 | 1 | 0 | 0 | 0 | 0 |
| Row 5 | 1 | 0 | 0 | 1 | 0 |
This photo has three sand islands. The squares in each sand island and its size are as follows.
| Sand island | Squares in it | Size |
|---|---|---|
| First | Row 1 column 2, row 1 column 3, row 2 column 1, row 2 column 2, row 2 column 3, row 3 column 1, row 3 column 2, row 4 column 1, row 5 column 1 | 9 |
| Second | Row 1 column 5, row 2 column 5, row 3 column 5, row 3 column 4 | 4 |
| Third | Row 5 column 4 | 1 |
The sand in row 5 column 4 has water in both row 4 column 4 and row 5 column 5, so it leads nowhere and makes a sand island on its own. Also the sand in row 1 column 2 and the sand in row 1 column 5 are in the same row, but row 1 column 4 is water, so they are not joined and belong to different sand islands. So the number of sand islands is 3, and writing the sizes from the smallest upward gives 1, 4 and 9.
A photo can also be all water, with no sand island in it at all. In that case the count given is 0 and there is no size to write after it, and the shape of the output for that case is written out separately in the output conditions.
Input and Output Examples
Example 1
Input
5 01101 11101 11011 10000 10010
Output
3 1 4 9
Example 2
Input
1 0
Output
0
Example 3
Input
2 10 01
Output
2 1 1
Input
First the number of squares N along one side of the photo is given on the first line. N is a natural number from 1 to 25.
Next the information on each row of the photo is given over N lines, from the top row downward. Each line is a string of length N made only of 0 and 1, with no space between two of the numbers. The characters from the left mean column 1, column 2, and so on up to column N, where 1 is sand and 0 is water.
Output
Print the number of sand islands on the first line. Then print the size of each sand island, from the smallest upward, one per line.
If the photo is all water and there is no sand island at all, print 0 on the first line and then print one more line that holds nothing, a blank line. If there is one sand island or more, no blank line is added after the last size you print.
Requirements
- Sand squares next to each other across or down make one sand island
- Two sand squares next to each other only on a diagonal belong to different sand islands
- The size of a sand island is the number of squares in it
- Print the sizes from the smallest upward, one per line
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 0
0
2 10 01
2 1 1
5 01101 11101 11011 10000 10010
3 1 4 9
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.