1. 程式人生 > >JAVA生成一個二維數組,使中間元素不與相鄰的9個元素相等,並限制每一個元素的個數

JAVA生成一個二維數組,使中間元素不與相鄰的9個元素相等,並限制每一個元素的個數

map 個數 nta nds put 可用 dom mov wid

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"); } } }

註:支持後退操作,解決了不符合條件的情況

JAVA生成一個二維數組,使中間元素不與相鄰的9個元素相等,並限制每一個元素的個數