Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include<iostream>#include<algorithm>using namespace std;int main(){int const MAX = 1e2 + 5;int n, m;int dp[MAX][MAX][2]; //0 -> 右, 1 -> 下char s[MAX][MAX];scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++)scanf("%s", s[i]+1);for (int i = 0; i <= n; i++)for (int j = 0; j <= m; j++)dp[i][j][0] = dp[i][j][1] = 0x3fffffff;dp[1][1][0] = 0;for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++){if (i == 1 && j == 1)continue;if (s[i - 1][j + 1] == 'b' || j == m)dp[i][j][1] = min(dp[i-1][j][1]+(s[i][j]=='b'), dp[i - 1][j][0]+ (s[i][j] == 'b'));elsedp[i][j][1] = min(dp[i-1][j][1]+ (s[i][j] == 'b'), dp[i - 1][j][0] + 1+ (s[i][j] == 'b'));if (s[i + 1][j - 1] == 'b' || i == n)dp[i][j][0] = min(dp[i][j-1][0]+ (s[i][j] == 'b'), dp[i][j - 1][1]+ (s[i][j] == 'b'));elsedp[i][j][0] = min(dp[i][j-1][0]+ (s[i][j] == 'b'), dp[i][j - 1][1] + 1+ (s[i][j] == 'b'));}}