zoukankan      html  css  js  c++  java
  • 【NOIP 2011】 Mayan游戏

    【题目链接】

               https://www.luogu.org/problemnew/show/P1312

    【算法】

                深度优先搜索

    【代码】

               

    #include<bits/stdc++.h>
    using namespace std;
    
    struct info
    {
            int x,y,op;
    } step[10];
    
    int i,n,t,x;
    int a[10][10];
    bool solved;
    
    inline void print()
    {
            int i;
            for (i = 1; i <= n; i++) printf("%d %d %d
    ",step[i].x - 1,step[i].y - 1,step[i].op);
    }
    inline bool check()
    {
            int i;
            for (i = 1; i <= 5; i++)
            {
                    if (a[i][1] != 0)
                            return false;
            }
            return true;
    }
    inline bool Remove()
    {
            int i,j,x,y,t;
            bool ret = false;
            bool d[10][10];
            memset(d,false,sizeof(d));
            for (i = 1; i <= 5; i++)
            {
                    for (j = 1; j <= 7; j++)
                    {
                            if (a[i][j])
                            {
                                    for (x = j + 1; a[i][j] == a[i][x] && x <= 7; x++);
                                    if (x - j >= 3)
                                    {
                                            for (y = j; y < x; y++)
                                                    d[i][y] = true;
                                            ret = true;
                                    }
                                    for (x = i + 1; a[i][j] == a[x][j] && x <= 5; x++);
                                    if (x - i >= 3)
                                    {
                                            for (y = i; y < x; y++)
                                                    d[y][j] = true;
                                            ret = true;
                                    }
                            }
                    }
            }
            for (i = 1; i <= 5; i++)
            {
                    for (j = 1; j <= 7; j++)
                    {
                            if (d[i][j])
                                    a[i][j] = 0;
                    }
            }
            for (i = 1; i <= 5; i++)
            {
                    t = 0;
                    for (j = 1; j <= 7; j++)
                    {
                            if (!a[i][j])
                            {
                                    for (x = j + 1; x <= 7 && !a[i][x]; x++);
                                    a[i][j] = a[i][x];
                                    a[i][x] = 0;
                            }
                    }                
            }
            return ret;
    }
    
    inline void dfs(int dep)
    {
            int i,j,x,y;
            int b[10][10];
            if (dep > n)
            {
                    if (check())
                    {
                            solved = true;
                            print();
                    }
                    return;        
            }        
            for (i = 1; i <= 5; i++)
            {
                    for (j = 1; j <= 7; j++)
                    {
                            b[i][j] = a[i][j];    
                    }
            }
            for (i = 1; i <= 5; i++)
            {
                    for (j = 1; j <= 7; j++)
                    {
                            if (i < 5 && a[i][j] != 0)
                            {
                                    if (a[i+1][j] != 0)
                                    {
                                            step[dep] = (info){i,j,1};
                                            swap(a[i][j],a[i+1][j]);
                                            while (Remove());
                                            dfs(dep+1);
                                            if (solved) return;
                                            for (x = 1; x <= 5; x++)
                                            {
                                                    for (y = 1; y <= 7; y++)
                                                    {
                                                            a[x][y] = b[x][y];
                                                    }
                                            }
                                    } else
                                    {
                                            step[dep] = (info){i,j,1};
                                            for (x = j - 1; x >= 1 && !a[i+1][x]; x--);
                                            a[i+1][x+1] = a[i][j];
                                            for (x = j; x <= 7; x++) a[i][x] = a[i][x+1];
                                            while (Remove());  
                                            dfs(dep+1);
                                            if (solved) return;
                                            for (x = 1; x <= 5; x++)
                                            {
                                                    for (y = 1; y <= 7; y++)
                                                    {
                                                            a[x][y] = b[x][y];
                                                    }
                                            }
                                    }
                            } 
                            if (i > 1 && a[i][j] != 0)
                            {
                                    if (a[i-1][j] == 0)
                                    {
                                            step[dep] = (info){i,j,-1};
                                            for (x = j - 1; x >= 1 && !a[i-1][x]; x--);
                                            a[i-1][x+1] = a[i][j];
                                            for (x = j; x <= 7; x++) a[i][x] = a[i][x+1];
                                            while (Remove());
                                            dfs(dep+1);
                                            if (solved) return;
                                            for (x = 1; x <= 5; x++)
                                            {
                                                    for (y = 1; y <= 7; y++)
                                                    {
                                                            a[x][y] = b[x][y];
                                                    }
                                            }
                                    }
                            }
                    }
            }
    }
    
    int main() 
    {
            
            scanf("%d",&n);
            for (i = 1; i <= 5; i++)
            {
                    t = 0;
                    while (scanf("%d",&x) && x)
                            a[i][++t] = x;    
            }
            solved = false;
            dfs(1);
            if (!solved) printf("-1
    ");
            
            return 0;
        
    }
  • 相关阅读:
    第108题:将有序数组转换成二叉搜索树
    第107题:二叉树的层次遍历II
    第106题:从中序与后序遍历序列构造二叉树
    java类读取properties文件
    WdatePicker.js开始日期和结束日期比较
    对两个整数变量的值进行互换
    Java基础知识总结
    jdk环境变量
    逻辑运算符有什么用?
    if和switch的应用
  • 原文地址:https://www.cnblogs.com/evenbao/p/9278326.html
Copyright © 2011-2022 走看看