洛谷 #1312. Mayan遊戲(dfs)

題意

在一個7行5列的期盤中,有至多10種顏色的方塊。當這些方塊下方空的時候,這些方塊總是會垂直掉落#### 當有3個顏色相同的方塊排成一行或一列時,它們會被同時消除

你需要在給定的步數內,通過調換相鄰方塊,使棋盤上沒有方塊。

若做不到,輸出-1;不然輸出1種方法(要移動的方塊的x、y座標 + 移動方向) (移動方向 1 = 右移,-1 = 左移)

【注】當有多組解時,以x爲第1關鍵字,以y爲第2關鍵字,且1優於-1。故輸出是唯一的

【注】棋盤讀入格式:共有5行,每行以0結尾,代表從左至右每列上從下到上的方塊顏色,每行至多8個數

題解

還是比較常規的dfs題

按照題意來,每次dfs枚舉要移動的方塊時,以x爲第1關鍵字,以y爲第2關鍵字

右移條件:首先選定的格子要有方塊;其次右邊有格子,且要交換的兩個方塊顏色不同

左移條件:首先選定的格子要有方塊;其次左邊有格子,且左邊沒有方格(不然還不如右移)

移完判斷清除和掉落,若步數大於n或當前有任意顏色的棋子只有1、2個,則return

若條件符合,直接輸出

調試記錄

Class裏面把‘N’達成‘n’

以後再也不用大小寫分辨變量了~~

#include <cstdio>
#include <cstring>
#include <cstdlib>
#define N 7
#define M 5

using namespace std;

void Swap(int &a, int &b){
    a ^= b ^= a ^= b;
}

int n, x[11], y[11], p[11];

class class_table{
    public:
        int table[N + 1][M + 1], num[11];
        class_table(){
            memset(this, 0, sizeof this);
        }
        bool Clear(){
            bool empty[N + 1][M + 1], res = false;
            memset(empty, 0, sizeof(empty));
            for (int i = 1; i <= N; i++){
                for (int j = 1; j <= M; j++){
                    if (i >= 3 && table[i][j] == table[i - 1][j] && table[i - 1][j] == table[i - 2][j] && table[i][j])
                        empty[i][j] = empty[i - 1][j] = empty[i - 2][j] = true;
                    if (j >= 3 && table[i][j] == table[i][j - 1] && table[i][j - 1] == table[i][j - 2] && table[i][j])
                        empty[i][j] = empty[i][j - 1] = empty[i][j - 2] = true;
                }
            }
            
            for (int i = 1; i <= N; i++){
                for (int j = 1; j <= M; j++){
                    if (empty[i][j]) num[table[i][j]]--, table[i][j] = 0, res = true;
                }
            }
            
            return res;
        }
        
        void Drop(){
            for (int j = 1; j <= M; j++){
                int tmp[8], cnt = 0;
                for (int i = 1; i <= N; i++){
                    if (table[i][j]) tmp[++cnt] = table[i][j];
                }
                for (int i = 1; i <= cnt; i++) table[i][j] = tmp[i];
                for (int i = cnt + 1; i <= N; i++) table[i][j] = 0;
            }
        }
        
        void Swap(int x1, int y1, int x2, int y2){
            :: Swap(table[x1][y1], table[x2][y2]);
            this -> Drop();
            while (this -> Clear()) this -> Drop();
        }
        bool Invalid(){
            for (int i = 1; i <= 10; i++) if (num[i] == 1 || num[i] == 2) return true;
            return false;
        }
        bool Finished(){
            for (int i = 1; i <= N; i++)
                for (int j = 1; j <= M; j++) if (table[i][j]) return false;
            return true;
        }
} now;

void Print(int dep){
    for (int i = 1; i <= dep; i++) printf("%d %d %d\n", x[i], y[i], p[i]);
}

void Dfs(int dep){
    if (now.Invalid()) return;
    if (now.Finished()) Print(dep - 1), exit(0); 
    if (dep > n) return;
    
    class_table temp = now; //temp.Watches();
    for (int j = 1; j <= M; j++){
        for (int i = 1; i <= N; i++){
            if (temp.table[i][j]){
                if (j < M && temp.table[i][j] != temp.table[i][j + 1]){
                    now = temp;
                    now.Swap(i, j, i, j + 1);
                    x[dep] = j - 1; y[dep] = i - 1; p[dep] = 1;
                    Dfs(dep + 1);
                }
                if (j > 1 && !temp.table[i][j - 1]){
                    now = temp;
                    now.Swap(i, j, i, j - 1);
                    x[dep] = j - 1; y[dep] = i - 1; p[dep] = -1;
                    Dfs(dep + 1);
                }
            }
        }
    }
}

int main(){
    scanf("%d", &n);
    
    int X;
    for (int i = 1; i <= M; i++) 
        for (int j = 1; scanf("%d", &X); ++j) if (X) ++now.num[now.table[j][i] = X]; else break;
    //for (int i = 1; i <= 10; i++) if (now.num[i]) printf("%d\n", now.num[i]); 
    Dfs(1); printf("-1\n");
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章