Tofu Seller Jang Hongjun 3
Time limit1sMemory limit512 MB
Given an N by M grid of letter grades, pick disjoint horizontal or vertical dominoes to maximize the sum of pairwise prices from the price table.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Matrix
- Solved
- No attempts yet
Problem
Jang Hongjun is a rather unusual tofu seller. He has a tofu board with height and width , and he cuts it into pieces to sell. Each cell of the board has its own grade, and the price of a piece depends on the grades of the two cells it contains. The price table is:
For example, a piece made of an A cell and a C cell costs 7, and a piece made of a D cell and a B cell costs 3. The table is symmetric, so the order of the two cells does not affect the price.
Every cell of the board has one of the grades A, B, C, D, or F. A piece consists of two horizontally adjacent cells or two vertically adjacent cells, and each cell can belong to at most one piece. Hongjun wants to cut the board so that the total price of the cut pieces is as large as possible. Single cells left over after cutting out the pieces are worth 0, so he throws them away.
Write a program that helps Hongjun find the maximum total price of the pieces he cuts out.
Input
The first line contains the height and the width of the tofu board. and are integers between 1 and 200, inclusive.
Each of the next lines contains characters with no spaces. Each character is the grade of the tofu in that cell and is one of A, B, C, D, and F.
Output
Print the maximum total price of the cut pieces on the first line.