Skip to content

Instantly share code, notes, and snippets.

@taroyabuki
Created June 7, 2014 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taroyabuki/729c15284ffaee997f3e to your computer and use it in GitHub Desktop.
Save taroyabuki/729c15284ffaee997f3e to your computer and use it in GitHub Desktop.
魔方陣の解の数を一瞬で求めるプログラム 4x4の魔方陣の場合 http://blog.unfindable.net/archives/7466
#include <iostream>
const int s = 34;
constexpr int eq(int a, int b) { return a != b ? 0 : a; }
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8, int x9, int x10, int x11, int x12, int x14, int x15) {
return (x15 < 1 || x15 > 16) ? 0 :
(((used & (1 << x15)) ? 0 : 1));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8, int x9, int x10, int x11, int x12, int x14) {
return (x14 < 1 || x14 > 16) ? 0 :
(((used & (1 << x14)) ? 0 : check(used + (1 << x14), x1, x4, x13, x16, x2, x3, x5, x6, x7, x8, x9, x10, x11, x12, x14, eq(s - x13 - x14 - x16, s - x3 - x7 - x11))));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8, int x9, int x10, int x11, int x12) {
return (x12 < 1 || x12 > 16) ? 0 :
(((used & (1 << x12)) ? 0 : check(used + (1 << x12), x1, x4, x13, x16, x2, x3, x5, x6, x7, x8, x9, x10, x11, x12, s - x2 - x6 - x10)));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8, int x9, int x10, int x11) {
return (x11 < 1 || x11 > 16) ? 0 :
(((used & (1 << x11)) ? 0 : check(used + (1 << x11), x1, x4, x13, x16, x2, x3, x5, x6, x7, x8, x9, x10, x11, eq(s - x9 - x10 - x11, s - x4 - x8 - x16))));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8, int x9, int x10) {
return (x10 < 1 || x10 > 16) ? 0 :
(((used & (1 << x10)) ? 0 : check(used + (1 << x10), x1, x4, x13, x16, x2, x3, x5, x6, x7, x8, x9, x10, s - x1 - x6 - x16)));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8, int x9) {
return (x9 < 1 || x9 > 16) ? 0 :
(((used & (1 << x9)) ? 0 : check(used + (1 << x9), x1, x4, x13, x16, x2, x3, x5, x6, x7, x8, x9, s - x4 - x7 - x13)));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7, int x8) {
return (x8 < 1 || x8 > 16) ? 0 :
(((used & (1 << x8)) ? 0 : check(used + (1 << x8), x1, x4, x13, x16, x2, x3, x5, x6, x7, x8, s - x1 - x5 - x13)));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6, int x7) {
return (x7 > 16) ? 0 :
(((used & (1 << x7)) ? 0 : check(used + (1 << x7), x1, x4, x13, x16, x2, x3, x5, x6, x7, s - x5 - x6 - x7))
+ check(used, x1, x4, x13, x16, x2, x3, x5, x6, x7 + 1));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5, int x6) {
return (x6 > 16) ? 0 :
(((used & (1 << x6)) ? 0 : check(used + (1 << x6), x1, x4, x13, x16, x2, x3, x5, x6, 1))
+ check(used, x1, x4, x13, x16, x2, x3, x5, x6 + 1));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3, int x5) {
return (x5 > 16) ? 0 :
(((used & (1 << x5)) ? 0 : check(used + (1 << x5), x1, x4, x13, x16, x2, x3, x5, 1))
+ check(used, x1, x4, x13, x16, x2, x3, x5 + 1));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2, int x3) {
return (x3 < 1 || x3 > 16) ? 0 :
(((used & (1 << x3)) ? 0 : check(used + (1 << x3), x1, x4, x13, x16, x2, x3, 1)));
}
constexpr int check(int used, int x1, int x4, int x13, int x16, int x2) {
return (x2 > 16) ? 0 :
(((used & (1 << x2)) ? 0 : check(used + (1 << x2), x1, x4, x13, x16, x2, s - x1 - x2 - x4))
+ check(used, x1, x4, x13, x16, x2 + 1));
}
constexpr int check(int used, int x1, int x4, int x13, int x16) {
return (x16 > 16) ? 0 :
(((used & (1 << x16)) ? 0 : check(used + (1 << x16), x1, x4, x13, x16, 1))
+ check(used, x1, x4, x13, x16 + 1));
}
constexpr int check(int used, int x1, int x4, int x13) {
return (x13 > 16) ? 0 :
(((used & (1 << x13)) ? 0 : check(used + (1 << x13), x1, x4, x13, x1 + 1))
+ check(used, x1, x4, x13 + 1));
}
constexpr int check(int used, int x1, int x4) {
return (x4 > 16) ? 0 :
(((used & (1 << x4)) ? 0 : check(used + (1 << x4), x1, x4, x4 + 1))
+ check(used, x1, x4 + 1));
}
constexpr int check(int used, int x1) {
return (x1 > 13) ? 0 :
(((used & (1 << x1)) ? 0 : check(used + (1 << x1), x1, x1 + 1))
+ check(used, x1 + 1));
}
int main() {
constexpr int result = check(0,1);
std::cout << result << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment