Counting ore regions
Statement
A quarry's cut face has been photographed as a grid of N rows and N columns. Each cell of the photo holds one character for the mineral found at that spot. Ruby is written "R", garnet is written "G", and beryl is written "B". Every cell holds one of these three characters, and there is no cell without a mineral.
Cells that hold the same mineral and touch each other up, down, left, or right form one region. Cells that touch only at a diagonal are not treated as connected. The quarry counts these regions to decide how many pieces to cut the face into, so knowing the exact number of regions matters.
A skilled appraiser tells all three minerals apart, so the appraiser counts regions from the characters exactly as written in the photo. The handheld tester kept at the site cannot separate the colors of ruby and garnet, so it reads the two minerals as one and the same mineral. In other words, the photo the tester sees is the photo with every "R" changed to "G". Beryl reads the same way under both standards.
Write a program that takes the photo and computes the number of regions the appraiser counts and the number of regions the tester counts, in that order.
For example, suppose the photo with N=3 is the one in the table below. Each position is written as (row, column), and row and column numbers start at 1.
| G | B | G |
|---|---|---|
| G | G | R |
| R | R | B |
The appraiser sees six regions. Writing them out gives the table below.
| Region | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Mineral | Garnet | Garnet | Beryl | Beryl | Ruby | Ruby |
| Cells | (1, 1), (2, 1), (2, 2) | (1, 3) | (1, 2) | (3, 3) | (2, 3) | (3, 1), (3, 2) |
The garnet at (1, 1) and the garnet at (1, 3) are the same mineral, but a beryl sits between them, so they are not connected and belong to different regions.
The photo the tester sees is shown below. All three cells that held ruby read as garnet.
| G | B | G |
|---|---|---|
| G | G | G |
| G | G | B |
In this photo the seven cells that read as garnet are all connected into one region. The beryl regions are still (1, 2) and (3, 3), so there are two of them. The tester therefore counts three regions.
The answer for this example is 6 and 3.
Input and Output Examples
Example 1
Input
3 GBG GGR RRB
Output
6 3
Example 2
Input
2 RB RR
Output
2 2
Example 3
Input
5 RRRBB GGBBB BBBRR BBRRR RRRRR
Output
4 3
Input
First the side length N of the photo is given as a natural number. N is a natural number from 1 to 70.
Then N lines follow, giving the rows of the photo from top to bottom in order. Each line is N characters joined together with no spaces, and each character is one of "R", "G", and "B". "R" means ruby, "G" means garnet, and "B" means beryl. A photo in which not all three minerals appear may be given.
Output
Print the number of regions the appraiser counts and the number of regions the tester counts on one line, in that order. Separate the two numbers with a single space. The space goes only between the two numbers, so no space is left after the last number on the line.
Requirements
- Count cells that hold the same mineral and touch up, down, left, or right as one region
- Do not treat cells that touch only at a diagonal as connected
- The tester reads ruby as the same mineral as garnet, so count its regions on the photo with every "R" changed to "G"
- Print the two numbers on one line in order, separated by a single space
- 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 16 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 16 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 16 cases to solve this.
2 RB RR
2 2
3 GBG GGR RRB
6 3
5 RRRBB GGBBB BBBRR BBRRR RRRRR
4 3
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.