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);  
        }  
    }
  • 相关阅读:
    HTML5结构
    HTML5新增的非主体元素header元素、footer元素、hgroup元素、adress元素
    CF GYM 100703G Game of numbers
    CF GYM 100703I Endeavor for perfection
    CF GYM 100703K Word order
    CF GYM 100703L Many questions
    CF GYM 100703M It's complicate
    HDU 5313 Bipartite Graph
    CF 560e Gerald and Giant Chess
    POJ 2479 Maximum sum
  • 原文地址:https://www.cnblogs.com/ljs-666/p/8612760.html
Copyright © 2011-2022 走看看