luoguP1312 Mayan游戏 题解(NOIP2011)

luoguP1312 Mayan游戏 题目

#include<bits/stdc++.h>
#define ll long long
#define rg register
#define il inline
#define inf 1<<30;
using namespace std;

int a[10][10],b[6][10][10];
int tmp[10][10];
int way[10][3];
int n,ans,maxn;

il ll gi() 
{
    ll x=0,o=1;
    char ch;ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
il void input() 
{
    n=gi();
    for(int i=1;i<=5;i++) 
    {
        int tmp=gi(),p=7;
        while(tmp)
            a[p--][i]=tmp,tmp=gi();
        a[8][i]=1;
    }
}
il void print(int a[10][10]) 
{
    for(int i=1;i<=7;i++) 
    {
        for(int j=1;j<=5;j++)
            cout<<a[i][j];
        cout<<endl;
    }
}
il void drop(int y) 
{
    int p=0;
    for(int i=7;i>=1;i--) 
    {
        if(a[i][y]!=0)
        {
            p=i+1;
            while(a[p][y]==0)
                p++;
            if(p==i+1) continue;
            p-=1;
            a[p][y]=a[i][y];
            a[i][y]=0;
        }
    }
}
il int del(int i,int j,int deti,int detj) 
{
    int fi=i,fj=j,s=1;
    while(1) 
    {
        i+=deti;j+=detj;
        if(i==0||i==8||j==0||j==6) break;
        if(tmp[i][j]!=tmp[fi][fj]) break;
        s++;
        if(s==3) break;
    }
    i=fi;j=fj;
    while(1) 
    {
        i-=deti;j-=detj;
        if(i==0||i==8||j==0||j==6) break;
        if(tmp[i][j]!=tmp[fi][fj]) break;
        s++;
        if(s==3) break;
    }
    if(s>=3)
    {
        a[fi][fj]=0;
        return 1;
    }
    return 0;
}
il int delet()
{
    int flag=0;
    for(int i=7;i>=1;i--)
        for(int j=1;j<=5;j++)
            tmp[i][j]=a[i][j];
    for(int i=7;i>=1;i--)
        for(int j=1;j<=5;j++) 
        {
            if(a[i][j]==0) continue;
            if(del(i,j,1,0)) flag=1;
            if(del(i,j,0,1)) flag=1;
        }
    for(int i=1;i<=5;i++)drop(i);
    return flag;
}
il void move(int x,int y,int mode) 
{
    swap(a[x][y],a[x][y+mode]);
    drop(y);
    drop(y+mode);
    while(delet());
}
il bool check(int b[10][10])
{
    for(int i=7;i>=1;i--)
        for(int j=1;j<=5;j++)
        {
            if(a[i][j]!=0)
                return 0;
        }
    return 1;
}
il void reset(int k) 
{
    for(int l=1;l<=7;l++)
        for(int r=1;r<=5;r++)
            a[l][r]=b[k][l][r];
}
il void copy(int k) 
{
    for(int l=1;l<=7;l++)
        for(int r=1;r<=5;r++)
            b[k+1][l][r]=a[l][r];
}
il void dfs(int k)
{
    if(k>n)
    {
        if(check(b[k])) 
        {
            ans=1;
            for(int i=1;i<k;i++)
            {
                for(int j=0;j<=2;j++)
                    cout<<way[i][j]<<' ';
                cout<<endl;
            }
        }
        return;
    }
    if(ans) return;
    for(int j=1;j<=5;j++) 
    {
        for(int i=7;i>=1;i--)
        {
            reset(k);
            if(a[i][j]==0) continue;
            if(j!=5&&a[i][j+1]!=a[i][j])
            {
                way[k][0]=j-1;
                way[k][1]=7-i;
                way[k][2]=1;
                move(i,j,1);
                copy(k);
                dfs(k+1);
            }
            if(ans) return;
            reset(k);
            if(j!=1&&a[i][j-1]==0) 
            {
                way[k][0]=j-1;
                way[k][1]=7-i;
                way[k][2]=-1;
                move(i,j,-1);
                copy(k);
                dfs(k+1);
            }
            if(ans)    return;
        }
    }
}

int main() 
{
    input();
    for(int i=1;i<=7;i++)
        for(int j=1;j<=5;j++)
            b[1][i][j]=a[i][j];
    dfs(1);
    if(!ans) cout<<"-1"<<endl;
    return 0;
}

 

posted @ 2018-04-05 07:45  Eternal风度  阅读(142)  评论(0编辑  收藏  举报
/*自定义地址栏logo*/