这次是德龙出的题,好好体会与真神之间态度、思维的差距。
//map[i][j][x]表示此状态最小改变方格数。仔细体会这种预处理方块摆放情况的dp,别老想着傻不愣登地暴力,限制条件很多时想dp。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1010;
int n, m;
int map[maxn][maxn][2], haoyu[maxn][maxn];//0右1下
bool check(int x, int y){
if(x > 0 && x <= n && y > 0 && y <= m && haoyu[x][y])
return true;
return false;
}
int main(){
memset(map, 0x3f3f3f3f, sizeof(map));
cin >> n >> m;
char ch;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++){
cin >> ch;
if(ch == '.') haoyu[i][j] = 1;
else if(ch == 'b') haoyu[i][j] = 0;
}
map[1][1][0] = 0;
map[1][1][1] = 1;
int now, ur, ld, d, r;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++){
now = check(i, j)? 0 : 1;
ur = check(i-1, j+1)? 1 : 0;
ld = check(i+1, j-1)? 1 : 0;
d = check(i+1, j)? 1 : 0;
r = check(i, j+1)? 1 : 0;
//right
map[i][j][0] = min(map[i][j][0], map[i-1][j][0] + now + ur + d);
map[i][j][0] = min(map[i][j][0], map[i][j-1][0] + now);
map[i][j][0] = min(map[i][j][0], map[i-1][j][1] + now + d);
map[i][j][0] = min(map[i][j][0], map[i][j-1][1] + now + ld);
//down
map[i][j][1] = min(map[i][j][1], map[i-1][j][1] + now);
map[i][j][1] = min(map[i][j][1], map[i][j-1][1] + now + ld + r);
map[i][j][1] = min(map[i][j][1], map[i-1][j][0] + now + ur);
map[i][j][1] = min(map[i][j][1], map[i][j-1][0] + now + r);
}
cout << min(map[n][m][1], map[n][m][0]) << endl;
return 0;
}