CF1592F2.Alice and Recoloring 2
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The difference between the versions is in the costs of operations. Solution for one version won't work for another!
Alice has a grid of size n×m , initially all its cells are colored white. The cell on the intersection of i -th row and j -th column is denoted as (i,j) . Alice can do the following operations with this grid:
-
Choose any subrectangle containing cell (1,1) , and flip the colors of all its cells. (Flipping means changing its color from white to black or from black to white).
This operation costs 1 coin.
-
Choose any subrectangle containing cell (n,1) , and flip the colors of all its cells.
This operation costs 3 coins.
-
Choose any subrectangle containing cell (1,m) , and flip the colors of all its cells.
This operation costs 4 coins.
-
Choose any subrectangle containing cell (n,m) , and flip the colors of all its cells.
This operation costs 2 coins.
As a reminder, subrectangle is a set of all cells (x,y) with x1≤x≤x2 , y1≤y≤y2 for some 1≤x1≤x2≤n , 1≤y1≤y2≤m .
Alice wants to obtain her favorite coloring with these operations. What's the smallest number of coins that she would have to spend? It can be shown that it's always possible to transform the initial grid into any other.
输入格式
The first line of the input contains 2 integers n,m ( 1≤n,m≤500 ) — the dimensions of the grid.
The i -th of the next n lines contains a string si of length m , consisting of letters W and B. The j -th character of string si is W if the cell (i,j) is colored white in the favorite coloring of Alice, and B if it's colored black.
输出格式
Output the smallest number of coins Alice would have to spend to achieve her favorite coloring.
输入输出样例
输入#1
3 3 WWW WBB WBB
输出#1
2
输入#2
10 15 WWWBBBWBBBBBWWW BBBBWWWBBWWWBBB BBBWWBWBBBWWWBB BBWBWBBWWWBBWBW BBBBWWWBBBWWWBB BWBBWWBBBBBBWWW WBWWBBBBWWBBBWW WWBWWWWBBWWBWWW BWBWWBWWWWWWBWB BBBWBWBWBBBWWBW
输出#2
68
说明/提示
In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells (2,2),(2,3),(3,2),(3,3) . This would cost 2 coins.