Connection Status:
Competition Arena > GardenHose
SRM 112 · 2002-09-06 · by dgoodman · Simple Math
Class Name: GardenHose
Return Type: int
Method Name: countDead
Arg Types: (int, int, int, int)
Problem Statement

Problem Statement

Our garden is a square containing plants in n rows and n columns, a total of n*n plants. The distance between plants within a row is rowDist and between plants within a column is colDist.

I want to water the garden without getting my shoes muddy. That requires that I stand outside the garden, never closer than where the next row or column of the garden would be if it were enlarged. The hose can water plants that are hoseDist or less away from where I am standing. (Of course, I can move around and water from various locations.)

Create a class GardenHose that contains the method countDead that takes n, rowDist, colDist, and hoseDist as inputs and returns the number of plants that cannot be watered.

Constraints

  • n is between 1 and 50 inclusive
  • rowDist is between 1 and 50 inclusive
  • colDist is between 1 and 50 inclusive
  • hoseDist is between 1 and 10,000 inclusive
Examples
0)
3
2
1
2
Returns: 0

In the picture below, each plant is shown with a P, and the closest spots where I can stand without getting muddy shoes are shown with o. Even the center plant can be watered by standing at the end of its column. 0 1 2 3 4 5 6 7 8 0 ooooooooooooooooooooooooo o | | | | | | | o 1 o--+--P--+--P--+--P--+--o o | | | | | | | o 2 o--+--P--+--P--+--P--+--o o | | | | | | | o 3 o--+--P--+--P--+--P--+--o o | | | | | | | o 4 ooooooooooooooooooooooooo

1)
3
2
1
1
Returns: 3

(Same picture) Now the hose cannot reach any of the plants in the middle row.

2)
4
50
2
2
Returns: 8
3)
4
50
2
4
Returns: 0
4)
4
3
2
3
Returns: 4

Submissions are judged against all 28 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class GardenHose with a public method int countDead(int n, int rowDist, int colDist, int hoseDist) · 28 test cases · 2 s / 256 MB per case

Submitting as anonymous