-
Notifications
You must be signed in to change notification settings - Fork 0
/
fleas.java
86 lines (82 loc) · 1.85 KB
/
fleas.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
public class fleas {
static double[][] TotalMatrix = new double[32][32];
static double[][] currentFlea = new double[32][32];
static double[][] currentFleaTemp2 = new double[32][32];
public static void main(String[] args){
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
TotalMatrix[k][i] = 1;
}
}
for(int a = 1; a<31;a++){
for (int b = 1; b<31;b++){
currentFlea = new double[32][32];
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
currentFlea[k][i] = 1;
}
}
currentFlea[a][b]=0;
for(int k = 0; k < 50; k++){
move();
}
combine();
}
}
printb(TotalMatrix);
}
public static void combine(){
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
TotalMatrix[k][i] *= currentFlea[k][i];
}
}
}
public static void move(){
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
currentFleaTemp2[k][i] = 0;
}
}
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
change(k,i);
}
}
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
currentFlea[k][i] -= currentFleaTemp2[k][i];
}
}
}
public static void change(int a, int b){
double place = currentFlea[a][b];
if(place==1)
return;
int count = 0;
if (TotalMatrix[a-1][b]==0)
count++;
if (TotalMatrix[a+1][b]==0)
count++;
if (TotalMatrix[a][b+1]==0)
count++;
if (TotalMatrix[a][b-1]==0)
count++;
double p = 1/(4.0-count);
currentFleaTemp2[a+1][b] += (1-place)*p;
currentFleaTemp2[a-1][b] += (1-place)*p;
currentFleaTemp2[a][b+1] += (1-place)*p;
currentFleaTemp2[a][b-1] += (1-place)*p;
currentFlea[a][b] = 1;
}
public static void printb(double[][] a){
double tot = 0;
for (int k = 1; k < 31; k ++){
for (int i = 1; i < 31; i++){
double temp = a[k][i];
tot+=temp;
}
}
System.out.println(tot);
}
}