Summary Files Commits Branches @commit @diff @stats
path: root / Year 4 / Artificial Intelligence / hw01 / main.cpp
blob: 46f2c6d898a199de75e7a9630bbda0b9f274773e (plain) (blame)
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
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <math.h>
#include <climits>
#include <cassert>

// #include "graph.h"

using namespace std;

int I, N = 0;

struct Node {
	vector<int> board;
	string change;
	int zero;

	int currentDepth;
	int remainingDepth;

	void update() {
		this->currentDepth++;
		this->remainingDepth = this->manhattan_dist();
	}

	Node& operator=(const Node& other) {
		if (this != &other) {
			this->change = other.change;
			this->board = other.board;
			this->zero = other.zero;
			this->currentDepth = other.currentDepth;
			this->remainingDepth = other.remainingDepth;
		}
		return *this;
	}

	bool solved() {
		return this->remainingDepth == 0;
	}

	int depth() {
		return this->currentDepth + this->remainingDepth;
	}

	bool equals_any_in(vector<Node*>& arr) {
		for (int i = 0; i < arr.size(); i++) {
			if (*arr[i] == *this) {
				return true;
			}
		}
		return false;
	}

	friend bool operator==(const Node& left, const Node& right) {
		return left.board == right.board;
	}

	void find_zero() {
		for (this->zero = 0; this->zero < this->board.size(); this->zero++) {
			if (this->board[this->zero] == 0) return;
		}
	}

private:
	pair<int, int> to_2D(int i) {
		return { i / N, i % N };
	}
	int manhattan_dist() {
		int total = 0;
		for (int i = 0; i < this->board.size(); i++) {
			if (this->board[i] == 0) continue;

			auto curr = to_2D(i);
			auto goal = to_2D(this->board[i] - 1 + (this->board[i] > I));
			total += abs(curr.first - goal.first) + abs(curr.second - goal.second);
		}
		return total;
	}
};

#define SOLVED -1

int dfs(vector<Node*>& path, int maxDepth) {
	Node* i = path.back();

	if (i->solved()) return SOLVED;
	if (i->depth() > maxDepth) return i->depth();

	int min = INT_MAX;
	for (int dir = 0; dir < 4; dir++) {
		Node* j = new Node(*i);

		switch (dir) {
			case 0: // left
				if (j->zero % N == 0) {
					delete j;
					continue;
				}
				swap(j->board[j->zero - 1], j->board[j->zero]);
				j->change = "right";
				j->zero--;
				break;
			case 1: // right
				if (j->zero % N == N - 1) {
					delete j;
					continue;
				}
				swap(j->board[j->zero + 1], j->board[j->zero]);
				j->change = "left";
				j->zero++;
				break;
			case 2: // up
				if (j->zero / N == 0) {
					delete j;
					continue;
				}
				swap(j->board[j->zero - N], j->board[j->zero]);
				j->change = "down";
				j->zero -= N;
				break;
			case 3: // down
				if (j->zero / N == N - 1) {
					delete j;
					continue;
				}
				swap(j->board[j->zero + N], j->board[j->zero]);
				j->change = "up";
				j->zero += N;
				break;
		}
		j->update();

		// Don't go back a step
		if (j->equals_any_in(path)) {
			delete j;
			continue;
		}

		path.push_back(j);
		int val = dfs(path, maxDepth);

		if (val == SOLVED) return SOLVED;
		if (val < min) min = val;

		path.pop_back();
		delete j;
	}
	return min;
}

bool solvable(Node* root) {
	vector<int> &board = root->board;

	int inversions = 0;
	for (int i = 0; i < board.size(); i++) {
		if (board[i] == 0) continue;
		for (int j = i; j < board.size(); j++) {
			if (board[j] == 0) continue;
			inversions += board[i] > board[j];
		}
	}

	if (board.size() % 2 == 0) {
		inversions += root->zero % N;
		return inversions % 2 == 1;
	}
	else {
		return inversions % 2 == 0;
	}
}

int main() {
	/* Input */
	cin >> N >> I;
	if (I == -1) { I = N; }
	N++;

	Node* root = new Node;
	int temp;
	for (int i = 0; i < N; i++) {
		cin >> temp;
		root->board.push_back(temp);
	}
	N = sqrt(N + 1);

	/* Prepare */
	root->find_zero();
	root->update();
	root->currentDepth = 0;

	if (!solvable(root)) {
		std::cout << -1 << std::endl;
		delete root;
		return 0;
	}

	vector<Node*> path;
	path.push_back(root);

	/* IDA* */
	int bound = 1;
	while(bound != SOLVED) {
		bound = dfs(path, bound);
	}

	/* Output */
	cout << (path.size() - 1) << endl;
	for (int i = 1; i < path.size(); i++) {
		cout << path[i]->change << endl;
		delete path[i];
	}
	delete root;
}