Skip to content

Instantly share code, notes, and snippets.

@Mine02C4
Last active October 9, 2015 20:14
Show Gist options
  • Save Mine02C4/0ea408528c363a730b38 to your computer and use it in GitHub Desktop.
Save Mine02C4/0ea408528c363a730b38 to your computer and use it in GitHub Desktop.
情報数学概論だったり?
#include <iostream>
#include <vector>
#include <algorithm>
#include <mutex>
typedef int64_t table;
const int SIZE = 5;
const table BASE = 1;
const table N_BASE = -1;
const table NUMBER_OF_PATTERN = BASE << (SIZE * SIZE);
const table TABLE_MASK = ~(N_BASE << (SIZE * SIZE));
table RIGHT_MASK = 0;
table LEFT_MASK = 0;
table TOP_MASK = 0;
table BOTTOM_MASK = 0;
std::vector<table> results;
std::mutex mtx;
bool validate(table p) {
table table = TABLE_MASK;
table ^= p;
table ^= (p >> 1) & (~LEFT_MASK);
table ^= (p << 1) & (~RIGHT_MASK);
table ^= p << SIZE;
table ^= p >> SIZE;
return (table & TABLE_MASK) == 0;
}
int main() {
for (int i = 0; i < SIZE; i++) {
RIGHT_MASK |= BASE << (i * SIZE);
LEFT_MASK |= (BASE << (SIZE - 1)) << (i * SIZE);
TOP_MASK |= (BASE << i) << (SIZE * (SIZE - 1));
BOTTOM_MASK |= BASE << i;
}
#pragma omp parallel for
for (table i = 0; i < NUMBER_OF_PATTERN; i++) {
if (validate(i)) {
#ifdef _OPENMP
mtx.lock();
#endif
results.push_back(i);
#ifdef _OPENMP
mtx.unlock();
#endif
}
}
std::for_each(results.begin(), results.end(), [](table r)
{
for (int i = SIZE - 1; i >= 0; i--) {
for (int j = SIZE - 1; j >= 0; j--) {
if ((r & (BASE << (i * SIZE + j))) == 0)
std::cout << "0";
else
std::cout << "1";
}
std::cout << std::endl;
}
std::cout << std::endl;
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment