编程介的小学生 2017-02-22 12:14 采纳率: 20.5%
浏览 810
已采纳

Farewell, My Friend

Liz and Lilly used to be very good friends, but recently, they got into a silly quarrel and finally decided to say farewell to each other. 'I don't want to see you any more! I must put some new rocks somewhere so that no matter how I travel from my house, I can never see your face.' They both said.
They live in a small village which is divided into n*n grid. Liz always lives in the top-left corner (i.e grid (1,1)), and Lilly always lives in the bottom-right corner. (i.e gird(n,n)). Each grid of land is one of the following types: land, lake or rock. They cannot move across a rock or a lake, of course. And although people cannot see through a rock, it's easy for them to see through a piece of land or a lake. Note that they can only move north, south, east or west one grid at a time, NOT diagonally, and they are a bit shortsighted - they can only see things that are no more than k grids away in front of them (in the same row or column, they don't see anyhing diagonally).

Since they're both lazy, they want to put as few new rocks as possible. A new rock can only be put on a piece of land that at least one of the two girls can reach from her house. Note that they don't want to put new rocks too close to their houses, so the new rocks must be at least m grids away from both of the houses. By definition, grid(x1,y1) and grid(x2,y2) are supposed to be abs(x1-x2)+abs(y1-y2) grids away from each other.

Input

The input will contain no more than 8 test cases. Each test case begins with a line containing three integers n, k and m(5<=n<=20, 1<=k<=n, 1<=m<=n) separated by a single space. The following n lines each contains n characters indicating the map of a village. The capital letter 'O' represents a lake, '*' represents a rock, '.' represents a piece of land. The test case containing n=0, k=0, m=0 will terminate the input, you should not give an answer to this 'test case'. No extra spaces at the beginning/end of each line.

Output

Output the least number of new rocks that must be put in order to separate them. Print your answer in a single line for each test case. If no solution found, you should output -1 in the corresponding line.

Sample Input

7 4 4
.......
......*
....*O.
**.*.O.
...*...
.OO..*.
.......
0 0 0

Sample Output

2
Note

If they only set one new rock at (4,3), when Liz comes to (2,6) and Lilly comes to (5,6), they can still see each other. Thus, an additional rock at (2,6) must be put. The new map is shown below: ('N' represents a new rock)

.......
.....N*
....*O.
**N*.O.
...*...
.OO..*.
.......

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-02-28 14:35
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)