校内测试-打怪兽

题目:

主要算法 :  记忆化搜索

题干:

  最长不下降链

应试策略:  

  想到记忆化搜索,但是伪记忆化搜索,放弃了

  打爆搜,卡时间

代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LL long long 
#define FORa(i,s,e) for(LL i=s;i<=e;i++)
#define FORs(i,s,e) for(LL i=s;i>=e;i--)
#define File(name) freopen(name".in","r",stdin);freopen(name".out","w",stdout);
#define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,100000,stdin),stdin)?EOF:*pa++

using namespace std;
inline LL read();
char buf[100000],*pa,*pb;

const LL N=510,M=510;
LL n,m,ans;
LL t,a[N+1][M+1];
bool vis[N+1][M+1];
inline LL max(LL fa,LL fb) {return fa>fb?fa:fb;}
LL mx[4]={0,0,1,-1},my[4]={1,-1,0,0};

void Dfs(LL x,LL y,LL cnt)
{
    ans=max(ans,cnt+1);
    LL fx,fy;
    FORa(i,0,3)
    {
        fx=x+mx[i],fy=y+my[i];
        if(a[fx][fy]<=a[x][y]||fx>n||fx<1||fy>m||fy<1) continue;
        vis[fx][fy]=1,Dfs(fx,fy,cnt+1),vis[fx][fy]=0,t++;
    }
}
int main()
{
    File("Llll");
    n=read(),m=read();
    FORa(i,1,n) FORa(j,1,m) a[i][j]=read();
    t=m*n;
    FORs(i,n,1)
        FORs(j,m,1)
        {
            memset(vis,0,sizeof(vis)),vis[i][j]=1,Dfs(i,j,0);
            if(t>2000000)
            {
                printf("%lld",ans);
                return 0;
            } 
        }
    printf("%lld",ans);
    return 0;
}
inline LL read()
{
    register LL x(0),f(1);register char c(gc);
    while(c<'0'||c>'9') f=c=='-'?-1:1,c=gc;
    while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=gc;
    return x*f;
}

非完美算法:  

  记忆化搜索

 正解:

  记忆化搜索

#include<stdio.h>
#include<stdlib.h>
#define LL long long 
#define FORa(i,s,e) for(LL i=s;i<=e;i++)
#define FORs(i,s,e) for(LL i=s;i>=e;i--)
#define File(name) freopen(name".in","r",stdin);freopen(name".out","w",stdout);
#define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,100000,stdin),stdin)?EOF:*pa++

using namespace std;
inline LL read();
char buf[100000],*pa,*pb;

const LL N=510,M=510;
LL n,m,ans;
LL a[N+1][M+1],f[N+1][M+1];
inline LL max(LL fa,LL fb) {return fa>fb?fa:fb;}
LL mx[4]={0,0,1,-1},my[4]={1,-1,0,0};

void Dfs(LL x,LL y,LL cnt)
{
    if(cnt>f[x][y]) f[x][y]=cnt,ans=max(cnt,ans);
    else return;
    LL fx,fy;
    FORa(i,0,3)
    {
        fx=x+mx[i],fy=y+my[i];
        if(a[fx][fy]<=a[x][y]||fx>n||fx<1||fy>m||fy<1) continue;
        Dfs(fx,fy,cnt+1);
    }
}
int main()
{
    File("Llll");
    n=read(),m=read();
    FORa(i,1,n) FORa(j,1,m) a[i][j]=read();
    FORa(i,1,n)
        FORa(j,1,m) 
            Dfs(i,j,1);
    printf("%lld",ans);
    return 0;
}
inline LL read()
{
    register LL x(0),f(1);register char c(gc);
    while(c<'0'||c>'9') f=c=='-'?-1:1,c=gc;
    while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=gc;
    return x*f;
}

总结:

  证明

猜你喜欢

转载自www.cnblogs.com/SeanOcean/p/11309994.html
今日推荐