-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPuzzleGen.cpp
More file actions
128 lines (122 loc) · 3.2 KB
/
BPuzzleGen.cpp
File metadata and controls
128 lines (122 loc) · 3.2 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
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
//
// Created by WADwi on 2021/3/7.
//
#include "BPuzzleGen.h"
#include <random>
#include <time.h>
int BPuzzleGen::SIZE = 1;
BPuzzleGen::BPuzzleGen(const char *filename, int size) {
SIZE = size;
FILE * fp;
int temp;
if((fp = fopen(filename, "r")) == nullptr){
printf("error occurred while opening file %s", filename);
exit(-1);
}
table = (Value **)calloc(sizeof(Value *), SIZE);
for(int x = 0 ; x < SIZE; x++){
table[x] = (Value *)calloc(sizeof(Value), SIZE);
for(int y = 0; y < SIZE; y++){
fscanf(fp, "%d", &temp);
table[x][y] = temp == 0 ? negative : positive;
}
}
}
Status BPuzzleGen::generate() {
int map[SIZE * SIZE];
int dis_map[SIZE * SIZE];
int count = SIZE * SIZE;
int min = SIZE * SIZE * 0.3;
auto *seed = (time_t*)calloc(sizeof(time_t), 1);
auto _try = (int **)calloc(sizeof(int * ), count + 1);
for(int i = 0; i < count + 1; i++)
_try[i] = (int *)calloc(sizeof(int), SIZE * SIZE);
time(seed);
srand(*seed);
for(int i = 0; i < SIZE * SIZE; i++)
map[i] = i;
int *disable = (int *)calloc(sizeof(int), count + 1);
while(count >= min){
int i = rand() % count;
int index = map[i];
if(disable[count] == count){
memset(_try[count], 0, sizeof(int) * SIZE * SIZE);
disable[count] = 0;
map[count] = abs(dis_map[SIZE * SIZE - count - 1]);
table[map[count] / SIZE][map[count] % SIZE] = dis_map[SIZE * SIZE - count - 1] > 0 ? positive : negative;
dis_map[SIZE * SIZE - count - 1] = 0;
++count;
// print();
// fflush(stdout);
}
if(_try[count][index] == 1)
continue;
table[index / SIZE][index % SIZE] = table[index / SIZE][index % SIZE] == positive ? negative : positive;
if(only_check() != unholdable){
if(_try[count][index] == 0){
_try[count][index] = 1;
++disable[count];
// printf("%d-%d\n", count, disable[count]);
// fflush(stdout);
}
table[index / SIZE][index % SIZE] = table[index / SIZE][index % SIZE] == positive ? negative : positive;
}else{
if(_try[count][index] == 0){
_try[count][index] = 1;
++disable[count];
}
dis_map[SIZE * SIZE - count] = map[i] * (table[index / SIZE][index % SIZE] == positive ? -1 : 1);
for(int x = i + 1; x < count; x++){
map[x - 1] = map[x];
}
--count;
table[index / SIZE][index % SIZE] = undefined;
// print();
// fflush(stdout);
}
}
free(seed);
return done;
}
Status BPuzzleGen::only_check() {
solver = new BPuzzle(table, SIZE);
Status temp = solver->solve();
delete solver;
return temp;
}
Status BPuzzleGen::print() {
for(int x = 0; x < SIZE; x++){
for(int y = 0; y < SIZE; y++){
if(table[x][y] == positive)
printf("1 ");
else if(table[x][y] == negative)
printf("0 ");
else
printf("_ ");
}
printf("\n");
}
return done;
}
Status BPuzzleGen::fsave(const char *filename) {
FILE * fp;
if((fp = fopen(filename, "w")) == nullptr){
printf("error occurred while opening file %s", filename);
exit(-1);
}
for(int x = 0; x < SIZE; x++){
for(int y = 0; y < SIZE; y++){
if(table[x][y] == positive)
fprintf(fp, "%d %d 1\n", x+1, y+1);
else if(table[x][y] == negative)
fprintf(fp, "%d %d 0\n", x+1, y+1);
}
}
fclose(fp);
return done;
}
BPuzzleGen::~BPuzzleGen() {
for(int i = 0; i < SIZE; i++)
delete table[i];
delete table;
}