QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#266697#4323. 德州消消乐jzhWA 5ms3720kbC++207.5kb2023-11-26 16:53:132023-11-26 16:53:13

Judging History

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

  • [2023-11-26 16:53:13]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3720kb
  • [2023-11-26 16:53:13]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
const int N = 60;
int a[N][N], tmp[N][N];
int sp[N][N];
bool vis[N][N];
bool vissp[N][N];
int tmpsp[N][N];
int n, m;
set<pii> elim() {
    set<pii> ans;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j] == 0)continue;
            if (a[i - 1][j] == a[i][j] && a[i][j] == a[i + 1][j]) {
                ans.insert({i - 1, j});
                ans.insert({i, j});
                ans.insert({i + 1, j});
            }
            if (a[i][j - 1] == a[i][j] && a[i][j] == a[i][j + 1]) {
                ans.insert({i, j - 1});
                ans.insert({i, j});
                ans.insert({i, j + 1});
            }
        }
    }
    return ans;
}

int dfs(pii pos, int col) {
    if (vis[pos.first][pos.second] || a[pos.first][pos.second] != col)return 0;
    vis[pos.first][pos.second] = true;
    const int x[] = {1, -1, 0, 0}, y[] = {0, 0, 1, -1};
    int cnt = 1;
    for (int i = 0; i < 4; i++) {
        int nx = pos.first + x[i], ny = pos.second + y[i];
        cnt += dfs({nx, ny}, col);
    }
    return cnt;
}


void fall() {
    memset(tmp, 0, sizeof(tmp));
    memset(tmpsp, 0, sizeof(tmpsp));
    for (int j = 1; j <= m; j++) {
        int ci = n;
        for (int i = n; i >= 1; i--) {
            if (a[i][j] != 0) {
                tmp[ci][j] = a[i][j];
                tmpsp[ci][j] = sp[i][j];
                ci--;
            }
        }
    }
    swap(tmp, a);
    swap(tmpsp, sp);
}

vector<set<int>> maincol;

queue<pii> q;
long long c1, c2, c3, c4;
bool is_in(pii p) {
    return 1 <= p.first && p.first <= n && 1 <= p.second && p.second <= m;
}
void spelim(int turn) {
    memset(vissp, 0, sizeof(vissp));
    while (!q.empty()) {
        auto p = q.front();
        q.pop();
        if (vissp[p.first][p.second] || a[p.first][p.second] == 0)continue;
        vissp[p.first][p.second] = true;
        int u = sp[p.first][p.second];
        c1 += ( (turn+1) * a[p.first][p.second]);
        int tmpc = a[p.first][p.second];
        a[p.first][p.second] = 0;

        if (u == 1) {
            for (int i = 1; i <= m; i++) {
                q.push({p.first, i});
            }
        } else if (u == 2) {
            for (int i = 1; i <= n; i++) {
                q.push({i, p.second});
            }
        } else if (u == 3) {
            for (int i = 1; i <= m; i++) {
                q.push({p.first, i});
            }
            for (int i = 1; i <= n; i++) {
                q.push({i, p.second});
            }
        } else if (u == 4) {
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    pii np = {p.first + i, p.second + j};
                    if (is_in(np)) {
                        q.push(np);
                    }
                }
            }
        } else if (u == 5) {
            for (int i = -2; i <= 2; i++) {
                for (int j = -2; j <= 2; j++) {
                    pii np = {p.first + i, p.second + j};
                    if (is_in(np)) {
                        q.push(np);
                    }
                }
            }
        } else if (u == 6) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    if (a[i][j] == tmpc) {
                        q.push({i, j});
                    }
                }
            }
        }
    }
}

void show() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << a[i][j] << "(" << sp[i][j] <<") ";
        }
        cout << endl;
    }
};

typedef long long ll;

