跳到正文

题解

题解:P12722 [KOI 2021 Round 2] 计算机器人

ZBH2015约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年11月18日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 7 分钟
题目
P12722

版本

  1. v1当前

这是一道 DP 的练手题。

注:di,jd_{i,j} 表示 (i,j)(i,j) 处机器人的权值,Si,jS_{i,j} 表示其存储值,Oi,jO_{i,j} 表示其输出值。

根据题意列方程可得:

Oi,j=max⁡(Oi,j−1, Oi−1,j−1, Oi+1,j−1)+di,jO_{i,j} = \max(O_{i,j-1},\ O_{i-1,j-1},\ O_{i+1,j-1}) + d_{i,j}

时间复杂度:O(nm)O(nm),空间复杂度:O(nm)O(nm)。

最后放个代码:

C++
//#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=2e3+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
int n,m;
string k;
int d[N][N],stay[N][N],ot[N][N];
const int inf=1e9+7;
inline void pre(){
    
}
inline void input(){
    n=read(),m=read();
    F(i,1,n,1){
        k=rs();
        F(j,0,m-1,1) d[i][j+1]=(k[j]-'0');
    }//输入
    F(i,1,n,1){
        F(j,1,m,1){
            stay[i][j]=-inf;
            ot[i][j]=-inf;
        }
    }
}
inline void output(){
    F(j,1,m,1){//列先
        F(i,1,n,1){
            if(j==1) stay[i][j]=0,ot[i][j]=d[i][j];
            else{
                int now=-inf;
                now=(now<ot[i][j-1]?ot[i][j-1]:now);
                if(i>1) now=(now<ot[i-1][j-1]?ot[i-1][j-1]:now);
                if(i<n) now=(now<ot[i+1][j-1]?ot[i+1][j-1]:now);
                stay[i][j]=now;
                ot[i][j]=now+d[i][j];
            }
        }
    }
    int ans=0;
    F(i,1,n,1){
        F(j,1,m,1) ans=(ans<stay[i][j]?stay[i][j]:ans);
    }
    write(ans);
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    //ios::sync_with_stdio(0);
    //cin.tie(0);cin.tie(0);
    int _=1;//_=read();
    while(_--) solve();
    flush();
    return 0;
}
原文