QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#325614#4833. Tetra-puzzletuanlinh1230 4ms4008kbC++205.4kb2024-02-11 17:54:062024-02-11 17:54:06

Judging History

你现在查看的是最新测评结果

  • [2024-02-11 17:54:06]
  • 评测
  • 测评结果:0
  • 用时:4ms
  • 内存:4008kb
  • [2024-02-11 17:54:06]
  • 提交

answer

#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
inline ll IDX(ll i, ll j) {return i*5+j;}
inline bool BIT(ll mask, ll i, ll j) {return (mask&1<<IDX(i, j))!=0;}

struct piece
{
    ll val=0, h=0, w=0;
    piece(){}
    piece(char s)
    {
        vector <vector <bool>> a(5, vector <bool> (5, 0));
        if (s=='I') a[0][0]=a[1][0]=a[2][0]=a[3][0]=1, h=4, w=1;
        if (s=='L') a[0][0]=a[1][0]=a[2][0]=a[2][1]=1, h=3, w=2;
        if (s=='O') a[0][0]=a[0][1]=a[1][0]=a[1][1]=1, h=w=2;
        if (s=='T') a[0][0]=a[0][1]=a[0][2]=a[1][1]=1, h=2, w=3;
        if (s=='Z') a[0][0]=a[0][1]=a[1][1]=a[1][2]=1, h=2, w=3;
        for (ll i=0; i<5; i++)
            for (ll j=0; j<5; j++)
                if (a[i][j]) val|=1<<IDX(i, j);
    }

    inline void fdiag()
    {
        ll res=0;
        for (ll i=0; i<5; i++)
            for (ll j=0; j<5; j++)
                if (BIT(val, i, j))
                    res|=1<<IDX(j, i);
        val=res, swap(h, w);
    }
    inline void fhori()
    {
        ll res=0;
        for (ll i=0; i<5; i++)
            for (ll j=0; j<5; j++)
                if (BIT(val, i, j))
                    res|=1<<IDX(4-i, j);
        val=res;
    }
    inline void rotate()
    {
        fdiag();
        fhori();
    }
    inline void fix()
    {
        while (1)
        {
            bool ok=1;
            for (ll i=0; i<5; i++)
                if (BIT(val, 0, i))
                    {ok=0; break;}
            if (!ok) break;
            val>>=5;
        }
        while (1)
        {
            bool ok=1;
            for (ll i=0; i<5; i++)
                if (BIT(val, i, 0))
                    {ok=0; break;}
            if (!ok) break;
            val>>=1;
        }
    }

    void print()
    {
        cout << h << " " << w << "\n";
        for (ll i=0; i<5; i++)
            for (ll j=0; j<5; j++)
                cout << BIT(val, i, j) << " \n"[j==4];
    }
};

const ll all=(1<<25)-1, inf=1e9, cap=100;
vector <pair<pll, pll>> best[1005];
vector <piece> I, L, O, T, Z;
ll hori[5], veri[5];
vector <ll> t[26];

struct state
{
    ll val=0;
    state(){}
    state(ll val): val(val){}
    ll evaluate(){return __builtin_popcount(val);}

    bool Move(ll move)
    {
        if (val&move) return 0;
        ll ne=val|move, re=0;
        for (ll k=0; k<5; k++)
        {
            if ((ne|hori[k])==ne) 
                re|=hori[k];
            if ((ne|veri[k])==ne) 
                re|=veri[k];
        }
        val=ne^re;
        return 1;
    }

    ll findbest(ll c)
    {
        pll best={inf, 0};
        for (ll move:t[c]) 
        {
            state ne=*this;
            if (!ne.Move(move)) continue;
            best=min(best, mp(ne.evaluate(), move));
        }
        return best.se;
    }

