SnowflakeOA #36
Airport Limousine (Maximum Passengers Collected) (Also for Core/Database Intern :)
Estimated HardJudge-ready
Topics (AI-labelled)Grid DP
Go from the top-left to the bottom-right moving only right/down, then return moving only left/up. −1 is a wall, 0 is an empty cell, and 1 is one passenger; once a cell is passed, its passenger is picked up and not counted again. If there is no valid outbound path, return 0. Find the maximum total number of passengers.
Input
The first line contains n, followed by n lines of the matrix. 1≤n≤100, and each cell is −1, 0, or 1. The start and end cells may also hold a passenger or a wall; if either is a wall, there is no valid trip.
Output
Output the single integer required by the problem.
Example 1
Input:
2
0 1
-1 0
Output:
1
Example 2
Input:
3
0 1 -1
1 0 -1
1 1 1
Output:
5
Example 3
Input:
1
1
Output:
1
Example notes
The answers to the three public examples are, in order: 1; 5; 1. The examples were rechecked by independent enumeration, state search, or direct simulation.
Editorial
Loading solution…