zoukankan      html  css  js  c++  java
  • 暴力(解数阵)

    题目大意:

      就是说给你1-6这些数字,把这些数字放到一个2*3的矩阵中,要求左边的数字比右边的数字大,上边的数字比下边的数字大,输出所有的方案总数。

    解题思路:

    ORZ,群里的一个小学弟问我的问题,由于没有OJ系统,我直接暴力解了,对于一个2*3的矩阵.

    6 5 4
    3 2 1

    这是我们最容易想到的一组解了,然后,我们就在这个解的情况下,进行变化,

    先从a[0]开始枚举,因为a[0]没有限制因素,它处于左上角,它比右边的数字大,比下边的数字小。

    再从a[1]开始枚举,a[2]开始枚举,a[3]开始枚举,到了a[4]就要注意了,因为a[4]的位置比较特殊

    他受a[1]和a[3]的影响,也就是说:a1] > a[4], a[3] > a[4],但是a[1]和a[3]的大小关系是不

    能确定的,那么我们就需要分别讨论a[1]和a[3]的大小关系了。

    if a[1]>a[3]的话,那么a[4]的值就从a[3]-1开始枚举

    if a[1]<a[3]的话,那么a[4]的值就从a[1]-1开始枚举

    在枚举的过程中,肯定或多或少的会出现枚举到了重复数字的情况,所以,我们应该再加一个check

    来判重。

    代码:

    # include<cstdio>
    # include<iostream>
    # include<algorithm>
    # include<cstring>
    # include<string>
    # include<cmath>
    # include<queue>
    # include<stack>
    # include<set>
    # include<map>
    
    
    typedef long long LL;
    typedef unsigned long long ULL;
    
    using namespace std;
    
    # define inf 999999999
    # define MAX 10
    
    
    int flag;
    int cnt;
    int a[MAX];
    
    void init()
    {
        for ( int i = 0;i < 6;i++ )
        {
            a[i] = 6-i;
        }
    }
    
    
    int check ( int a[] )
    {
        for ( int i = 0;i < 6-1;i++ )
        {
            for ( int j = i+1;j < 6;j++ )
            {
                if ( a[i]==a[j] )
                {
                    return 0;
                }
            }
        }
        return 1;
    }
    
    void output()
    {
        for ( int i = 0;i < 6;i++ )
        {
            if ( i%3==0 )
            {
                cout<<endl;
                cout<<a[i]<<" ";
            }
            else
            {
                cout<<a[i]<<" ";
            }
        }
        cout<<endl;
        cout<<endl;
    }
    
    void solve()
    {
        for ( a[1] = a[0]-1;a[1] > 0;a[1]-- )
        {
            for ( a[2]= a[1]-1;a[2] > 0;a[2]-- )
            {
                for ( a[3] = a[0]-1;a[3] > 0;a[3]-- )
                {
                    int temp;
                    if ( a[3] > a[1] )
                    {
                        temp = a[1] - 1;
                    }
                    else
                    {
                        temp = a[3] - 1;
                    }
                    for ( a[4] = temp;a[4] > 0;a[4]-- )
                    {
                        flag = check(a);
                        if ( flag )
                        {
                            cnt++;
                            output();
                        }
                    }
                }
            }
        }
    }
    
    int main(void)
    {
        init();
        solve();
        cout<<"总数:"<<cnt<<endl;
    
    	return 0;
    }
    

      

    代码:

  • 相关阅读:
    Advanced Configuration Tricks
    Reviewing the Blog Module
    Editing and Deleting Data
    Making Use of Forms and Fieldsets
    Understanding the Router
    SQL Abstraction and Object Hydration
    Preparing for Different Databases
    Java学习理解路线图
    Openstack学习历程_1_视频
    CentOS安装Nginx负载
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4375691.html
Copyright © 2011-2022 走看看