    void act(ll move)
    {
        for (ll i=0; i<5; i++) for (ll j=0; j<5; j++)
        {
            if (BIT(val, i, j)) cout << '#';
            else if (BIT(move, i, j)) cout << '*';
            else cout << '.'; if (j==4) cout << "\n";
        }
        cout << endl, Move(move);
    }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    for (ll i=0; i<5; i++)
        for (ll j=0; j<5; j++)
        {
            hori[i]|=1<<IDX(i, j);
            veri[j]|=1<<IDX(i, j);
        }
    auto init=[&](vector <piece> &a, char s)
    {
        a.resize(8), a[0]=piece(s);
        for (ll i=1; i<8; i++)
        {
            if (i<4) a[i]=a[i-1], a[i].rotate();
            else a[i]=a[i-4], a[i].fhori();
            a[i].fix();
        }
        map <ll, bool> Map;
        for (ll i=0; i<8; i++) for (ll j=0; j<=5-a[i].h; j++) for (ll k=0; k<=5-a[i].w; k++)
        {
            ll val=a[i].val<<IDX(j, k);
            if (!Map[val]) Map[val]=1, t[s-'A'].pb(val);
        }
    }; init(I, 'I'), init(L, 'L'), init(O, 'O'), init(T, 'T'), init(Z, 'Z');
    string type; cin >> type;
    if (type=="prepare")
    {
        ll n; cin >> n;
        best[0].pb({{0, 0}, {-1, -1}});
        for (ll i=1; i<=n; i++)
        {
            string c; cin >> c;
            for (ll id=0; id<best[i-1].size(); id++)
            {
                pair<pll, pll> s=best[i-1][id];
                for (ll j=0; j<2; j++)
                {
                    state cr(s.fi.se);
                    cr.Move(cr.findbest(c[j]-'A'));
                    best[i].pb({{cr.evaluate(), cr.val}, {id, c[j]-'A'}});
                }
            }
            sort(best[i].begin(), best[i].end());
            if (best[i].size()>cap) best[i].resize(cap);
        }
        string ans="";
        for (ll i=n, j=0; best[i][j].se.fi!=-1; j=best[i][j].se.fi, i--)
            ans=char(best[i][j].se.se+'A')+ans;
        cout << ans;
    }
    else
    {
        state cr; ll n; cin >> n;
        for (ll i=0; i<n; i++)
        {
            char c; cin >> c; c-='A';
            pll best={inf, 0};
            for (ll move:t[c]) 
            {
                state ne=cr;
                if (!ne.Move(move)) continue;
                best=min(best, mp(ne.evaluate(), move));
            }
            if (best.fi==inf) assert(0);
            cr.act(best.se);
        }
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3660kb

First Run Input

prepare
6
TO LO ZI LI LT LT

First Run Output

OLILLL

Second Run Input

play
6
O
L
I
L
L
L

Second Run Output

**...
**...
.....
.....
.....

##***
##*..
.....
.....
.....

****.
###..
.....
.....
.....

####*
###.*
...**
.....
.....

.***.
###*#
...##
.....
.....

.###.
*....
***##
.....
.....


result:

ok good job!

Test #2:

score: 100
Accepted
time: 0ms
memory: 3600kb

First Run Input

prepare
1
ZI

First Run Output

I

Second Run Input

play
1
I

Second Run Output

****.
.....
.....
.....
.....


result:

ok good job!

Test #3:

score: 100
Accepted
time: 1ms
memory: 3624kb

First Run Input

prepare
10
LO TZ ZO OI OL ZL ZO TL OZ LO

First Run Output

OZZIOLOLOO

Second Run Input

play
10
O
Z
Z
I
O
L
O
L
O
O

Second Run Output

**...
**...
.....
.....
.....

##.**
##**.
.....
.....
.....

##.##
####*
...**
...*.
.....

##*##
..*..
..*##
..*#.
.....

.....
**#..
**###
..##.
.....

.....
###**
....*
..##*
.....

.....
.....
**..#
**###
.....

..*..
..*..
##**#
.....
.....

**#..
**#..
.....
.....
.....

###**
###**
.....
......

result:

ok good job!

Test #4:

score: 100
Accepted
time: 2ms
memory: 4008kb

First Run Input

prepare
29
ZT OT ZL LT ZI LO IZ LT OI ZT ZT OL ZI LT LZ TZ IL ZI OT LZ LT LZ OT OI IO OZ IZ ZL ZL

First Run Output

TTZTILZLITTOILZZLITLTLOIIZILL

Second Run Input

play
29
T
T
Z
T
I
L
Z
L
I
T
T
O
I
L
Z
Z
L
I
T
L
T
L
O
I
I
Z
I
L
L

Second Run Output

***..
.*...
.....
.....
.....

###..
.#...
.*...
**...
.*...

#.#**
..**.
.....
#....
.....

#*###
**##.
.*...
#....
.....

....*
####*
.#..*
#...*
.....

....#
.....
.#.*#
#***#
.....

.**.#
**...
.#.##
.....
.....

.##.#
##***
.#*##
.....
.....

*##.#
*....
*####
*....
.....

###*#
#.***
.....
#.....

result:

ok good job!

Test #5:

score: 100
Accepted
time: 2ms
memory: 3788kb

First Run Input

prepare
36
TI ZT IT IT OT OZ IL TZ ZO LO LZ ZL OT ZI IL OT OT ZI ZT TZ TI LT LO OL IL OL IZ OZ OL ZL LT IO ZO ZL ZO LT

First Run Output

TZIIOOITZLLZTILTOITTTLLLILIZOLLIOLZT

Second Run Input

play
36
T
Z
I
I
O
O
I
T
Z
L
L
Z
T
I
L
T
O
I
T
T
T
L
L
L
I
L
I
Z
O
L
L
I
O
L
Z
T

Second Run Output

***..
.*...
.....
.....
.....

###**
.#**.
.....
.....
.....

****.
.###.
.....
.....
.....

####*
.###*
....*
....*
.....

.....
.####
**..#
**..#
.....

.....
.####
##**#
##**#
.....

*....
*####
*....
*....
.....

#***.
..*..
#....
#....
.....

####*
..#**
#..*.
#....
.....

.....
**###
#*.#.
#*....

result:

ok good job!

Test #6:

score: 100
Accepted
time: 3ms
memory: 3748kb

First Run Input

prepare
57
LT OI TL IZ OT TL LT TI LO LT LO LO OL IO LI IT TZ TZ IO ZT TI OT OI TL TL ZT TZ OL OI LI OZ ZI TO LZ IO IO LT OZ TO OZ IZ TZ LT ZO TO TZ OT LT ZO OL IL LO ZT OZ OL IL ZI

First Run Output

TILITTLTLLLLOILITTIZITILTTTLIIZITLIOLOTOIZLZOTOLZOILTZLLZ

Second Run Input

play
57
T
I
L
I
T
T
L
T
L
L
L
L
O
I
L
I
T
T
I
Z
I
T
I
L
T
T
T
L
I
I
Z
I
T
L
I
O
L
O
T
O
I
Z
L
Z
O
T
O
L
Z
O
I
L
T
Z
L
L
Z

Second Run Output

***..
.*...
.....
.....
.....

###..
*#...
*....
*....
*....

.##..
.#...
**...
.*...
.*...

..#..
.....
#****
.....
.....

.*#..
***..
.....
.....
.....

.##*.
###**
...*.
.....
.....

*###.
***..
...#.
.....
.....

####*
###**
...#*
.....
.....

.....
*....
***##
.....
.....

.***.
#*...
.....
......

result:

ok good job!

Test #7:

score: 100
Accepted
time: 4ms
memory: 3736kb

First Run Input

prepare
88
ZL OL OT IT LO ZL OI ZT TO OI OL OL LZ ZT TL TZ IO ZL TZ TO IL LO ZT OL LT ZI TZ IL LT ZL OT TI ZO ZO ZT OZ TL TL LO OI TO OZ LI ZO ZT ZT IO IZ OZ ZO ZI ZO IO IO TI OL IL IT IL TL TL ZL IT IT ZO IO OL IL ZI TO OL IL ZI LZ LT IL LI ZO IL TL IT LI ZI OL TO OZ IZ OZ

First Run Output

LOTILZITOIOLLZLTILZOILZOLZTITZTTOOZOLLLIOZLOTZOZOZIZIIILLIILLLIIZILIIOOLILLLLZILILZLOZIO

Second Run Input

play
88
L
O
T
I
L
Z
I
T
O
I
O
L
L
Z
L
T
I
L
Z
O
I
L
Z
O
L
Z
T
I
T
Z
T
T
O
O
Z
O
L
L
L
I
O
Z
L
O
T
Z
O
Z
O
Z
I
Z
I
I
I
L
L
I
I
L
L
L
I
I
Z
I
L
I
I
O
O
L
I
L
L
L
L
Z
I
L
I
L
Z
L
O
Z
I
O

Second Run Output

***..
*....
.....
.....
.....

###**
#..**
.....
.....
.....

.*...
#**##
.*...
.....
.....

.#...
****.
.#...
.....
.....

.#***
####*
.#...
.....
.....

.####
.**..
.#**.
.....
.....

*####
*##..
*###.
*....
.....

....*
###**
####*
#....
.....

**..#
**...
.....
#....
.....

##..#
##...
.....
#**...

result:

ok good job!

Test #8:

score: 0
Stage 2: Program answer Runtime Error

First Run Input

prepare
105
LI LI OI TL OZ LZ IL IZ IL TI IL IO OT LT LO ZO LI LZ OI IL ZT IT LZ IZ IZ LI IT LI IO TI ZL OI IT LO TZ OI IL OL IL OT IL OZ LT IO OL OL LO ZO TI LI LI LI ZO ZI LI OL TI TZ LO LT IL LZ IL OL ZT LT IZ IL TZ IT OZ TL ZI IZ IZ TO IT IO TO TI TO TZ ZT IZ LI IL ZI TI TL TZ TO LI LO OL IL IL ...

First Run Output

LIOTOLLILTLOTLOZILIITILIILIIIILIIOTILOLOIOLOOLOZIIILOIILIZLLILIOTLZIZIOLIIZTTITITZZIILIILTOILLLLZOLLZITTL

Second Run Input

play
105
L
I
O
T
O
L
L
I
L
T
L
O
T
L
O
Z
I
L
I
I
T
I
L
I
I
L
I
I
I
I
L
I
I
O
T
I
L
O
L
O
I
O
L
O
O
L
O
Z
I
I
I
L
O
I
I
L
I
Z
L
L
I
L
I
O
T
L
Z
I
Z
I
O
L
I
I
Z
T
T
I
T
I
T
Z

Second Run Output

***..
*....
.....
.....
.....

###..
#****
.....
.....
.....

###**
...**
.....
.....
.....

.*...
***##
.....
.....
.....

.#**.
..**.
.....
.....
.....

.###.
..##.
.**..
..*..
..*..

.#.#.
...#.
.#**.
...*.
...*.

.#...
****.
.##..
.....
.....

.#***
####*
.##..
.....
.....

*####
**...
*##..
......

result: