NOIP2011Mayan 游戏

来源:互联网 发布:nginx 拦截指定域名 编辑:程序博客网 时间:2024/06/18 14:41

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
原创粉丝点击