mayan游戏

原题网上去看吧,太长了这里就不贴了╮(╯_╰)╭

这道题简直毒瘤啊啊,,,超级长的搜索。。。由于bfs空间遭不住,所以选择了dfs,然后就是十分烦人的移动、下落和清除的函数,过程繁复,下面贴出代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n,a[10][10];
struct node{
    int x,y,ops;
}ans[10];
bool empty()
{
    for(int i=0;i<5;i++)
     for(int j=0;j<7;j++)
     if(a[i][j])return false;
     return true;
}
void drop()
{
    int num[10][10];
    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]];
}
bool clear()
{
    bool flag=0;
    for(int x = 0; x < 3; x++)
        for(int y = 0; y < 7; y++)
        if(a[x][y])
        {
            int x2;
            for(x2 = x; x2+1<5&&a[x2+1][y]==a[x][y]; x2++);
            if(x2 - x >= 2)
            {
                int tx;
                for(tx = x; tx <= x2; tx++)
                {
                    int Up = y,Dn = y;
                    while(Up+1<7&&a[tx][Up+1] == a[x][y]) Up++;
                    while(Dn-1>=0&&a[tx][Dn-1] == a[x][y]) Dn--;
                    if(Up - Dn >= 2)
                    {
                        int ty;
                        for(ty = Dn; ty <= Up; ty++)
                            a[tx][ty] = 0;
                    }
                }
                for(tx = x; tx <= x2; tx++)
                    a[tx][y] = 0;
                flag = 1;
            }
        }
     for(int x=0;x<5;x++)
        for(int y=0;y<5;y++)
        if(a[x][y])
        {
            int y2;
            for(y2=y;y2+1<7&&a[x][y2+1]==a[x][y];y2++);
            if(y2-y>=2)
            {
                int ty;
                for(ty=y;ty<=y2;ty++)
                {
                    int Lf=x,Ri=x;
                    while(Lf-1>=0&&a[Lf-1][ty]==a[x][y])Lf--;
                    while(Ri+1<7&&a[Ri+1][ty]==a[x][y])Ri++;
                    if(Ri-Lf>=2)
                    {
                        int tx;
                        for(tx=Lf;tx<=Ri;tx++)
                            a[tx][ty]=0;
                    }
                }
                for(ty = y; ty <= y2; ty++)
                    a[x][ty] = 0;
                flag = 1;
            }
        }
    if(flag) return true;
    else return false;
}
void dfs(int step)
{
    if(step>n)
    {
        if(empty())
        {
            for(int i=1;i<=n;i++)
            {
                if(ans[i].ops)
                cout<<ans[i].x+1<<' '<<ans[i].y<<' '<<-1<<endl;
                else
                cout<<ans[i].x<<' '<<ans[i].y<<' '<<1<<endl;
            }
            exit(0);
        }
        return ;
    }
    int sum[15];
    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]!=0&&sum[i]<3) return ;
    for(int i=0;i<4;i++)
     for(int j=0;j<7;j++)
     if(a[i][j]!=a[i+1][j])
     {
         ans[step].x=i;
         ans[step].y=j;
         ans[step].ops=(!a[i][j]);
         int temp[10][10];
         memcpy(temp,a,sizeof(temp));
         swap(a[i][j],a[i+1][j]);
         drop();
         while(clear())drop();
         dfs(step+1);
         ans[step].x=0;
         ans[step].y=0;
         ans[step].ops=0;
         memcpy(a,temp,sizeof(a));
     }
}
int main()
{
    freopen("mayan.in","r",stdin);
    freopen("mayan.out","w",stdout);
    scanf("%d",&n);
    for(int i=0;i<5;i++)
    {
        for(int j=0;;j++)
        {
            scanf("%d",&a[i][j]);
            if(a[i][j]==0) break;
        }
    }
    dfs(1);
    cout<<-1;
    return 0;
}

调试了一个多小时的clear函数,整个人都瘫了%>_<%

清清正正射命丸文是也~

原文地址:https://www.cnblogs.com/Ayateriteri/p/5823433.html