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;
}
#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;
}