2016年蓝桥杯初赛-方格填数

题目:

方格填数

如下的10个格子
+–+–+–+
| | | |
+–+–+–+–+
| | | | |
+–+–+–+–+
| | | |
+–+–+–+

(如果显示有问题,也可以参看【图1.jpg】)

填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)

一共有多少种可能的填数方案?

请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

我一开是用暴力for循环的方法,后面采用dfs.

暴力:

package cn.zhku.province2017.b.six;

import java.util.Arrays;

/**
 * @author : 钱伟健 gonefuture@qq.com
 * @version : 2017/12/4 20:22.
 * 说明:
 */
public class Violence {
    //  一个6 x 6 矩阵, 4 x 4 的范围为方格
    static int[][]  matrix = new int[5][6];

    public static void main(String[] args) {
        //  给矩阵赋予初值
        for (int i=0; i<5;i++) {
            for (int j=0; j<6;j++) {
                matrix[i][j] = 20;
            }
        }
        System.out.println(Arrays.deepToString(matrix));
        long startTime=System.currentTimeMillis();//记录开始时间

        int count = 0;
        for (int a= 0; a<10; a++) {
            for (int b= 0; b<10; b++) {
                for (int c= 0; c<10; c++) {
                    for (int d= 0; d<10; d++) {
                        for (int e= 0; e<10; e++) {
                            for (int f= 0; f<10; f++) {
                                for (int g= 0; g<10; g++) {
                                    for (int h= 0; h<10; h++) {
                                        for (int i= 0; i<10; i++) {
                                            for (int j= 0; j<10; j++) {
                                                matrix[1][2] = a;
                                                matrix[1][3] = b;
                                                matrix[1][4] = c;
                                                matrix[2][1] = d;
                                                matrix[2][2] = e;
                                                matrix[2][3] = f;
                                                matrix[2][4] = g;
                                                matrix[3][1] = h;
                                                matrix[3][2] = i;
                                                matrix[3][3] = j;
                                                if ( deDuplicate(a,b,c,d,e,f,g,h,i,j)
                                                        && check(1,2,a)
                                                        && check(1,3,b)
                                                        && check(1,4,c)
                                                        && check(2,1,d)
                                                        && check(2,2,e)
                                                        && check(2,3,f)
                                                        && check(2,4,g)
                                                        && check(3,1,h)
                                                        && check(3,2,i)
                                                        && check(3,3,j)
                                                        ) {
                                                    count++;
                                                    System.out.println("矩阵是:"+Arrays.deepToString(matrix));
                                            }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.print("===============共有:"+count+"种方法");
        long endTime=System.currentTimeMillis();//记录结束时间

        float excTime=(float)(endTime-startTime)/1000;

        System.out.println("执行时间:"+excTime+"s");
    }

    public static boolean deDuplicate(int a, int b,int c, int d,int e, int f,int g, int h,int i,int j) {
        if(        a!=b && a!=c && a!=d && a!=e && a!=f && a!=g && a!=h && a!=i && a!=j
                && b!=c && b!=d && b!=e && b!=f && b!=g  && b!=h && b!=i && b!=j
                && c!=d && c!=e && c!=f && c!=g && c!=h && c!=i && c!=j
                && d!=e && d!=f && d!=g && d!=h && d!=i && d!=j
                && e!=f && e!=g && e!=h && e!=i && e!=j
                && f!=g && f!=h && f!=i && f!=j
                && g!=h && g!=i && g!=j
                && h!=i && h!=j
                && i!=j ) {
            return true;
        } else {
            return false;
        }
    }

    static boolean check(int row,int col,int n) {
        int l = n-1; // 比n小1的左相邻
        int r = n+1; // 比n大1的右相邻

        if (    l != matrix[row-1][col-1] && l != matrix[row-1][col] && l!=matrix[row-1][col+1] &&
                l != matrix[row+1][col-1] && l != matrix[row+1][col] && l!=matrix[row+1][col+1] &&
                l != matrix[row][col-1] && l!= matrix[row][col+1] &&

                r != matrix[row-1][col-1] && r != matrix[row-1][col] && r!=matrix[row-1][col+1] &&
                r != matrix[row+1][col-1] && r != matrix[row+1][col] && r!=matrix[row+1][col+1] &&
                r != matrix[row][col-1] && r!= matrix[row][col+1]
                ) {
            return true;
        } else {
            return false;
        }
    }
}

dfs:

package cn.zhku.province2017.b.six;

import java.util.Arrays;

/**
 * @author : 钱伟健 gonefuture@qq.com
 * @version : 2017/11/27 17:29.
 * 说明:
 */
public class Main {

    //  一个5 x 6 矩阵, 3 x 4 的范围为方格
    private static int[][]  matrix = new int[7][8];

    //  填充的数字
    private static  boolean[] number = new boolean[10];

    //  填数成功次数
    private static int count = 0;

    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(matrix));
        long startTime=System.currentTimeMillis();//记录开始时间

        //  给矩阵赋予初值
        for (int i=0; i<5;i++) {
            for (int j=0; j<6;j++) {
                matrix[i][j] = -9;
            }
      }
        show();
        System.out.println("================开始=============");
        dfs(1,2);

        long endTime=System.currentTimeMillis();//记录结束时间

        float excTime=(float)(endTime-startTime)/1000;

        System.out.println("执行时间:"+excTime+"s");

    }

    //  打印矩阵
    private static void show() {
        for (int i=1; i<=3;i++) {
            for (int j=1; j<=4;j++) {
                System.out.printf("%6d",matrix[i][j]);
            }
            System.out.println();
        }
    }


    private static void dfs(int row,int col) {
        //  走到最后的格子
        if (row ==3 && col==4) {
            count++;
            show();
            System.out.println(count+"个符合条件");
            return;
        }

        if (row == 1 && col == 5) {
            dfs(2,1);
            return;
        }

        if (row ==2  && col ==5) {
            dfs(3,1);
            return;
        }

        for (int n=0; n<10; n++ ) {
            if (check(row, col,n) && !number[n]){
                number[n] = true;
                matrix[row][col] = n;
                dfs(row,col+1);
                matrix[row][col] = -9;
                number[n] = false;
            }
        }
    }


    static boolean check(int row,int col,int n) {
        int l = n-1; // 比n小1的左相邻
        int r = n+1; // 比n大1的右相邻

        if (    l != matrix[row-1][col-1] && l != matrix[row-1][col] && l!=matrix[row-1][col+1] &&
                l != matrix[row+1][col-1] && l != matrix[row+1][col] && l!=matrix[row+1][col+1] &&
                l != matrix[row][col-1] && l!= matrix[row][col+1] &&

                r != matrix[row-1][col-1] && r != matrix[row-1][col] && r!=matrix[row-1][col+1] &&
                r != matrix[row+1][col-1] && r != matrix[row+1][col] && r!=matrix[row+1][col+1] &&
                r != matrix[row][col-1] && r!= matrix[row][col+1]
                ) {
            return true;
        } else {
            return false;
        }
    }

}

答案是1580种方案.

暴力那个算法差不多用了两分钟,dfs的算法用了0.651s,对比明显.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值