JAVA生成一个二维数组,使中间元素不与相邻的9个元素相等,并限制每一个元素的个数

JAVA生成一个二维数组,使中间元素不与相邻的9个元素相等,并限制每一个元素的个数

示例如下

至少需要九个元素:"A","B","C","D","E","F","G","H","I"

我们打印一个30*15的二维数组

刚好限制每一个元素出现50次

I    D    H    A    C    F    E    G    B    E    F    C    B    I    A    
G    A    E    D    H    I    B    F    H    G    D    G    H    C    E    
D    F    I    B    C    A    C    G    D    B    I    A    E    F    H    
A    H    C    G    I    D    B    E    F    E    H    G    B    C    D    
F    I    A    F    C    A    G    I    H    D    B    E    A    I    F    
D    E    H    G    B    E    C    A    F    I    G    H    D    C    B    
F    B    C    I    D    H    G    E    G    A    E    C    I    A    H    
D    H    F    B    F    C    I    A    B    D    G    H    E    D    I    
C    E    A    G    H    B    F    C    E    F    I    B    G    A    F    
D    G    I    B    A    C    D    H    B    G    E    D    H    C    I    
E    A    F    C    H    F    E    G    I    D    A    B    G    A    E    
I    C    H    D    B    G    C    F    E    H    F    D    I    B    C    
A    D    E    F    H    A    I    G    B    C    A    H    G    D    E    
I    F    B    A    C    G    F    H    E    I    D    B    A    H    B    
D    G    I    E    F    B    C    D    G    A    H    I    E    C    F    
H    C    B    G    D    I    F    E    F    B    D    A    G    I    H    
A    E    F    C    E    H    B    C    I    G    I    H    D    A    C    
F    G    B    D    A    C    E    F    D    B    E    A    G    I    H    
C    H    I    F    B    D    A    G    E    F    D    I    B    E    G    
A    G    C    H    I    F    C    D    H    B    E    A    H    D    B    
E    I    F    A    G    B    I    G    C    A    C    F    E    A    H    
D    B    G    D    I    F    E    H    F    I    E    B    C    G    D    
A    C    H    B    A    H    D    I    E    G    C    F    D    B    A    
F    I    E    C    G    B    E    H    C    D    A    G    H    F    I    
D    H    G    I    F    C    A    B    E    F    E    B    I    G    D    
A    C    A    H    G    B    D    F    C    H    I    F    E    C    A    
I    H    G    B    E    C    A    B    D    F    G    D    H    I    E    
A    D    C    H    I    F    E    G    C    B    I    A    G    B    D    
E    H    F    E    A    C    B    D    F    H    G    H    I    E    G    
C    A    D    B    I    F    H    C    G    I    F    E    B    A    D    
A_50    B_50    C_50    D_50    E_50    F_50    G_50    H_50    I_50    

 

 

代码如下:

package com.lc.array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;

/**
 * 生成数组
 * @author Cheng
 */
public class MatrixManage {
    private Integer hight = 30;
    private Integer wide = 15;
    private Integer max = 50;
    /** 每个元素出现的个数  **/
    private TreeMap<String, Integer> keyNumMap = new TreeMap<>();
    /** 目标数组 **/
    private String[][] arr = new String[hight][wide];
    /** 每个点 可使用的元素集合 **/
    private Map<String,Set<String>> pointMap = new TreeMap<>();
    
    public MatrixManage(String[] keys, Integer hight, Integer wide, Integer max) {
        
        if((hight*wide)>max*(keys.length)){
            System.out.println("二逼,("+hight+"*" + wide + ")大于("+max+"*"+keys.length+")了,还玩毛");
            return;
        }
        
        this.hight = hight;
        this.wide = wide;
        this.max = max;
        this.arr = new String[hight][wide];
        
        for(String key :keys){
            keyNumMap.put(key, 0);
        }
    }

    private void addKeyNumMap(String key){
        keyNumMap.put(key, keyNumMap.get(key)+1);
    }
    
    private void subtractKeyNumMap(String key){
        keyNumMap.put(key, keyNumMap.get(key)-1);
    }
    
    public static void main(String[] args) {
        MatrixManage entity = new MatrixManage(new String[]{"A","B","C","D","E","F","G","H","I"},30,15,50);
        entity.print();
    }

