Number of charging cables
Statement
A parking lot is a grid, M squares across and N squares down. One car can be parked on each square. The position of a square is given by a horizontal coordinate and a vertical coordinate. The horizontal coordinate is an integer from 0 to M-1, counted from the left, and the vertical coordinate is an integer from 0 to N-1, counted from the top.
K electric cars are parked in this lot, and the attendant has to charge every electric car that is parked. The charging cable is wound on a reel, and the cable pulled off one reel can be run on as far as a square directly next to it, across or down. So electric cars next to each other across or down can all be charged from one reel. Even when several cars next to each other run on in a line, one reel is enough, since the cable can be run on from one car to the next. Two electric cars next to each other only on a diagonal, on the other hand, cannot be charged from one reel.
Write a program that, given the positions of the electric cars, works out the smallest number of reels needed to charge them all. The attendant does this work over several days, so T different parking states are given and the answer has to be found for each day.
For example, think about one day with K=3 electric cars parked in a lot M=5 squares across and N=3 squares down. The positions of the three cars are in the table below.
| Car number | 1 | 2 | 3 |
|---|---|---|---|
| Horizontal coordinate | 2 | 3 | 0 |
| Vertical coordinate | 1 | 2 | 1 |
Drawing this parking state onto the shape of the lot gives the table below. In the table V is a square with an electric car on it.
| Square | Horizontal 0 | Horizontal 1 | Horizontal 2 | Horizontal 3 | Horizontal 4 |
|---|---|---|---|---|---|
| Vertical 0 | |||||
| Vertical 1 | V | V | |||
| Vertical 2 | V |
No two of the three electric cars are next to each other across or down. The car at horizontal 0, vertical 1 and the car at horizontal 2, vertical 1 have the same vertical coordinate, but the one square between them is empty, so they are apart. The car at horizontal 2, vertical 1 and the car at horizontal 3, vertical 2 are next to each other only on a diagonal, so they cannot share a reel. Three reels have to be used, and the answer for this day is 3.
Input and Output Examples
Example 1
Input
1 5 3 3 2 1 3 2 0 1
Output
3
Example 2
Input
2 2 3 3 0 0 1 1 1 0 3 2 3 2 1 1 0 2 0
Output
1 1
Example 3
Input
1 1 1 1 0 0
Output
1
Input
First the number of parking states T is given on the first line. T is a natural number from 1 to 10.
Next the T parking states are given in order.
The first line of each parking state holds the number of squares across M and the number of squares down N of the lot, and the number of electric cars K, separated by spaces. M and N are each a natural number from 1 to 50, and K is a natural number from 1 to M times N.
Next the horizontal coordinate and the vertical coordinate of the square each electric car is parked on are given over K lines, separated by a space. The horizontal coordinate comes first and the vertical coordinate comes after it. The horizontal coordinate is an integer from 0 to M-1, and the vertical coordinate is an integer from 0 to N-1. The same square is never given twice.
Output
For the T parking states, print the smallest number of reels needed to charge every electric car, one per line, in order. Keep the order in which the parking states were given.
Requirements
- Electric cars next to each other across or down are charged together from one reel
- Two electric cars next to each other only on a diagonal cannot be charged from one reel
- The position of each square is given with the horizontal coordinate first and the vertical coordinate after it
- Print the answer for each parking state, 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 1 1 1 0 0
1
1 5 3 3 2 1 3 2 0 1
3
2 2 3 3 0 0 1 1 1 0 3 2 3 2 1 1 0 2 0
1 1
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
Nobody yet. You could be the first.