-
Notifications
You must be signed in to change notification settings - Fork 0
/
dp.java
148 lines (129 loc) · 6.23 KB
/
dp.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.*;
public class dp {
public static int max(int[] lst){
int max = -1;
for (int i=0;i<lst.length ;i++ ) {
if(lst[i]>max){
max = lst[i];
}
}
return max;
}
public static int mex(int[] lst) {
int[] appearances = new int[max(lst)+2];
for (int i=0;i<lst.length ;i++ ) {
appearances[lst[i]]+=1;
}
for (int i=0; i<appearances.length ;i++ ) {
if(appearances[i]==0){
return i;
}
}
System.out.println("L y propocion");
return -1;
}
public static int[][][] returnPieces(int d) {
if(d==2){
int[][][] pieces = {{{0,0},{1,0}},{{0,0},{0,1}}};
return pieces;
}
// if(d==3){
// return [[[0,0],[1,0],[2,0]],[[0,0],[0,1],[0,2]], [[0,0],[1,0],[1,1]],[[0,0],[1,0],[0,1]],[[0,0],[0,1],[1,1]],[[0,0],[0,1],[-1,1]]];
// }
if(d==5){
pentomino worker = new pentomino(5);
return worker.pentomino_pieces();
// return [[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]], [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1]], [[0, 0], [0, 1], [1, 0], [2, 0], [3, 0]], [[0, 0], [0, 1], [0, 2], [1, 2], [0, 3]], [[0, 0], [0, 1], [1, 1], [0, 2], [0, 3]], [[0, 0], [0, 1], [1, 0], [0, 2], [2, 0]], [[0, 0], [1, 0], [2, 0], [1, 1], [2, 1]], [[0, 0], [1, 0], [2, 0], [0, 1], [1, 1]], [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]], [[0, 0], [1, 0], [0, 1], [0, 2], [0, 3]], [[0, 0], [0, 1], [0, 2], [0, 3], [1, 3]], [[0, 0], [1, 0], [0, 1], [0, 2], [1, 2]], [[0, 0], [0, 1], [0, 2], [1, 2], [0, 3]], [[0, 0], [0, 1], [0, 2], [-1, 2], [0, 3]], [[0, 0], [0, 1], [1, 1], [0, 2], [0, 3]], [[0, 0], [0, 1], [-1, 1], [0, 2], [0, 3]], [[0, 0], [1, 0], [2, 0], [3, 0], [2, -1]], [[0, 0], [1, 0], [1, -1], [2, 0], [3, 0]], [[0, 0], [1, 0], [0, 1], [1, 1], [0, 2]], [[0, 0], [1, 0], [0, 1], [1, 1], [1, 2]], [[0, 0], [0, 1], [1, 1], [2, 1], [2, 0]], [[0, 0], [1, 0], [1, 1], [1, 2], [0, 2]], [[0, 0], [1, 0], [1, 1], [2, 1], [2, 2]], [[0, 0], [1, 0], [0, 1], [-1, 1], [-1, 2]], [[0, 0], [0, 1], [1, 1], [1, 2], [2, 2]], [[0, 0], [0, 1], [-1, 1], [-1, 2], [-2, 2]], [[0, 0], [0, 1], [-1, 1], [0, 2], [1, 2]], [[0, 0], [0, 1], [1, 1], [0, 2], [-1, 2]], [[0, 0], [1, 0], [2, 0], [1, 1], [2, -1]], [[0, 0], [0, 1], [1, 1], [2, 1], [1, 2]], [[0, 0], [0, 1], [1, 1], [2, 1], [2, 2]], [[0, 0], [0, 1], [-1, 1], [-2, 1], [-2, 2]], [[0, 0], [1, 0], [2, 0], [2, 1], [2, -1]], [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2]], [[0, 0], [0, 1], [0, 2], [-1, 2], [1, 2]], [[0, 0], [1, 0], [2, 0], [1, 1], [1, 2]], [[0, 0], [0, 1], [-1, 1], [1, 1], [1, 2]], [[0, 0], [0, 1], [1, 1], [1, 2], [1, 3]], [[0, 0], [0, 1], [-1, 1], [-1, 2], [-1, 3]], [[0, 0], [1, 0], [1, -1], [2, -1], [3, -1]], [[0, 0], [1, 0], [2, 0], [2, 1], [3, 1]], [[0, 0], [1, 0], [2, 0], [2, -1], [3, -1]], [[0, 0], [1, 0], [1, 1], [2, 1], [3, 1]], [[0, 0], [0, 1], [0, 2], [-1, 2], [-1, 3]], [[0, 0], [0, 1], [0, 2], [1, 2], [1, 3]], [[0, 0], [1, 0], [0, 1], [0, 2], [-1, 2]], [[0, 0], [1, 0], [1, 1], [1, 2], [2, 2]], [[0, 0], [1, 0], [1, 1], [2, 1], [1, 2]], [[0, 0], [1, 0], [0, 1], [0, 2], [-1, 1]], [[0, 0], [0, 1], [1, 0], [2, 0], [1, -1]], [[0, 0], [1, 0], [1, -1], [2, 0], [2, 1]], [[0, 0], [0, 1], [0, 2], [0, 3], [-1, 3]], [[0, 0], [1, 0], [1, 1], [1, 2], [1, 3]], [[0, 0], [0, 1], [1, 1], [2, 1], [3, 1]], [[0, 0], [1, 0], [2, 0], [3, 0], [3, -1]], [[0, 0], [1, 0], [2, 0], [-1, 0], [-1, 1]], [[0, 0], [1, 0], [2, 0], [1, -1], [2, -1]], [[0, 0], [0, 1], [1, 1], [0, 2], [1, 2]], [[0, 0], [0, 1], [-1, 1], [0, 2], [-1, 2]], [[0, 0], [0, 1], [0,2 ], [1, 2], [2, 2]], [[0, 0], [0, 1], [0, 2], [-1, 2], [-2, 2]], [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]], [[0, 0], [0, 1], [0, 2], [1, 0], [2, 0]]];
}
int[][][] voiart = {{{0}}};
return voiart;
}
public static int BoardToInt(int[][] board,int a,int b) {
int val = 0;
for (int i=0;i<a ;i++ ) {
for (int j=0;j<b ;j++ ) {
val += board[i][j] * (Math.pow(2,b*i+j));
}
}
return val;
}
public static int hammingWeight(int o){
int sum = 0;
while(o>0) {
sum += o%2;
o = o/2;
}
return sum;
}
public static int dp(int a, int b, int d){
final long startTime = System.currentTimeMillis();
int[][][] gamemoves = returnPieces(d);
int[] dpvalues = new int[(int) Math.pow(2,a*b)];
for (int o=0;o<Math.pow(2,a*b) ;o++ ) {
int[][] board = new int[a][b];
int p = 10;
if((o % (Math.pow(2,a*b-p))) == 0){
System.out.println(o + " out of " + Math.pow(2,a*b) + " (out of " + Math.pow(2,p) + " this is " + o/Math.pow(2,a*b-p) + ")");
System.out.println("Total execution time so far (seconds): " + ((System.currentTimeMillis() - startTime))/1000.0);
}
if (((a*b-hammingWeight(o))%d)==0) {
// System.out.println(o + " is a plausible position");
int bitmask = o;
for (int i=0;i<a ;i++ ) {
for (int j=0;j<b ;j++ ) {
board[i][j] = bitmask %2;
// System.out.println("board[i][j]: " + board[i][j]);
bitmask = bitmask/2;
}
}
ArrayList<Integer> states = new ArrayList<>();
for (int m=0;m<gamemoves.length; m++ ) {
// considering the piece gamemove[m], which is a list of points.
for (int i=0;i<a ;i++ ) {
for (int j=0;j<b ;j++ ) {
int[][] move = new int[d][2];
for (int k=0;k<d ;k++ ) {
move[k][0] = i + gamemoves[m][k][0];
move[k][1] = j + gamemoves[m][k][1];
}
int[][] newboard = new int[a][b];
for (int x=0;x<a ;x++ ) {
for (int y=0;y<b ;y++ ) {
newboard[x][y] = board[x][y];
}
}
boolean valid = true;
for (int k=0;k<d; k++ ) {
if (0<= move[k][0] && move[k][0]<a && 0<=move[k][1] && (move[k][1]<b && newboard[move[k][0]][move[k][1]]==1)) {
newboard[move[k][0]][move[k][1]]=0;
}
else {
valid = false;
}
}
if(valid) {
states.add(dpvalues[BoardToInt(newboard,a,b)]);
}
}
}
}
// states.add(100);
// System.out.println(states);
int[] statesArray = new int[states.size()];
for (int i=0;i<states.size() ;i++ ) {
statesArray[i] = states.get(i);
}
dpvalues[o] = mex(statesArray);
}
// System.out.println(o + " " + dpvalues[o]);
}
return dpvalues[(int) Math.pow(2,a*b)-1];
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int a = console.nextInt();
int b = console.nextInt();
int d = console.nextInt();
System.out.println(dp(a,b,d));
}
}