-
Notifications
You must be signed in to change notification settings - Fork 4
/
Node.h
218 lines (184 loc) · 4.53 KB
/
Node.h
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Created by subangkar on 11/25/18.
//
#ifndef NPUZZLE_NODE_H
#define NPUZZLE_NODE_H
#define BOARD_SQ_SIZE 3
#define PRINT_W 3
//#include<bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define RIGHT 0
#define LEFT 1
#define DOWN 2
#define UP 3
typedef int direction_t;
typedef int8_t puzzle_t;
using namespace std;
int dirX[4] = {0, 0, 1, -1}; // RIGHT-LEFT-DOWN-UP
int dirY[4] = {1, -1, 0, 0}; // RIGHT-LEFT-DOWN-UP
class Node {
public:
puzzle_t **A = nullptr;
bool emptyNode = true;
static int boardSqSize;
friend ostream &operator<<(ostream &os, const Node &node);
Node() {
emptyNode = true;
A = new puzzle_t *[boardSqSize];
for (int i = 0; i < boardSqSize; ++i) {
A[i] = new puzzle_t[boardSqSize];
memset(A[i], 0, boardSqSize * sizeof(A[0][0]));
}
}
Node(const Node &node) {
this->~Node();
// emptyNode = false;
this->emptyNode = node.emptyNode;
A = new puzzle_t *[boardSqSize];
for (int i = 0; i < boardSqSize; ++i) {
A[i] = new puzzle_t[boardSqSize];
}
for (int i = 0; i < boardSqSize; i++) {
for (int j = 0; j < boardSqSize; j++) {
A[i][j] = node.A[i][j];
}
}
}
Node &operator=(const Node &node) {
this->~Node();
this->emptyNode = node.emptyNode;
A = new puzzle_t *[boardSqSize];
for (int i = 0; i < boardSqSize; ++i) {
A[i] = new puzzle_t[boardSqSize];
}
for (int i = 0; i < boardSqSize; i++) {
for (int j = 0; j < boardSqSize; j++) {
A[i][j] = node.A[i][j];
}
}
return *this;
}
~Node() {
if (A == nullptr) return;
for (int i = 0; i < boardSqSize; ++i) {
delete A[i];
}
delete[] A;
A = nullptr;
}
bool operator==(const Node &right) const {
for (int i = 0; i < boardSqSize; i++)
for (int j = 0; j < boardSqSize; j++)
if (A[i][j] != right.A[i][j]) return false;
return true;
}
bool operator!=(const Node &right) const {
return !(*this == right);
}
bool operator<(const Node &right) const {
for (int i = 0; i < boardSqSize; i++) {
for (int j = 0; j < boardSqSize; j++) {
if (A[i][j] < right.A[i][j]) return true;
else if (A[i][j] == right.A[i][j]) continue;
else return false;
}
}
return false;
}
bool isSolveAble() {
int blank_row_no = -1;
vector<int> arr;
for (int i = 0; i < boardSqSize; i++)
for (int j = 0; j < boardSqSize; j++) {
if (A[i][j])
arr.push_back(A[i][j]);
else
blank_row_no = i;
}
int invCount = getInvCount(arr);
bool boardSizeOdd = static_cast<bool>(boardSqSize & 1);
// cout << boardSizeOdd << " " << blank_row_no << " " << invCount << endl;
if (boardSizeOdd && !(invCount & 1)) // odd-board & even-inversions
return true;
else if (!boardSizeOdd && ((blank_row_no + getInvCount(arr)) & 1)) // even-board & odd-sum
return true;
return false;
}
static int getInvCount(const vector<int> &arr) {
int inv_count = 0;
for (int i = 0; i < arr.size() - 1; i++)
for (int j = i + 1; j < arr.size(); j++)
if (arr[i] > arr[j])
inv_count++;
return inv_count;
}
// not works donno why not
Node getNode(int direction, int zX = -1, int zY = -1) {
if (A == nullptr || direction > 3)
return *this;
if (zX == -1 || zY == -1) {
if (!getZeroPos(*this, zX, zY))
return Node();
}
int zXnew = zX + dirX[direction];
int zYnew = zY + dirY[direction];
if (zXnew < 0 || zYnew < 0 || zXnew >= Node::boardSqSize || zYnew >= Node::boardSqSize)
return Node();
Node v = *this;
// cout << v;
swap(v.A[zX][zY], v.A[zXnew][zYnew]);
return v;
}
static bool getZeroPos(const Node &node, int &zX, int &zY) {
zX = zY = -1;
for (int i = 0; i < Node::boardSqSize; i++) {
for (int j = 0; j < Node::boardSqSize; j++)
if (!node.A[i][j]) {
zX = i, zY = j;
return true;
}
}
return false;
}
static int oppositeDirection(int direction) {
switch (direction) {
case LEFT:
return RIGHT;
case RIGHT:
return LEFT;
case UP:
return DOWN;
case DOWN:
return UP;
default:
return EOF;
}
}
bool isEmptyNode() const {
return emptyNode;
}
};
int Node::boardSqSize = 0;
ostream &operator<<(ostream &os, const Node &node) {
if (!node.A) return os;
for (int i = 0; i < Node::boardSqSize; i++) {
for (int j = 0; j < Node::boardSqSize; j++)
if (node.A[i][j])
os << setw(PRINT_W) << (static_cast<int>(node.A[i][j])) << " ";
else
os << setw(PRINT_W) << " " << " ";
os << endl;
}
os << " ----------- " << std::endl;
return os;
}
#endif //NPUZZLE_NODE_H