Dynamic Planning-maze-Baidu star-1004

Source: Internet
Author: User

Labyrinth

Problem Description

Du du Xiong is a bear who loves adventure. He accidentally falls into a m * n matrix maze, which can only start from the first square in the upper left corner of the matrix, only the first grid in the upper-right corner can be used to walk out of the maze. Each grid can only go up and down to the right to go to the grid that has not been traveled before. Each grid has some gold coins (either positive or negative, there may be a robber in the Road robbery. The gold coins on Dudu Xiong can be negative, and you need to write an overdue bill to the robbers. At the beginning, Dudu Xiong's gold coins were 0, q: How many gold coins does du Xiong have when he walks out of the maze?

Input

The first line of the input is an integer T (T <200), indicating a total of T groups of data. Enter two positive integers m, n (m <= 100, n <= 100) in the first row of each data group ). In the next m row, n integers in each line represent the number of gold coins in the corresponding grid. Each integer is greater than or equal to-100 and less than or equal to 100.

Output

For each group of data, you must first output a separate line "Case #? : ", Where the question mark should be filled with the current number of data groups. The number of groups starts from 1. Each group of test data outputs a row and an integer, which indicates the maximum number of coins you can obtain when you go to the upper right corner Based on the optimal strategy.

Sample Input

2

3 4

1-1 1 0

2-2 4 2

3 5 1-90

2 2

1 1

1 1

Sample Output

Case #1:

18

Case #2:

4

If the maze is large, DFS will inevitably time out. Note that there are only three walking directions: top, bottom, and right. This means that the road that has been traveled cannot be walked any more, and that you do not need to trace back. The question is the maximum value. Everything clearly points to DP!

Time Complexity: O (n * m ).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.