LuoguP2254 [NOI2005]瑰丽华尔兹 (单调队列优化DP)(用记忆化过了。。。)

记忆化

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

//#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n") 
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;

#endif
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

template<typename ATP>inline ATP max(ATP &a, ATP &b){
    return a > b ? a : b;
}

const int N = 207;

int n, m, timeLine;
int dx[] = {0, -1, 1, 0, 0};
int dy[] = {0, 0, 0, -1, 1};

char mp[N][N];
int l[N], r[N], dir[N];
int f[N][N][N];

inline int DFS(int tim, int x, int y){
    if(tim > timeLine) return 0;
    if(f[tim][x][y] != -1) return f[tim][x][y];
    int len = r[tim] - l[tim] + 1;
    int fx = x, fy = y;
    int sum = DFS(tim + 1, x, y);
    R(i,1,len){
        fx += dx[dir[tim]], fy += dy[dir[tim]];
        if(fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == 'x') break;
        sum = max(sum, i + DFS(tim + 1, fx, fy));
    }
    return f[tim][x][y] = sum;
}

int main(){
FileOpen();
    Fill(f, -1);
    int X, Y;
    io >> n >> m >> X >> Y >> timeLine;
    R(i,1,n){
        R(j,1,m){
            char ch;
            for(ch = getchar(); ch != '.' && ch != 'x'; ch = getchar());
            mp[i][j] = ch;
        }
    }
    R(i,1,timeLine){
        io >> l[i] >> r[i] >> dir[i];
    }
    
    printf("%d", DFS(1, X, Y));
    
    return 0;
}

WA的暴力

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n----------\n") 
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)

#else

#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;

#endif
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;

const int N = 207;

int f[N][N][2];
char mp[N][N];

int Dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int main(){
//FileOpen();

    int n, m, X, Y, K;
    io >> n >> m >> X >> Y >> K;
    R(i,1,n){
        R(j,1,m){
            char ch;
            for(ch = getchar(); ch != '.' && ch != 'x'; ch = getchar());
            mp[i][j] = ch;
        }
    }
    
    int l, r, dir;
    int tot = 0;
    R(i,1,n){
        R(j,1,m){
            f[i][j][0] = -2147483647;
        }
    }
    f[X][Y][0] = 0;
    R(timeLine,1,K){
        io >> l >> r >> dir;
        R(t, l, r){
            ++tot;
            R(i,1,n){
                R(j,1,m){
                    if(mp[i][j] == 'x') continue;
                    int fx = i + Dir[dir - 1][0], fy = j + Dir[dir - 1][1];
                    if(fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == 'x') continue;
                    f[fx][fy][tot & 1] = Max(f[fx][fy][tot & 1], f[i][j][!(tot & 1)] + 1);
                }
            }
        }
    }
    
    int ans = 0;
    R(i,1,n){
        R(j,1,m){
            ans = Max(ans, f[i][j][tot & 1]);
        }
    }
    
    printf("%d", ans);
    
    return 0;
}
/*
10 10 5 8 5
..........
......xxxx
.....xxxxx
.....xxxxx
..........
xxxx......
..........
..........
..........
..x.......
1 5 3
6 7 1
8 11 2
12 15 3
16 17 2

the right ans is : 15
but mine is : 17
*/

转载于:https://www.cnblogs.com/bingoyes/p/11421370.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值