bool simop(pii &p1, pii &p2) {
    if (a[p1.first][p1.second] == 0 || a[p2.first][p2.second] == 0) {
        return false;
    }
    if(abs(p1.first - p2.first) + abs(p1.second - p2.second) != 1){
        return false;
    }
    swap(a[p1.first][p1.second], a[p2.first][p2.second]);
    swap(sp[p1.first][p1.second],sp[p2.first][p2.second]);
    bool lst = true;
    int turn = 0;
    while (lst) {
        auto res = elim();
        lst = (!res.empty());
        

        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                vis[i][j] = 1;
            }
        }
        for(auto p:res){
            vis[p.first][p.second] = 0;
        }
        for (auto p: res) {
            ll cnt = dfs(p, a[p.first][p.second]);
            if (cnt >= 3){
                c3 +=  50 * (cnt - 3) * (cnt - 3);
            }
        }
        set<int> col;
        for (auto p: res) {
            if(a[p.first][p.second] == 0)continue;
            col.insert(a[p.first][p.second]);
        }
        if(turn == 0 && col.empty()){
            swap(a[p1.first][p1.second], a[p2.first][p2.second]);
            swap(sp[p1.first][p1.second],sp[p2.first][p2.second]);
            return false;
        }

        if(turn==0 && col.size() > 0 ){
            maincol.push_back(col);
        }

        while (!q.empty())q.pop();
        for (auto p: res) {
            q.push(p);
        }
        spelim(turn);
        fall();
        turn++;
    }
    if(turn>=2) c2 += 80 * (turn - 2) * (turn - 2);
    return true;
}

bool all_empty() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (a[i][j])return false;
        }
    }
    return true;
}

int k;
int cnt[N];

ll dfscol(int u) {
    if (u == 5) {
        int p1 = 0, p2 = 0;
        int i1 = 0 , i2 = 0;
        for (int i = 1; i <= k; i++) {
            if (cnt[i] >= p1) {
                p2 = p1;
                i2 = i1;
                p1 = cnt[i];
                i1 = i;
            } else if (cnt[i] >= p2) {
                p2 = cnt[i];
                i2 = i;
            }
        }

        if (p1 == 5) {
            return 1000 + i1 * 10;
        } else if (p1 == 4) {
            return 750 + i1 * 5;
        } else if (p1 == 3 && p2 == 2) {
            return 500 + 3 * i1 + i2;
        } else if (p1 == 3 && p2 == 1) {
            return 300 + 3 * i1;
        } else if (p1 == 2 && p2 == 2) {
            return 200 + 2 * max(i1, i2) + min(i1, i2);
        } else if (p1 == 2 && p2 == 1) {
            return 100 + 2 * i1;
        } else {
            return 50 + i1;
        }
    }
    ll ans = -1e18;
    //cout <<" siz " << maincol[u].size() <<" on " << u << endl;
    for (int c: maincol[u]) {
        cnt[c]++;
        ans = max(ans, dfscol(u + 1));
        cnt[c]--;
    }
    return ans;
}


int main() {

    ios::sync_with_stdio(false);

    int num;
    cin >> n >> m >> k >> num;

    bool b1 = true;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> sp[i][j];
        }
    }

    for (int i = 1; i <= num; i++) {
        pii p1, p2;
        cin >> p1.first >> p1.second >> p2.first >> p2.second;
        //cout << " on sim " << p1.first << " " << p1.second << endl;
        bool sud = simop(p1, p2);
        b1 &= sud;
       // cout << " cur is " << c1 << "," << c2 << "," << c3 << " and " << c1 + c2 + c3 << endl;
        if (maincol.size() == 5) {
          //  cout <<" counting " << endl;
            c4 += dfscol(0);
            maincol.clear();
        }

    }

    ll ans = c1 + c2 + c3 + c4;
    if (b1) ans += 1000;
    if (all_empty()) ans += 10000;

    cout << ans;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

