[Luogu] Mayan游戏

https://www.luogu.org/problemnew/show/P1312

太恶心了

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>

using namespace std;
const int N = 10;

struct Node{int x, y, ho;} answer[N];
int n;
int a[N][N];

inline int read() {
    int x = 0; char c = getchar();
    while(c < '0' || c > '9')c = getchar();
    while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
    return x;
}

void drop() {
    int num[N][N];
    memset(num, -1, sizeof(num));
    for(int i = 0; i < 5; i ++) {
        int h = 0;
        for(int j = 0; j < 7; j ++) if(a[i][j]) num[i][h ++] = j;
    }
    for(int i = 0; i < 5; i ++)
        for(int j = 0; j < 7; j ++)
            a[i][j] = num[i][j] == -1 ? 0 : a[i][num[i][j]];
    return ;
}

bool empty() {
    for(int i = 0; i < 5; i ++) for(int j = 0; j < 7; j ++) if(a[i][j]) return 0;
    return 1;
}

bool clear() {
    bool ret_flag = 0;
    for(int i = 0; i < 3; i ++)
        for(int j = 0; j < 7; j ++)
            if(a[i][j]) {
                int x = i;
                while(x < 4 && a[i][j] == a[x + 1][j]) x ++;
                if(x - i >= 2) {
                    for(int xx = i; xx <= x; xx ++) {
                        int up = j; int dn = j;
                        while(a[xx][up + 1] == a[i][j] && up < 6) up ++;
                        while(a[xx][dn - 1] == a[i][j] && dn > 0) dn --;
                        if(up - dn >= 2)
                            for(int y_ = dn; y_ <= up; y_ ++) a[xx][y_] = 0;
                    }
                    for(int x_ = i; x_ <= x;  x_ ++) a[x_][j] = 0;
                    ret_flag = 1;
                }
            }
    for(int i = 0; i < 5; i ++)
        for(int j = 0; j < 5; j ++)
            if(a[i][j]) {
                int y = j;
                while(a[i][y + 1] == a[i][j] && y < 6) y ++;
                if(y - j >= 2) {
                    for(int yy = j; yy <= y; yy ++) {
                        int lef = i; int rig = i;
                        while(a[lef - 1][yy] == a[i][j] && lef > 0) lef --;
                        while(a[rig + 1][yy] == a[i][j] && rig < 6) rig ++;
                        if(rig - lef >= 2)
                            for(int x_ = lef; x_ <= rig; x_ ++) a[x_][yy] = 0;
                    }
                    for(int y_ = j; y_ <= y; y_ ++) a[i][y_] = 0;
                    ret_flag = 1;
                }
            }
    if(ret_flag) return 1;
    else return 0;
}

void dfs(int tot) {
    if(tot > n) {
        if(empty()) {
            for(int i = 1; i <= n; i ++)  {
                if(answer[i].ho) printf("%d %d %d
", answer[i].x + 1, answer[i].y, -1);
                else printf("%d %d %d
", answer[i].x, answer[i].y, 1);
            }
            exit(0);
        }
        return ;
    }        
    int sum[N + 1];
    memset(sum, 0, sizeof(sum));
    for(int i = 0; i < 5; i ++) for(int j = 0; j < 7; j ++) sum[a[i][j]] ++;
    for(int i = 1; i <= 10; i ++) if(sum[i] && sum[i] <= 2) return ;
    for(int i = 0; i < 4; i ++)
        for(int j = 0; j < 7; j ++)
            if(a[i][j] != a[i + 1][j]) {
                answer[tot].x = i; answer[tot].y = j; answer[tot].ho = (!a[i][j]);
                int tmp[N][N];
                memcpy(tmp, a, sizeof(tmp));
                swap(a[i][j], a[i + 1][j]);
                drop();
                while(clear()) drop();
                dfs(tot + 1);
                answer[tot].x = 0; answer[tot].y = 0; answer[tot].ho = 0;
                memcpy(a, tmp, sizeof(a));
            }
}

int main() {
    n = read();
    for(int i = 0; i < 5; i ++) {
        for(int j = 0; ; j ++) {
            a[i][j] = read();
            if(!a[i][j]) break;
        }    
    }
    dfs(1);
    printf("-1
"); 
    return 0;
}
/*
3
1 0
2 1 0
2 3 4 0
3 1 0
2 4 3 4 0
*/
原文地址:https://www.cnblogs.com/shandongs1/p/8643354.html