P1002 过河卒

过河卒

首先初始化地图,去掉不能走的点;
由于卒的走的方向只有向右或向下一步,因此对于点(x,y),到达该点的路径数为f[x][y] = f[x-1][y] + f[x][y-1]

#include
#include
#include

long long map[22][22];
void init(int x,int y){
    if (x>=0&&y>=0) map[x][y] = -1;
}

int main(){
    memset(map,0,sizeof(map));

    int a,b;
    scanf("%d%d",&a,&b);
    int ha,hb;
    scanf("%d%d",&ha,&hb);
    init(ha,hb);
    init(ha+2,hb+1);init(ha+2,hb-1);init(ha+1,hb+2);init(ha+1,hb-2);
    init(ha-2,hb+1);init(ha-2,hb-1);init(ha-1,hb+2);init(ha-1,hb-2);
    map[0][0] = 1;
    for (int i = 0;i <= a;i++){
        for (int j = 0;j <= b; j++){
            if (i == 0&&j==0) continue;
            if (map[i][j] == -1){
                map[i][j] = 0;
            }else{
                if (i-1<0)  map[i][j] = map[i][j-1];
                else if (j-1<0) map[i][j] = map[i-1][j];
                else map[i][j] = map[i-1][j] + map[i][j-1];
            }
    //  std::cout<
        }    
    //std::cout<
    }
    std::cout<<map[a][b];
    return 0;
}

你可能感兴趣的:(NOIP,【算法】,洛谷,动态规划)