D. Minimum path /没交过

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=4000+10;
char a[maxn][maxn];
bool tf[maxn][maxn];
int32_t main()
{
    int n,x; scanf("%lld %lld",&n,&x);
    for(int i=0;i<n;i++)
        scanf("%s",a[i]);
    for(int i=0;i<n;i++)
    {
        for(int j=n;j<=2*n;j++) a[i][j]='z';
    }
    for(int i=n;i<=2*n;i++)
    {
        for(int j=0;j<=2*n;j++) a[i][j]='z';
    }
    /*for(int i=0;i<=2*n;i++)
    {
        for(int j=0;j<=2*n;j++)
            cout<<a[i][j];
        cout<<endl;
    }*/
    tf[0][0]=1;
    for(int k=0;k<=2*n-2;k++)
    {
        //cout<<"---"<<k<<x<<endl;
        int t=0;
        for(int i=0;i<=k;i++)
        {
            if(a[i][k-i]=='a' && tf[i][k-i]==1 ) t=1;
        }
        if(t==1)
        {
            for(int i=0;i<=k;i++)
            {
                if(a[i][k-i]=='a' && tf[i][k-i]==1 )
                {
                    if(k-i+1<n) tf[i][k-i+1]=1;
                    if(i+1<n)   tf[i+1][k-i]=1;

                    //cout<<a[i][k-i]<<endl;
                }
            }
            cout<<"a";
        }
        else if(t!=1 && x)
        {
            for(int i=0;i<=k;i++)
            {
                if(tf[i][k-i]==1 )
                {
                    if(k-i+1<n) tf[i][k-i+1]=1;
                    if(i+1<n) tf[i+1][k-i]=1;

                    //cout<<a[i][k-i]<<endl;
                }
            }cout<<"a"; x--;
        }
        else if(t!=1&&x==0)
        {
            char c='z';
            for(int i=0;i<=k;i++)
            {
                if( tf[i][k-i]==1 )
                {
                    if(a[i][k-i]<c) { c=a[i][k-i]; }
                    //cout<<a[i][k-i]<<endl;
                }
            }
            for(int i=0;i<=k;i++)
            {
                if(tf[i][k-i]==1 && a[i][k-i]==c)
                {
                    if(k-i+1<n) tf[i][k-i+1]=1;
                    if(i+1<n)   tf[i+1][k-i]=1;
                }
            }
            cout<<c;
        }
        //cout<<x<<"()"<<"t="<<t<<endl;

    }
}

猜你喜欢

转载自www.cnblogs.com/Andromeda-Galaxy/p/9826122.html