Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (73 loc) · 2.27 KB
/
Copy pathmain.cpp
File metadata and controls
88 lines (73 loc) · 2.27 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
#include "sokoban.h"
// Global variable definitions
GridBitset wall_bitset;
GridBitset target_bitset;
GridBitset fragile_bitset;
int rows, cols;
vector<int> target_list;
vector<vector<int>> dist_to_target;
int main(int argc, char* argv[]) {
auto start_time = chrono::high_resolution_clock::now();
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <input_file>\n";
return 1;
}
ifstream infile(argv[1]);
if (!infile.is_open()) {
cerr << "Error: cannot open file " << argv[1] << "\n";
return 1;
}
string line;
State initial_state;
initial_state.boxes.reset();
fragile_bitset.reset();
int r = 0;
size_t max_len = 0;
vector<string> raw_lines;
while (getline(infile, line)) {
if (!line.empty() && line.back() == '\r') line.pop_back();
raw_lines.push_back(line);
if (line.size() > max_len) max_len = line.size();
r++;
}
infile.close();
if (raw_lines.empty()) {
cerr << "Error: empty board\n";
return 1;
}
cols = (int)max_len;
rows = (int)raw_lines.size();
wall_bitset.reset();
target_bitset.reset();
for (int i = 0; i < rows; ++i) {
const string& line = raw_lines[i];
for (int c = 0; c < cols; ++c) {
char ch = (c < line.size()) ? line[c] : '#';
int idx = i * cols + c;
if (ch == '#') {
wall_bitset[idx] = 1;
}
if (ch == 'o' || ch == 'O' || ch == '!') {
initial_state.player_idx = idx;
}
if (ch == 'x' || ch == 'X') {
initial_state.boxes[idx] = 1;
}
if (ch == '.' || ch == 'X' || ch == 'O') {
target_bitset[idx] = 1;
target_list.push_back(idx);
}
if (ch == '@' || ch == '!') {
fragile_bitset[idx] = 1;
}
}
}
mark_unreachable_as_fragile();
precompute_distances();
string solution = AStar(initial_state);
auto end_time = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end_time - start_time);
// cerr << "Total time: " << duration.count() << " ms" << endl;
cout << solution << endl;
return 0;
}