Stamp tour of a shopping mall
Statement
The shops of a shopping mall are laid out neatly in R rows and C columns. Each square holds one shop, and each shop carries one uppercase letter that shows its trade. Shops with the same trade can sit in several places in the building.
The mall runs a stamp tour. A visitor starts at the shop in row 1 column 1 and takes one stamp there. After that the visitor can only walk to a shop directly next to the shop they are standing in, along a row or along a column, and takes one more stamp at the shop they arrive at. A stamp from the same trade cannot be taken twice, though. That is, the visitor cannot enter a shop that carries the same letter as a trade they already have.
The visitor ends the tour when there is nowhere left to go. The stamp taken at the starting shop counts toward the number as well. Write a program that, given the trades in the mall, works out the largest number of stamps one visitor can collect.
For example, suppose the trades of a mall with R=3 and C=6 are as in the table below.
| Square | Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 |
|---|---|---|---|---|---|---|
| Row 1 | Z | I | Q | C | W | E |
| Row 2 | Z | U | L | W | K | K |
| Row 3 | Y | G | O | M | W | U |
Walking as in the table below collects 11 stamps.
| Step | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Square | Row 1 column 1 | Row 1 column 2 | Row 2 column 2 | Row 3 column 2 | Row 3 column 3 | Row 2 column 3 | Row 1 column 3 | Row 1 column 4 | Row 1 column 5 | Row 1 column 6 | Row 2 column 6 |
| Trade | Z | I | U | G | O | L | Q | C | W | E | K |
The visitor started at the Z in row 1 column 1, so they cannot enter the Z in row 2 column 1, and once they have taken the W in row 1 column 5 they cannot enter the W in row 2 column 4 or the W in row 3 column 5 either. There is no way to take 12 or more, so the answer in this example is 11.
Input and Output Examples
Example 1
Input
3 6 ZIQCWE ZULWKK YGOMWU
Output
11
Example 2
Input
2 2 BB AA
Output
2
Example 3
Input
5 5 EDACD EEBEE DDAEE ABECB DCDCE
Output
4
Input
First the number of rows R and the number of columns C of the mall are given on the first line, separated by a space. R and C are each a natural number from 1 to 20.
Next the trades of each row are given over R lines, from the top row downward. Each line is a string of C uppercase letters run together with no spaces, and the letters from the left give the trades of column 1, column 2, and so on up to column C. The same letter can appear on several squares.
Output
Print the largest number of stamps one visitor can collect on the first line. The stamp taken at the starting shop in row 1 column 1 counts toward the number as well, so the answer is always 1 or more.
Requirements
- The starting square is always row 1 column 1
- A move only goes to a square directly next to the current one along a row or a column
- A square carrying the same letter as a trade already taken cannot be entered
- The stamp taken at the starting square counts toward the number
- 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.
2 2 BB AA
2
5 5 EDACD EEBEE DDAEE ABECB DCDCE
4
3 6 ZIQCWE ZULWKK YGOMWU
11
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.