C Program
C Program
#include <vector>
#include <thread>
#include <mutex>
#include <memory>
#include <condition_variable>
#define P 9
class X {
std::vector<std::vector<int>> z;
std::mutex m;
std::condition_variable cv;
bool solved;
public:
X(std::vector<std::vector<int>> w) : z(w), solved(false) {}
void f() {
for (const auto& g : z) {
for (int h : g) std::cout << h << " ";
std::cout << "\n";
}
}
void k(int n) {
std::unique_lock<std::mutex> lock(m);
for (int i = 0; i < P; ++i) {
for (int j = 0; j < P; ++j) {
if (z[i][j] == 0) {
for (int s = 1; s <= P; ++s) {
if (d(i, j, s)) {
z[i][j] = s;
if (e(i, j + 1)) {
solved = true;
cv.notify_all();
f();
return;
}
z[i][j] = 0;
}
}
return;
}
}
}
cv.notify_all();
}
void solve() {
std::vector<std::thread> threads;
for (int i = 0; i < 4; ++i) {
threads.push_back(std::thread(&X::k, this, i));
}
int main() {
std::vector<std::vector<int>> y = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
std::unique_ptr<X> x = std::make_unique<X>(y);
x->solve();
return 0;
}