8 8 5 5
1 1 5 1 5 4 5 3
2 1 2 2 5 4 3 2
1 4 1 4 2 1 5 4
2 1 5 5 2 1 4 4
2 3 5 2 3 4 2 2
4 2 4 3 3 2 4 5
1 3 4 3 5 2 4 3
3 4 2 5 2 1 1 2
0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0
0 0 0 0 5 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 6 0 0 3 1
0 0 0 0 3 0 0 0
0 0 0 0 0 0 1 4
3 2 4 2
5 4 5 5
7 2 7 3
8 5 8 6
6 7 ...

output:

11692

result:

ok single line: '11692'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

10 10 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
1 2 1 2 1 2 1 2 1 2
2 1 2 1 2 1 2 1 2 1
0 0 0 0 0 0 0 0 0 0
0 6 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 ...

output:

108124

result:

ok single line: '108124'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3720kb

input:

50 50 2 1
2 1 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2
1 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1
1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 2 ...

output:

138130775

result:

ok single line: '138130775'

Test #4:

score: 0
Accepted
time: 5ms
memory: 3460kb

input:

49 50 65 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
2 4 5 ...

output:

91395346

result:

ok single line: '91395346'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3520kb

input:

50 50 100 1
1 1 89 1 11 35 63 13 23 74 44 29 13 2 41 11 49 14 5 22 37 82 95 62 8 75 52 28 28 32 54 48 4 12 52 78 79 9 9 46 34 65 42 38 80 48 59 37 20 94
51 92 25 46 87 26 3 19 65 42 50 72 63 19 40 1 61 35 99 13 40 83 48 13 93 56 90 29 23 15 39 68 17 63 37 50 55 32 20 86 62 92 61 61 48 28 20 20 18 52...

output:

25471

result:

ok single line: '25471'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3540kb

input:

49 50 65 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
2 4 5 ...

output:

733763

result:

ok single line: '733763'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3484kb

input:

50 50 9 1
1 1 4 1 8 5 5 3 7 6 6 8 9 2 8 4 7 1 5 4 9 9 8 6 7 6 3 7 6 7 4 2 7 7 2 6 2 7 5 5 8 4 9 7 8 8 5 1 6 3
9 9 5 8 7 6 5 5 3 3 8 1 4 2 7 6 6 1 5 8 9 1 7 8 2 9 1 6 7 7 4 7 2 5 2 6 5 4 4 9 7 7 8 5 2 2 1 1 4 3
4 5 2 4 1 2 1 8 3 6 6 3 3 5 5 4 5 6 6 2 6 1 5 8 2 2 9 4 6 5 8 2 2 3 1 1 8 9 2 2 7 8 3 9 4 ...

output:

4472

result:

ok single line: '4472'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

50 50 9 1
1 1 3 1 3 8 8 1 2 1 7 7 5 5 9 1 7 6 2 7 1 9 8 5 4 8 3 4 8 9 3 7 7 3 9 2 8 9 8 7 6 2 7 4 9 9 7 2 8 5
6 6 8 7 7 2 5 6 4 8 4 8 6 1 1 6 4 4 5 5 4 7 6 2 2 1 7 2 3 1 2 6 2 1 9 2 2 7 9 7 5 6 7 9 8 7 7 3 6 7
8 9 5 7 7 8 9 1 5 8 3 9 4 6 1 6 8 4 5 1 9 3 8 3 3 5 9 4 4 3 7 9 5 8 8 7 5 2 3 9 2 7 6 2 6 ...

output:

10659

result:

ok single line: '10659'

Test #9:

score: -100
Wrong Answer
time: 5ms
memory: 3480kb

input:

50 48 100 1000
17 96 31 100 66 100 26 97 28 97 13 97 65 95 31 95 6 95 26 99 90 96 37 96 73 97 46 97 80 97 44 100 63 98 25 98 10 97 54 98 70 98 74 98 64 98 55 98
96 62 96 70 100 4 97 54 97 36 97 82 95 23 95 34 95 90 99 32 99 67 96 7 97 11 97 4 97 94 100 26 100 2 98 79 97 42 97 64 98 73 98 14 98 68 98...

output:

94259

result:

wrong answer 1st lines differ - expected: '104407', found: '94259'