周测3 T1 无碳小车

这次是德龙出的题,好好体会与真神之间态度、思维的差距。

//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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值