Making a Digram
Time limit1sMemory limit256 MB
Place one of the seven-square digram shapes on an n by m grid to minimize the cost of adding and erasing cells so the black cells match the shape.
- Level
Medium6 of 10
- Topics
- Brute force, Implementation, Prefix sum, Simulation
- Solved
- No attempts yet
Problem
In 2021, making a digram on the spot became a basic skill for Koreans. We want to draw a digram on the grid in front of us.
A digram is defined as a shape made by joining seven squares. The following are digrams for and .

Examples of shapes that are not digrams are as follows.
Some cells of the grid are already colored black. Painting a white cell black costs , and erasing a black cell to make it white costs . Write a program that finds the minimum cost needed so that the black cells form a digram.
The position and size of the digram are not restricted, but it cannot be flipped or rotated, and it must not go outside the grid. Also, every black cell must be included in the digram, and every cell not included in the digram must be white.
Input
The first line gives the size of the grid, .
The second line gives the costs of changing a cell's color.
The next lines each give a string of length . # is a cell colored black, and . is a white cell.
Output
On the first line, print the minimum cost to make a digram.

