LeetCode 675. Cut Off Trees for Golf Event

This problem can be simplified to an n shortest distance problem.

We start from arr[0][0], looking for the next larger element .... until we reach the last one. This requires sorting and queue and traverse all element, we can take use of priority queue.

1. put all elements into a priority queue q, put smaller into the front.

2.from arr[0][0], to q.poll(), we use bfs to calculate the shortest distance.

3. until we reach the last element, return the total amount of all distances.


bfs:

1. Initialize a seen array which fills in false. 

2. add an element in to queue,.

3. poll an element interatively, if it's the aim value, return the value's path length. 

4. move the element in array 1 unit to get a new element. Eachtime we check the boundaries, whether it's a wall and whether it has been reached before.

4. If pass all conditions, add the new element into the queue

5. set the seen arr of the new element to true, representing checked

6. until there's no new element, return -1.


Code: 


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容