Thứ Năm, 5 tháng 5, 2022

TREASURE C++

#include<bits/stdc++.h>
using namespace std;
int a[10][10],cha[10][10];
int n=9,k;
int x[10],y[10];
int h[5]={0,-1,0,1,0};
int c[5]={0,0,1,0,-1};
string s,maxx;

void tao_xau()
{
    s="";
    for(int i=1;i<=n;i++)
        s=s+char(a[x[i]][y[i]]+48);

}
void vet(int i)
{
    //cout<<x[i-1]<<" "<<y[i-1]<<endl;
    int u,v;
    for(int j=1;j<=4;j++)
    {
        u=x[i-1]+h[j];
        v=y[i-1]+c[j];
        if(cha[u][v]==0&&1<=u&&u<=3&&1<=v&&v<=3)
        {
            x[i]=u;y[i]=v;
            cha[u][v]=j;
            if(i==n)
            {
                tao_xau();
              //  cout<<s<<endl;
                if(s>maxx) maxx=s;
            }
            else vet(i+1);
            cha[u][v]=0;
        }
    }
}
void giai()
{
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++) cha[i][j]=0;
    maxx="";
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++)
        {
            x[1]=i;y[1]=j;
            cha[i][j]=10;
            vet(2);
            cha[i][j]=0;
        }
    cout<<maxx<<endl;
}
void nhap()
{
    for(int i=1;i<=3;i++)
        for(int j=1;j<=3;j++) cin>>a[i][j];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    freopen("treasure.inp","r",stdin);
    freopen("treasure.out","w",stdout);
    int t;
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        nhap();
        giai();
    }
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.