hdu 2782 The Worm Turns

6 篇文章 0 订阅
5 篇文章 0 订阅
The Worm Turns

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 910 Accepted Submission(s): 348


Problem Description
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divided into cells, and each cell contains either food or a rock. Winston wanders aimlessly for a while until he gets hungry; then he immediately eats the food in his cell, chooses one of the four directions (north, south, east, or west) and crawls in a straight line for as long as he can see food in the cell in front of him. If he sees a rock directly ahead of him, or sees a cell where he has already eaten the food, or sees an edge of the rectangular patch, he turns left or right and once again travels as far as he can in a straight line, eating food. He never revisits a cell. After some time he reaches a point where he can go no further so Winston stops, burps and takes a nap.

For instance, suppose Winston wakes up in the following patch of earth (X's represent stones, all other cells contain food):





If Winston starts eating in row 0, column 3, he might pursue the following path (numbers represent order of visitation):




In this case, he chose his path very wisely: every piece of food got eaten. Your task is to help Winston determine where he should begin eating so that his path will visit as many food cells as possible.


Input
Input will consist of multiple test cases. Each test case begins with two positive integers, m and n , defining the number of rows and columns of the patch of earth. Rows and columns are numbered starting at 0, as in the figures above. Following these is a non-negative integer r indicating the number of rocks, followed by a list of 2r integers denoting the row and column number of each rock. The last test case is followed by a pair of zeros. This should not be processed. The value m×n will not exceed 625.


Output
For each test case, print the test case number (beginning with 1), followed by four values:

amount row column direction

where amount is the maximum number of pieces of food that Winston is able to eat, (row, column) is the starting location of a path that enables Winston to consume this much food, and direction is one of E, N, S, W, indicating the initial direction in which Winston starts to move along this path. If there is more than one starting location, choose the one that is lexicographically least in terms of row and column numbers. If there are optimal paths with the same starting location and different starting directions, choose the first valid one in the list E, N, S, W. Assume there is always at least one piece of food adjacent to Winston's initial position.


Sample Input
5 5
3
0 4 3 1 3 2
0 0


Sample Output
Case 1: 22 0 3 W


Source
2008 East Central Regional Contest


Recommend

lcy


题意:有一个矩形区域,区域内有的格子是墙,剩余的格子是吃的,人在区域内走动,除非碰到墙,边界或是已经走过的格子,否则一定会一直向前走,不会转弯(这一点很重要,我一开始没有意识到),求人在区域内能够走动的最大步数,输出对应的最大步数,起始点,起始点一开始走的方向(当有相同的点可以达到最大步数时,输出行列数最小的(这就要求从第一个节点开始搜);当从同一个点出发,向多个(>2)方向都能达到相同的最大步数时,按照E,N,S,W的顺序,输出第一个)。


暴力深搜:注意:
1.对于第一个点枚举四个方向,并且后来dfs中不再重复枚举
2.当没有碰到障碍的时候,一直向前走,不要四个方向搜
3.每次搜完一个点再搜下一个点的时候,不用再memset()标记数组了,因为每次dfs()返回到第一层,会将标记数组的所有的值取消标记;所以不必再加,而且加上memset()会超时,这个地方困扰了我很久。。。并且因此还查找了memset()的具体实现方法(跟for循环数组赋初值时间差不多)

对memset不了解可以看看:http://blog.csdn.net/dan15188387481/article/details/49621447

http://blog.sina.com.cn/s/blog_6f611c300101e59l.html

代码:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值