描述
On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point may contain at most 1 ship.
You have a function Sea.hasShips(topRight, bottomLeft)
which takes two points as arguments and returns true if and only if there is at least one ship in the rectangle represented by the two points, including on the boundary.
Given two points, which are the top right and bottom left corners of a rectangle, return the number of ships present in that rectangle. It is guaranteed that there are at most 10 ships in that rectangle.
Submissions making more than 400 calls to hasShips
will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.
Example:
1 | Input: |
Constraints:
1 | On the input ships is only given to initialize the map internally. You must solve this problem "blindfolded". In other words, you must find the answer using the given hasShips API, without knowing the ships position. |
思路
- 每次按照中心点分成4块(左上, 左下, 右上, 右下), 然后递归检查这四块, 如果
hasShips
返回false
就直接跳过. - hack: Python的话可以用
print sea.__dict__
直接打印出sea
这个class的所有属性, 对于样例来说, 结果是{'_Sea__guesses': 400, '_Sea__ans': [[1, 1], [2, 2], [3, 3], [5, 5]]}
. 相当于保存答案的字典已经拿到了. 直接用sea._Sea__ans
可以access到.
代码
1 | # """ |
1 | class Solution(object): |