两道水题

17 篇文章 0 订阅

POJ 1088 滑雪
一开始行列搞反了 WA了一次

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define inf 0x3f3f3f3f
#define maxn 205
int dp[maxn][maxn], dir[2] = {-1, 1};
int map[maxn][maxn];

int max(int a, int b){
    return a > b ? a : b;
}

int solve(int x, int y){
    if(dp[x][y] != -1)  return dp[x][y];
    for(int i = 0; i < 2; i++){
        if(map[x][y] > map[x][y + dir[i]])  
            dp[x][y] = max(dp[x][y], 1 + solve(x, y + dir[i]));
        if(map[x][y] > map[x + dir[i]][y])
            dp[x][y] = max(dp[x][y], 1 + solve(x + dir[i], y));
    }
    if(map[x][y] <= map[x-1][y] && map[x][y] <= map[x+1][y] && 
        map[x][y] <= map[x][y-1] && map[x][y] <= map[x][y+1])
        dp[x][y] = 1;
    return dp[x][y];
}

int main(){
    int R, C;
    memset(map, 0x3f, sizeof(map));
    memset(dp, -1, sizeof(dp));
    scanf("%d %d", &R, &C);
    for(int x = 1; x <= R; x++)
        for(int y = 1; y <= C; y++)
            scanf("%d", &map[x][y]);
    int res = 0;
    for(int x = 1; x <= R; x++)
        for(int y = 1; y <= C; y++)
            res = max(res, solve(x, y));
    printf("%d\n", res);
    return 0;
}

Dance Dance Revolution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define inf 0x3f3f3f3f
#define LEN 10000

int dp[5][5][LEN], steps[LEN];


int min(int a, int b){
    return a > b ? b : a;
}

int cost(int x, int y){
    if(x == y)
        return 1;
    if((x == 0 || y == 0) && (x + y != 0))
        return 2;
    if(abs(x % 4 - y % 4) == 1)
        return 3;
    if(abs(x % 4 - y % 4) == 2)
        return 4;
}

int solve(int len, int *step){
    dp[0][step[1]][1] = cost(0, step[1]);
    dp[step[1]][0][1] = cost(0, step[1]);
    for(int i = 1; i < len; i++){
        for(int x = 0; x <= 4; x++){
            for(int y = 0; y <= 4; y++){
                //move left
                if(step[i+1] != y)
                    dp[step[i+1]][y][i+1] = min(dp[step[i+1]][y][i+1], 
                        cost(x, step[i+1]) + dp[x][y][i]);
                //move right
                if(step[i+1] != x)
                    dp[x][step[i+1]][i+1] = min(dp[x][step[i+1]][i+1], 
                        cost(y, step[i+1]) + dp[x][y][i]);
            }
        }
    }
    int ans = inf;
    for(int i = 0; i < 5; i++){
        ans = min( min(dp[i][step[len]][len], 
                    dp[step[len]][i][len]),
                   ans);
    }
    return ans;
}

int main(){
    int len = 0;
    while(true){
        int i = 1;
        while(true){
            scanf("%d", &steps[i]);
            if(0 == steps[i]){
                len = i - 1;
                break;
            }
            i++;
        }
        if(0 == len)
            break;
        memset(dp, 0x3f, sizeof(dp));
        int ans = solve(len, steps);
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值