NOIP2011Mayan 游戏

Mayan 游戏

【题目分析】
这题与之前写过的天天爱消除十分的相似(想死)。
这题的最难点在于消除、下落与合并:
显然是用个while来操作不断消除与不断下落、不断合并,而消除、下落与合并又是相对独立的函数,
所以每个函数的衔接十分重要。(如果不是暴搜,起码还能水到部分分,但是在赛场上调暴搜是一件十分尴尬的事情,因为如果没调出来,真的就要爆炸了)

【代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
struct T{
    int x,y,ops;
}ans[10];
int n,st[10][10];
bool empty(){
    for(int i=0;i<5;i++){
        for(int j=0;j<7;j++){
            if(st[i][j])return 0;
        }
    }return 1;
}
void drop(){
    int num[10][10];
    memset(num,-1,sizeof(num));
    for(int x=0;x<5;x++){
        int h=0;
        for(int y=0;y<7;y++){
            if(st[x][y])num[x][h++]=y;
        }
    }
    for(int x=0;x<5;x++){
        for(int y=0;y<7;y++){
            if(num[x][y]==-1)st[x][y]=0;
            else st[x][y]=st[x][num[x][y]];
        }
    }
}
bool clear(){
    bool f=0;
    for(int x=0;x<3;x++){
        for(int y=0;y<7;y++){
            if(st[x][y]){
                int x2=x;
                while(x2<4&&st[x2+1][y]==st[x][y])x2++;
                if(x2-x>=2){
                    for(int tx=x;tx<=x2;tx++){
                        int Up=y,Dn=y;
                        while(Up+1<7&&st[tx][Up+1]==st[x][y])Up++;
                        while(Dn-1>=0&&st[tx][Dn-1]==st[x][y])Dn--;
                        if(Up-Dn>=2){
                            int ty;
                            for(ty=Dn;ty<=Up;ty++)st[tx][ty]=0;
                        }
                    }for(int tx=x;tx<=x2;tx++)st[tx][y]=0;
                    f=1;
                }
            }
        }
    }
    for(int x=0;x<5;x++){
        for(int y=0;y<5;y++){
            if(st[x][y]){
                int y2=y;
                while(y2<6&&st[x][y2+1]==st[x][y])y2++;
                if(y2-y>=2){
                    for(int ty=y;ty<=y2;ty++){
                        int Lf=x,Ri=x;
                        while(Lf-1>=0&&st[Lf-1][ty]==st[x][y])Lf--;
                        while(Ri+1<7&&st[Ri+1][ty]==st[x][y])Ri++;
                        if(Ri-Lf>=2){
                            int tx;
                            for(tx=Lf;tx<=Ri;tx++)st[tx][ty]=0;
                        }
                    }
                    for(int ty=y;ty<=y2;ty++)st[x][ty]=0;
                    f=1;
                }
            }
        }
    }return f;
}
bool H(){
    int sum[12];
    memset(sum,0,sizeof(sum));
    for(int x=0;x<5;x++){
        for(int y=0;y<7;y++)sum[st[x][y]]++;
    }
    for(int i=1;i<=10;i++){
        if(sum[i]!=0&&sum[i]<3)return 0;
    }return 1;
}
void dfs(int step){
    if(step>n){
        if(empty()){
            for(int i=1;i<=n;i++){
                if(ans[i].ops)printf("%d %d %d\n",ans[i].x+1,ans[i].y,-1);
                else printf("%d %d %d\n",ans[i].x,ans[i].y,1);
            }exit(0);
        }return;
    }if(!H())return;
    for(int x=0;x<4;x++){
        for(int y=0;y<7;y++){
            if(st[x][y]!=st[x+1][y]){
                ans[step].x=x;
                ans[step].y=y;
                ans[step].ops=(!st[x][y]);
                int temp[10][10];
                memcpy(temp,st,sizeof(temp));
                swap(st[x][y],st[x+1][y]);
                drop();
                while(clear())drop();
                dfs(step+1);
                ans[step].x=ans[step].y=ans[step].ops=0;
                memcpy(st,temp,sizeof(st));
            }
        }
    }
}
int main(){
    scanf("%d",&n);
    for(int i=0;i<5;i++){
        for(int j=0;;j++){
            scanf("%d",&st[i][j]);
            if(st[i][j]==0)break;
        }
    }dfs(1);
    puts("-1");
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值