    private void print() {
        
        for(int i=0;i<hight;i++){
            
            for(int j=0;j<wide;j++){
                
                while(true){
                    
                    String pointKey = i + "_" + j;//点的key
                    
                    
                    if(pointMap.containsKey(pointKey)){// 是否存储过该点 
                        
                        Set<String> pointSet = pointMap.get(pointKey);//获取该点可用的元素集合
                        
                        subtractKeyNumMap(arr[i][j]); //更新元素的数量
                        
                        pointSet.remove(arr[i][j]);   //删除目前的元素
                        
                        if(pointSet.isEmpty()){//该点没有可用的元素
                            
                            pointMap.remove(pointKey);//删除该点、后退
                            
                            if(j==0){
                                i--;
                                j=wide-1;
                            }else{
                                j--;
                            }
                            
                        }else{
                            
                            TreeMap<Integer, List<String>> usableMap = getUsableMap(pointSet, false);
                            
                            if(usableMap.isEmpty()){//该点没有可用的元素
                                
                                pointMap.remove(pointKey);//删除该点、后退
                                
                                arr[i][j]=null;
                                if(j==0){
                                    i--;
                                    j=wide-1;
                                }else{
                                    j--;
                                }
                            }else{
                                arr[i][j] = getKey(usableMap);
                                break;
                            }
                        }
                        
                    }else{
                        
                        Set<String> set = getRoundSet(i, j);//(右上方4个)环绕的数组集合
                        
                        TreeMap<Integer, List<String>> usableMap = getUsableMap(set, true);
                        
                        if(usableMap.isEmpty()){

                            if(j==0){
                                i--;
                                j=wide-1;
                            }else{
                                j--;
                            }
                            
                        }else{
                            
                            Set<String> tempSet = new HashSet<>();
                            for(List<String> l:usableMap.values()){
                                tempSet.addAll(l);
                            }
                            
                            arr[i][j] = getKey(usableMap);
                            
                            tempSet.remove(arr[i][j]);
                            pointMap.put(pointKey, tempSet);
                            break;
                        }
                    }
                }
                
                //修改元素的数量
                addKeyNumMap(arr[i][j]);
            }
        }
        printArr();
        printKeyNum();
    }
    
    /**
     * 获取key
     * @param usableMap
     * @return
     */
    private String getKey(TreeMap<Integer, List<String>> usableMap) {
        Map.Entry<Integer,List<String>> entry = usableMap.firstEntry();
        Random random = new Random();
        int s = random.nextInt(entry.getValue().size());
        return entry.getValue().get(s);
    }
    
    /**
     * 获取可用集合
     * @param treeMap
     * @param set
     * @param b 1、true set包含 2、 false set不包含
     * @return
     */
    private TreeMap<Integer, List<String>> getUsableMap(Set<String> set,boolean b) {
        TreeMap<Integer,List<String>> usableMap = new TreeMap<>();
        for(Map.Entry<String, Integer> entry:keyNumMap.entrySet()){
            if(entry.getValue() < max){
                if((b==true && !set.contains(entry.getKey())) || (b==false && set.contains(entry.getKey()))){
                    if(usableMap.get(entry.getValue())==null){
                        usableMap.put(entry.getValue(),new ArrayList<>());
                    }
                    usableMap.get(entry.getValue()).add(entry.getKey());
                }
            }
        }
        return usableMap;
    }
    
    /**
     * 获取周围的元素集合
     * @param i
     * @param j
     * @return
     */
    private Set<String> getRoundSet(int i, int j) {
        Set<String> set = new HashSet<>();
        for(int x=1;x>=0;x--){
            if((i-x)>=0){
                for(int y=-1;y<2;y++){
                    
                    if(x==0 && y==0){
                        break;
                    }
                    
                    if((j+y)>=0 && (j+y)<wide){
                        set.add(arr[i-x][j+y]);
                    }
                }
            }
        }
        return set;
    }
    
    /**
     * 打印数组
     */
    private void printArr() {
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
    
    /**
     * 打印数组数
     */
    private void printKeyNum() {
        for(Map.Entry<String, Integer> entry:keyNumMap.entrySet()){
            System.out.print(entry.getKey()+"_"+entry.getValue()+"\t");
        }
    }

}

注:支持后退操作,解决了不符合条件的情况

 

posted @ 2018-01-17 11:53  图生  阅读(643)  评论(0编辑  收藏  举报