QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#323186#4833. Tetra-puzzlehotboy27030 114ms3900kbC++148.4kb2024-02-08 19:51:412024-02-08 19:51:42

Judging History

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

  • [2024-02-08 19:51:42]
  • 评测
  • 测评结果:0
  • 用时:114ms
  • 内存:3900kb
  • [2024-02-08 19:51:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define MASK(i) (1LL<<(i))
#define BIT(mask,i) (((mask) >> (i))&1LL)
string best_move = "TLZOI";
const ll INF = 1e9;
pll piece[][4]={
{{0,0},{1,0},{-1,0},{0,-1}},
{{0,0},{0,1},{0,2},{1,0}},
{{0,0},{0,1},{-1,1},{1,0}},
{{0,0},{0,1},{1,0},{1,1}},
{{0,0},{0,1},{0,2},{0,3}},
};
ll penalty[5][5];
void cv_to_board(ll state,ll board[5][5]){

    ll ptr = 0;
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++,ptr++){
            board[i][j] = BIT(state,ptr);
        }
    }
}
ll to_state(ll board[5][5]){
    ll res = 0;
    ll ptr = 0;
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++,ptr++){
            res ^= board[i][j]*MASK(ptr);
        }
    }
    return res;
}
ll mx[] = {0,0,1,-1};
ll my[] = {-1,1,0,0};
ll evaluate(ll state){
    if (state<0)return INF;
    ll board[5][5];
    ll res = 0;
    cv_to_board(state,board);
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++){
            res += penalty[i][j] * board[i][j];
        }
    }

    auto valid = [&](pll pnt){
        if (pnt.fi >= 0 && pnt.fi<5 && pnt.se >= 0 && pnt.se<5 && board[pnt.fi][pnt.se]==0)return 1;
        else return 0;
    };
    ll holes = 1;
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++){
            if (board[i][j]==0){
                holes++;
                queue <pll> q;
                q.push({i,j});
                board[i][j] = 1;
                while (!q.empty()){
                    pll u = q.front();
                    q.pop();
                    for (ll k = 0;k < 4;k ++){
                        pll nw = {u.fi+mx[k],u.se+my[k]};
                        if (valid(nw)){
                            q.push(nw);
                            board[nw.fi][nw.se] = 1;
                        }
                    }
                }
            }
        }
    }
    return res*holes;
}
void print_move(ll cur_state,ll nxt_state){
    ll ptr = 0;
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++,ptr++){
            if (BIT(nxt_state,ptr)){
                if (BIT(cur_state,ptr)){
                    cout<<'#';
                }
                else{
                    cout<<'*';
                }
            }
            else{
                cout<<'.';
            }
        }
        cout<<endl;
    }
}
void print_state(ll state){
    ll board[5][5];
    cv_to_board(state,board);
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++){
            cout<<board[i][j];
        }
        cout<<endl;
    }
}
ll f_clear(ll state){
    ll board[5][5];
    cv_to_board(state,board);
    ll row[5]={},col[5]={};
    for (ll i = 0;i < 5;i ++)col[i] = row[i] = 1;
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++){
            col[j] &= board[i][j];
            row[i] &= board[i][j];
        }
    }
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++){
            if (col[j] || row[i])board[i][j] = 0;
        }
    }
    return to_state(board);
}
vector <pll> all_move(ll state,ll type){
    ll board[5][5];
    vector <pll> ans_move;
    cv_to_board(state,board);
    auto valid = [&](pll pnt){
        if (pnt.fi >= 0 && pnt.fi<5 && pnt.se >= 0 && pnt.se<5 && board[pnt.fi][pnt.se]==0)return 1;
        else return 0;
    };
    for (ll flip = 0;flip <= 1;flip++){
        for (ll rot = 0;rot < 4;rot++){
            vector <pll> cur_piece;
            for (ll i = 0;i < 4;i ++){
                pll cur = piece[type][i];
                if (flip){
                    cur.fi = -cur.fi;
                }
                for (ll j = 0;j < rot;j ++){
                    ll x = cur.fi,y = cur.se;
                    cur.fi = y;
                    cur.se = -x;
                }
                cur_piece.push_back(cur);
            }
            for (ll i = 0;i < 5;i ++){
                for (ll j = 0;j < 5;j ++){
                    bool ok = 1;
                    for (ll k = 0;k < 4;k ++){
                        pll cur = cur_piece[k];
                        ok &= valid({cur.fi+i,cur.se+j});
                    }
                    if (ok){
                        for (ll k = 0;k < 4;k ++){
                            pll cur = cur_piece[k];
                            board[cur.fi+i][cur.se+j] = 1;
                        }
                        ll nxt_state = to_state(board);
                        ll end_state = f_clear(nxt_state);
                        ans_move.push_back({nxt_state,end_state});
                        for (ll k = 0;k < 4;k ++){
                            pll cur = cur_piece[k];
                            board[cur.fi+i][cur.se+j] = 0;
                        }
                    }
                }
            }
        }
    }
    return ans_move;
}
pll greedy(ll cur_state,ll type){
    vector <pll> choices = all_move(cur_state,type);
    ll ans_eval = INF;
    pll ans = {-1,-1};
    for (auto x:choices){
        ll val = evaluate(x.se);
        if (val < ans_eval){
            ans = x;
            ans_eval = val;
        }
    }
    return ans;
}
ll DEPTH = 3;
ll plan_ahead(ll cur_state,string coming){
    for (auto x:coming){
        ll type = 0;
        for (ll k = 0;k < sz(best_move);k ++){
            if (best_move[k] == x){type = k;break;}
        }
        cur_state = greedy(cur_state,type).se;
        if (evaluate(cur_state)==INF){
            return cur_state;
        }
    }
    return cur_state;
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
    string task;
    cin>>task;
    ll ptr = 0;
    for (ll i = 0;i < 5;i ++){
        for (ll j = 0;j < 5;j ++,ptr++){
            ll dist_i = min(abs(i-0),abs(i-4));
            ll dist_j = min(abs(j-0),abs(j-4));
            penalty[i][j] = MASK(dist_i+dist_j);
//            cout<<penalty[i][j]<<' ';
        }
//        cout<<endl;
    }
    if (task == "prepare"){
        ll n;
        cin>>n;
        ll cur_state = 0;
        vector <string> s(n);
        for (auto &x:s)cin>>x;
        for (ll i = 0;i < n;i ++){
            string cur;
            ll best_eval[2];
            best_eval[0] = best_eval[1] = INF;
            function<void(int)> backtrack = [&](int t){
                if(t==n || t == i+DEPTH){
                    ll cur_eval = evaluate(plan_ahead(cur_state,cur));
                    for (ll j = 0;j < 2;j ++)if (cur[0]==s[i][j])best_eval[j] = min(best_eval[j],cur_eval);
                    return;
                }
                for (ll j = 0;j < 2;j ++){
                    cur.push_back(s[t][j]);
                    backtrack(t+1);
                    cur.pop_back();
                }
            };
            backtrack(i);
            ll type[2];
            for (ll j = 0;j < 2;j ++){
                for (ll k = 0;k < sz(best_move);k ++){
                    if (best_move[k] == s[i][j]){type[j] = k;break;}
                }
            }
            pll ans0 = greedy(cur_state,type[0]);
            pll ans1 = greedy(cur_state,type[1]);
//            cout<<"PENDING "<<s[i]<<endl;
            char choose = s[i][0];
            cur_state = ans0.se;
//            cout<<"EVAL "<<best_eval[0]<<' '<<best_eval[1]<<'\n';
            if (best_eval[0]>best_eval[1]){choose = s[i][1];cur_state=ans1.se;}
            if (evaluate(cur_state)==INF)assert(0);
//            cout<<evaluate(cur_state)<<endl;
//            print_state(cur_state);
            cout<<choose;
        }
    }
    else{
        ll n;
        cin>>n;
        ll cur_state = 0;
        while (n--){
            char x;
            cin>>x;
            ll type;
            for (ll i = 0;i < 5;i ++){
                if (x==best_move[i]){
                    type = i;
                    break;
                }
            }
            pll ans=greedy(cur_state,type);
            print_move(cur_state,ans.fi);
            cur_state = ans.se;
//            print_state(cur_state);
        }
    }
}
// 7 10 3 9 8 9 10 11
/*
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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

First Run Input

prepare
6
TO LO ZI LI LT LT

First Run Output

TLZITL

Second Run Input

play
6
T
L
Z
I
T
L

Second Run Output

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

result:

ok good job!

Test #2:

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

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: 4ms
memory: 3660kb

First Run Input

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

First Run Output

OTOILLOLOL

Second Run Input

play
10
O
T
O
I
L
L
O
L
O
L

Second Run Output

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

result:

ok good job!

Test #4:

score: 100
Accepted
time: 9ms
memory: 3900kb

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

TTZLILILOZTLZLZTIITLLZTIIZILL

Second Run Input

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

Second Run Output

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

result:

ok good job!

Test #5:

score: 100
Accepted
time: 10ms
memory: 3604kb

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

TTIIOOLTZOLZOILOTITTTTLOLLIOLLTIZLZL

Second Run Input

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

Second Run Output

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

result:

ok good job!

Test #6:

score: 100
Accepted
time: 21ms
memory: 3680kb

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

TITIOLLILLOOLOIITTOTITILTZTOILZITZIILOTZITLZOTOTOLILZOLLI

Second Run Input

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

Second Run Output

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

result:

ok good job!

Test #7:

score: 100
Accepted
time: 38ms
memory: 3868kb

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

LOOIOLIZTILOZTTTOLTTIOTLTIZITLOIOOTOTTLIOOLZTTOZOOIZIOIOLILLTZIIZILIIOOLIZLILZILTIIOTZIZ

Second Run Input

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

Second Run Output

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

result:

ok good job!

Test #8:

score: 100
Accepted
time: 41ms
memory: 3672kb

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

LLILOLIIITIIOLLZILOITTLIIIIIOILIIOTOILITIZTIOOLOILIIZIILIZOTILIOZLIIZTOTIZITIOOIOTTZIIZTLTOIOOLLTOTZZIIIO

Second Run Input

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

Second Run Output

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

result:

ok good job!

Test #9:

score: 100
Accepted
time: 75ms
memory: 3620kb

First Run Input

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

First Run Output

TLOTOOOITOILZTTZIILTZIOTLIZTIZLLIOILLLZTOIIOILITZOIZLITIILIOZITIOOIOTIOIZOZLTILLLZLLZTLOLOTZLZOOOLIITIOZLOZILTILZIIOLOTITZTIIIITZZLLZLIIZITOTLLZLILZZILZZTLIILOZIZLTLIZOZIZOLLOOLZTOLTOIZLLZITIIOZLLLZLLTLZL

Second Run Input

play
204
T
L
O
T
O
O
O
I
T
O
I
L
Z
T
T
Z
I
I
L
T
Z
I
O
T
L
I
Z
T
I
Z
L
L
I
O
I
L
L
L
Z
T
O
I
I
O
I
L
I
T
Z
O
I
Z
L
I
T
I
I
L
I
O
Z
I
T
I
O
O
I
O
T
I
O
I
Z
O
Z
L
T
I
L
L
L
Z
L
L
Z
T
L
O
L
O
T
Z
L
Z
O
O
O
L
I
I
T
I
O
Z
L
O
Z
I
L
T
I
L
Z
I
I
O
L
O
T
I
T
Z
T
I
I
I
I
T
Z
Z
L
L
Z
L
I
I
Z
I
T
O
T
L
L
Z
L
I...

Second Run Output

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

result:

ok good job!

Test #10:

score: 100
Accepted
time: 114ms
memory: 3620kb

First Run Input

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

First Run Output

TTIZLILLITTZLOZLIZIILZIOTILTLLILZITZTOLLILIOTLIIOLITOTOTIIITLLTTILIZLZOLLTLIOITIIOLIOOTOIIOZIIIILLILTLIZLLTILIOLZTZLLOIOTILZILZTOTIZOIILILOLOOIITLLLLIZILZTITLIOLTOTZLTZZLLIILLLTTILOILIIIIIOTOLZTOLLILLTITLLTTTOLLLOLOOOTLIZLIOTOOITTOITZLOLTIIOOLIOLOITLTTTZLOLLOZIIIILTOLLIOLOZLLOIZILOILTLOOIIOTIZTILLLOOLT

Second Run Input

play
303
T
T
I
Z
L
I
L
L
I
T
T
Z
L
O
Z
L
I
Z
I
I
L
Z
I
O
T
I
L
T
L
L
I
L
Z
I
T
Z
T
O
L
L
I
L
I
O
T
L
I
I
O
L
I
T
O
T
O
T
I
I
I
T
L
L
T
T
I
L
I
Z
L
Z
O
L
L
T
L
I
O
I
T
I
I
O
L
I
O
O
T
O
I
I
O
Z
I
I
I
I
L
L
I
L
T
L
I
Z
L
L
T
I
L
I
O
L
Z
T
Z
L
L
O
I
O
T
I
L
Z
I
L
Z
T
O
T
I
Z
O
I
I
L
I
L
O
L
O
O
I
I
T
L...

Second Run Output

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

result:

ok good job!

Test #11:

score: 0
Stage 1: Program answer Runtime Error

First Run Input

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

First Run Output


Second Run Input


Second Run Output


result: