Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64439dd

Browse files
cpp solution 1
1 parent 075d53e commit 64439dd

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
*.sln
77
obj
88
bin
9+
*.exe
10+
.vscode

‎algorithm-1/C++/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
Katkıda bulunmak istiyorsanız, lütfen aşağıdaki adımları takip edin:
44

5-
İlgili algoritma için çözümünüzü repo içerisinde kaç tane çözüm varsa ona göre isimlendirin. Eğer 2 adet çözüm varsa kendi çözümünüzü "solution-3.cs" şeklinde kaydedebilirsiniz.
5+
İlgili algoritma için çözümünüzü repo içerisinde kaç tane çözüm varsa ona göre isimlendirin. Eğer 2 adet çözüm varsa kendi çözümünüzü "solution-3.cpp" şeklinde kaydedebilirsiniz.
66
Bu numaralandırma şekliyle çözümleri daha derli toplu tutmayı hedefliyoruz. Farklı isimlendirmeler ile gönderilen PR 'lar otomatik olarak reddedilecektir.

‎algorithm-1/C++/solution-1.cpp‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
Usage: g++ -std=c++20 -Ofast .\solution-1.cpp -o solution-1.exe && .\solution-1.exe
3+
After that write the initial board to the terminal.
4+
*/
5+
6+
#include <algorithm>
7+
#include <array>
8+
#include <bitset>
9+
#include <iostream>
10+
#include <memory>
11+
#include <numeric>
12+
#include <queue>
13+
#include <unordered_set>
14+
15+
const std::array<std::bitset<4>, 6> possible_directions = {0b0110, 0b1110, 0b1100, 0b0011, 0b1011, 0b1001}; // left, bottom, right, top
16+
const std::array<int8_t, 4> direction_offset = {-3, 1, 3, -1}; // top, right, bottom, left
17+
const uint32_t winning_hash = 0b001010011100101000; // { 1, 2, 3, 4, 5, 0 } (3 bit groups)
18+
19+
class Board {
20+
private:
21+
std::array<uint8_t, 6> tiles;
22+
uint8_t empty_location;
23+
uint8_t current_direction = 0;
24+
uint32_t depth = 0;
25+
26+
public:
27+
Board(std::array<uint8_t, 6> tiles): tiles{tiles} {
28+
empty_location = std::distance(
29+
tiles.begin(),
30+
std::find(tiles.begin(), tiles.end(), 0)
31+
);
32+
}
33+
34+
std::shared_ptr<Board> next_move() {
35+
if (current_direction > 3) return nullptr;
36+
37+
auto directions = possible_directions[empty_location];
38+
39+
if (!directions.test(current_direction)) {
40+
current_direction++;
41+
return this->next_move();
42+
}
43+
44+
uint8_t tile = empty_location + direction_offset[current_direction++];
45+
46+
return this->swap(
47+
tile,
48+
empty_location
49+
);
50+
}
51+
52+
uint32_t get_hash() const {
53+
return std::accumulate(
54+
tiles.begin(),
55+
tiles.end(),
56+
(uint32_t)0,
57+
[](uint32_t hash, uint8_t tile) { return (hash << 3) | tile; }
58+
);
59+
}
60+
61+
uint32_t get_depth() const {
62+
return depth;
63+
}
64+
65+
private:
66+
Board(std::array<uint8_t, 6> tiles, uint32_t depth): Board(tiles) {
67+
this->depth = depth;
68+
}
69+
70+
std::shared_ptr<Board> swap(uint8_t tile, uint8_t empty) {
71+
std::array<uint8_t, 6> new_state = tiles;
72+
std::swap(new_state[tile], new_state[empty]);
73+
return std::make_shared<Board>(Board(new_state, depth + 1));
74+
}
75+
};
76+
77+
class Solution {
78+
private:
79+
std::unordered_set<uint32_t> previous_boards;
80+
std::queue<std::shared_ptr<Board>> bfs_queue;
81+
82+
public:
83+
Solution() { }
84+
85+
int64_t solve(std::shared_ptr<Board> board) {
86+
if (board->get_hash() == winning_hash) return 0;
87+
88+
bfs_queue = std::queue<std::shared_ptr<Board>>({board});
89+
90+
while (!bfs_queue.empty()) {
91+
auto front = bfs_queue.front();
92+
93+
auto next_board = front->next_move();
94+
95+
if (next_board == nullptr) {
96+
bfs_queue.pop();
97+
continue;
98+
}
99+
100+
auto hash = next_board->get_hash();
101+
102+
if (hash == winning_hash) return next_board->get_depth();
103+
104+
if (previous_boards.contains(hash)) continue;
105+
106+
previous_boards.insert(hash);
107+
bfs_queue.push(next_board);
108+
}
109+
110+
return -1;
111+
}
112+
};
113+
114+
std::array<uint8_t, 6> parse(std::string str_board) {
115+
uint8_t index = 0;
116+
return std::accumulate(
117+
str_board.begin(),
118+
str_board.end(),
119+
std::array<uint8_t, 6>(),
120+
[index](std::array<uint8_t, 6> tiles, char ch) mutable {
121+
if (isdigit(ch)) tiles[index++] = ch - '0';
122+
return tiles;
123+
}
124+
);
125+
}
126+
127+
void solve(std::string str_board) {
128+
auto tiles = parse(str_board);
129+
auto board = std::make_shared<Board>(Board(tiles));
130+
auto solution = Solution();
131+
std::cout << solution.solve(board) << "\n";
132+
}
133+
134+
int main(void) {
135+
std::string board;
136+
std::cin >> board;
137+
solve(board);
138+
}

‎algorithm-1/text.txt‎

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /