zoukankan      html  css  js  c++  java
  • 方格填数

    /*如下的10个格子
       +--+--+--+
       |  |  |  |
    +--+--+--+--+
    |  |  |  |  |
    +--+--+--+--+
    |  |  |  |
    +--+--+--+
    
    填入0~9的数字。要求:连续的两个数字不能相邻。
    (左右、上下、对角都算相邻)
    
    一共有多少种可能的填数方案?
    
    请填写表示方案数目的整数。
    注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
    1580*/
    package test;
    
    public class 方格填数 {
        static int count = 0;
        
        static void swap(char array[], int a, int b) {  
            char temp = array[a];  
            array[a] = array[b];  
            array[b] = temp;  
        }  
        
        static boolean check(char [] str) {  
            if(check2(str[0], str[1]) && check2(str[0], str[3]) && check2(str[0], str[4]) && check2(str[0], str[5])   
                     && check2(str[1], str[2]) && check2(str[1], str[4]) && check2(str[1], str[5]) && check2(str[1], str[6])  
                     && check2(str[2], str[5]) && check2(str[2], str[6])  
                     && check2(str[3], str[4]) && check2(str[3], str[7]) && check2(str[3], str[8])  
                     && check2(str[4], str[5]) && check2(str[4], str[7]) && check2(str[4], str[8]) && check2(str[4], str[9])  
                     && check2(str[5], str[6]) && check2(str[5], str[8]) && check2(str[5], str[9])  
                     && check2(str[6], str[9]) && check2(str[7], str[8]) && check2(str[8], str[9]))  
                return true;  
            return false;  
        }
        
        static boolean check2(char c, char d) {  
            // TODO Auto-generated method stub  
            if(c == (d+1) || c == (d-1))  
                return false;  
            return true;  
        }
        
        static void permutation(char[] str, int a, int length){  
            if(a == length){  
                if(check(str)){  //检查是否有相邻的连续数字
                    count++;  
                    System.out.println(String.valueOf(str));  
                }  
            }else{  
                for(int i = a; i <= length; ++i){  
                    swap(str, i, a);  
                    permutation(str, a + 1, length);
                    swap(str, a, i);  
                }  
            }  
        }
      
        public static void main(String[] args) {  
            char[] str = "0123456789".toCharArray();  
            permutation(str, 0, 9);  
            System.out.println(count);  
        }  
    }
  • 相关阅读:
    Magento安装教程
    让老婆爱你的十大方法。
    easy ui layout设计
    下交叉综合症
    fileloder.js+struts2实现文件异步上传,无页面刷新效果。
    将mysql中的Blob的图片在jsp中显示
    详解CSS样式的position属性
    Struts2与Spring的整合
    Play Framework常用标签list,set,如何遍历list、map类型数据
    我所理解的团队
  • 原文地址:https://www.cnblogs.com/ljs-666/p/8612760.html
Copyright © 2011-2022 走看看