StackMatrix.java
2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.huaheng.api.wcs.domain;
import java.util.*;
import java.util.function.Function;
/***
* @author tongzonghao
*
*/
public class StackMatrix {
private int[][] matr;
private int row;
private int column;
public StackMatrix(int row, int column) {
this.row = row;
this.column = column;
matr = new int[row][column];
}
public void setForEach(Function<Integer, Integer> funtion) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matr[i][j] = funtion.apply(matr[i][j]);
}
}
}
/**
* 从高位向下取 位置号
*
* @param columnIdx
* @param rowIdx
* @return
*/
public List<Integer> getColumnValue(int rowIdx, int columnIdx) {
Set<Integer> columnValues = new HashSet<>();
if (columnIdx < 0 || columnIdx > this.column) {
return null;
}
for (int i = this.row - 1; i >= rowIdx; i--) {
columnValues.add(matr[i][columnIdx]);
}
columnValues.remove(new Integer(0));
return new ArrayList<>(columnValues);
}
public Map<String, Integer> searchValueByFullMatr(int target) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (target == matr[i][j]) {
map.put("row", i);
map.put("column", j);
return map;
}
}
}
return null;
}
public StackMatrix setMatr(int[][] matr) {
this.matr = matr;
return this;
}
public int[][] getMatr() {
return matr;
}
public int getEmptyPosition() {
int minPosition = 0;
int num = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
num++;
if (matr[i][j] == 0) {
return num;
}
}
}
return minPosition;
}
}