fork(1) download
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <istream>
  6. #include <iterator>
  7. #include <map>
  8. #include <set>
  9. #include <sstream>
  10. #include <stdexcept>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14.  
  15. using namespace std;
  16.  
  17. #define DBG(x) { cout << left << setw(40) << #x << (x) << endl; }
  18.  
  19. template<int Base, int Exp>
  20. class Power{
  21. public:
  22. static const int value = Power<Base,Exp-1>::value * Base;
  23. };
  24.  
  25. template<int Base>
  26. class Power<Base,0>{
  27. public:
  28. static const int value = 1;
  29. };
  30.  
  31. template<template <int,int,int> class Func, template<int> class ArgGetter, int Times>
  32. class Apply{
  33. public:
  34. template<typename T>
  35. static void exec(T& t){
  36. typedef ArgGetter<Times> Arg;
  37. Func<Arg::type::x, Arg::type::y, Arg::type::value>::exec(t);
  38. Apply<Func,ArgGetter,Times-1>::exec(t);
  39. }
  40. template<typename T>
  41. static void exec(T const& t){
  42. typedef ArgGetter<Times> Arg;
  43. Func<Arg::type::x, Arg::type::y, Arg::type::value>::exec(t);
  44. Apply<Func,ArgGetter,Times-1>::exec(t);
  45. }
  46. template<typename T, typename U>
  47. static void exec(T& t, U& u){
  48. typedef ArgGetter<Times> Arg;
  49. Func<Arg::type::x, Arg::type::y, Arg::type::value>::exec(t, u);
  50. Apply<Func,ArgGetter,Times-1>::exec(t, u);
  51. }
  52. template<typename T, typename U>
  53. static void exec(T const& t, U& u){
  54. typedef ArgGetter<Times> Arg;
  55. Func<Arg::type::x, Arg::type::y, Arg::type::value>::exec(t, u);
  56. Apply<Func,ArgGetter,Times-1>::exec(t, u);
  57. }
  58. template<typename T, typename U>
  59. static void exec(T& t, U const& u){
  60. typedef ArgGetter<Times> Arg;
  61. Func<Arg::type::x, Arg::type::y, Arg::type::value>::exec(t, u);
  62. Apply<Func,ArgGetter,Times-1>::exec(t, u);
  63. }
  64. template<typename T, typename U>
  65. static void exec(T const& t, U const& u){
  66. typedef ArgGetter<Times> Arg;
  67. Func<Arg::type::x, Arg::type::y, Arg::type::value>::exec(t, u);
  68. Apply<Func,ArgGetter,Times-1>::exec(t, u);
  69. }
  70. };
  71.  
  72. template<template <int,int,int> class Func, template<int> class ArgGetter>
  73. class Apply<Func,ArgGetter,0>{
  74. public:
  75. template<typename T>
  76. static void exec(T&){}
  77. template<typename T>
  78. static void exec(T const&){}
  79. template<typename T, typename U>
  80. static void exec(T&, U&){}
  81. template<typename T, typename U>
  82. static void exec(T const&, U&){}
  83. template<typename T, typename U>
  84. static void exec(T&, U const&){}
  85. template<typename T, typename U>
  86. static void exec(T const&, U const&){}
  87. };
  88.  
  89. template<template<int,int> class Iterator, int Size, int X, int Y, int N>
  90. class IfCellTraversed{
  91. public:
  92. typedef Iterator<Size,N> Current;
  93. static const bool value = (Current::x == X && Current::y == Y) || IfCellTraversed<Iterator,Size,X,Y,N-1>::value;
  94. };
  95.  
  96. template<template<int,int> class Iterator, int Size, int X, int Y>
  97. class IfCellTraversed<Iterator,Size,X,Y,0>{
  98. public:
  99. typedef Iterator<Size,1> Current;
  100. static const bool value = (Current::x == X && Current::y == Y);
  101. };
  102.  
  103. template<int Size, int N>
  104. class MagicSquareIterator{
  105. public:
  106. typedef MagicSquareIterator<Size,N-1> Prev;
  107. typedef IfCellTraversed< ::MagicSquareIterator,Size, (Prev::x+1)%Size, (Prev::y-1+Size)%Size, N-1> SkipTest;
  108. static const int x = SkipTest::value ? Prev::x : (Prev::x+1)%Size;
  109. static const int y = SkipTest::value ? (Prev::y + 1)%Size : (Prev::y-1+Size)%Size;
  110. static const int value = Prev::value + 1;
  111. };
  112.  
  113. template<int Size>
  114. class MagicSquareIterator<Size,1>{
  115. public:
  116. static const int x = Size/2;
  117. static const int y = 0;
  118. static const int value = 1;
  119. };
  120.  
  121. template<int Size>
  122. class IteratorForSize{
  123. public:
  124. template<int N>
  125. class Template{
  126. public:
  127. typedef MagicSquareIterator<Size,N> type;
  128. };
  129. };
  130.  
  131. template<int X, int Y, int Value>
  132. class FillSquare{
  133. public:
  134. template<typename T>
  135. static void exec(T& t){
  136. t(X,Y) = Value;
  137. }
  138. };
  139.  
  140.  
  141.  
  142. template<int N>
  143. class Copy{
  144. public:
  145. template<int X, int Y, int>
  146. class Regular{
  147. public:
  148. template<typename T, typename U>
  149. static void exec(T const& from, U& to){
  150. to(X,Y) = from(X,Y);
  151. }
  152. };
  153.  
  154. template<int X, int Y, int>
  155. class FromRawToSquare{
  156. public:
  157. template<typename T, typename U>
  158. static void exec(T const& from, U& to){
  159. to(X,Y) = from[Y*N + X];
  160. }
  161. };
  162. };
  163.  
  164.  
  165.  
  166.  
  167.  
  168. template<int X, int Y, int>
  169. class AddValue{
  170. public:
  171. template<typename T, typename U>
  172. static void exec(T& to, U const& what){
  173. to(X,Y) += what;
  174. }
  175. };
  176.  
  177. template<int X, int Y, int>
  178. class AddValueFrom{
  179. public:
  180. template<typename T, typename U>
  181. static void exec(T& to, U const& what){
  182. to(X,Y) += what(X,Y);
  183. }
  184. };
  185.  
  186.  
  187. template<int N>
  188. class MagicSquare;
  189.  
  190. template<int N>
  191. istream& operator>> (istream& s, MagicSquare<N> & sq);
  192.  
  193. template<int N>
  194. class MagicSquare{
  195.  
  196. public:
  197.  
  198. template<template <int,int,int> class Func>
  199. class SquareApply{
  200. public:
  201. typedef Apply<Func, IteratorForSize<N>::template Template,Power<N,2>::value> type;
  202. };
  203.  
  204. MagicSquare(){
  205. SquareApply<FillSquare>::type::exec(*this);
  206. }
  207.  
  208. MagicSquare(MagicSquare const& o){
  209. SquareApply<Copy<N>::template Regular>::type::exec(o, *this);
  210. }
  211.  
  212. MagicSquare(istream& s){
  213. s >> *this;
  214. }
  215.  
  216. MagicSquare(int added){
  217. SquareApply<FillSquare>::type::exec(*this);
  218. SquareApply<AddValue>::type::exec(*this,added);
  219.  
  220. }
  221.  
  222. ~MagicSquare(){}
  223.  
  224. MagicSquare& operator=(MagicSquare const& o){
  225. SquareApply<Copy<N>::template Regular>::type::exec(o, *this);
  226. }
  227.  
  228. MagicSquare operator+(MagicSquare const& o){
  229. MagicSquare sq;
  230. SquareApply<Copy<N>::template Regular>::type::exec(o, sq);
  231. SquareApply<AddValueFrom>::type::exec(sq, *this);
  232. return sq;
  233. }
  234.  
  235. int operator()(int x, int y) const {
  236. return data[y][x];
  237. }
  238.  
  239. int& operator()(int x, int y){
  240. return data[y][x];
  241. }
  242.  
  243.  
  244. private:
  245. int data[N][N];
  246. int property1, property2, property3;
  247. };
  248.  
  249. template<int N>
  250. ostream& operator<< (ostream& o, MagicSquare<N> const& sq){
  251. o << "Magic Square " << N << "x" << N << ":" << endl;
  252. for(int y = 0; y < N; y++){
  253. for(int x = 0; x < N; x++){
  254. o << setw(4) << right << sq(x,y);
  255. }
  256. o << endl;
  257. }
  258. return o;
  259. }
  260.  
  261. template<int N>
  262. istream& operator>> (istream& s, MagicSquare<N> & sq){
  263. std::vector<int> values(Power<N,2>::value);
  264. for(int i = 0; i < Power<N,2>::value; i++){
  265. s >> values[i];
  266. }
  267.  
  268. MagicSquare<N>::template SquareApply<Copy<N>::template FromRawToSquare>::type::exec(values, sq);
  269. return s;
  270. }
  271.  
  272. int main()
  273. {
  274. MagicSquare<5> sq;
  275.  
  276. stringstream str("17 23 4 10 11 24 5 6 12 18 1 7 13 19 25 8 14 20 21 2 15 16 22 3 9");
  277.  
  278. MagicSquare<5> sq2(str);
  279.  
  280. DBG(sq);
  281. DBG(sq2);
  282. DBG(sq+sq2);
  283.  
  284. MagicSquare<1> m1;
  285. MagicSquare<3> m3;
  286. MagicSquare<5> m5;
  287. MagicSquare<7> m7;
  288. MagicSquare<9> m9;
  289. MagicSquare<11> m11;
  290.  
  291. DBG(m1);
  292. DBG(m3);
  293. DBG(m5);
  294. DBG(m7);
  295. DBG(m9);
  296. DBG(m11);
  297. }
  298.  
  299.  
  300.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
sq                                      Magic Square 5x5:
  17  24   1   8  15
  23   5   7  14  16
   4   6  13  20  22
  10  12  19  21   3
  11  18  25   2   9

sq2                                     Magic Square 5x5:
  17  23   4  10  11
  24   5   6  12  18
   1   7  13  19  25
   8  14  20  21   2
  15  16  22   3   9

sq+sq2                                  Magic Square 5x5:
  34  47   5  18  26
  47  10  13  26  34
   5  13  26  39  47
  18  26  39  42   5
  26  34  47   5  18

m1                                      Magic Square 1x1:
   1

m3                                      Magic Square 3x3:
   8   1   6
   3   5   7
   4   9   2

m5                                      Magic Square 5x5:
  17  24   1   8  15
  23   5   7  14  16
   4   6  13  20  22
  10  12  19  21   3
  11  18  25   2   9

m7                                      Magic Square 7x7:
  30  39  48   1  10  19  28
  38  47   7   9  18  27  29
  46   6   8  17  26  35  37
   5  14  16  25  34  36  45
  13  15  24  33  42  44   4
  21  23  32  41  43   3  12
  22  31  40  49   2  11  20

m9                                      Magic Square 9x9:
  47  58  69  80   1  12  23  34  45
  57  68  79   9  11  22  33  44  46
  67  78   8  10  21  32  43  54  56
  77   7  18  20  31  42  53  55  66
   6  17  19  30  41  52  63  65  76
  16  27  29  40  51  62  64  75   5
  26  28  39  50  61  72  74   4  15
  36  38  49  60  71  73   3  14  25
  37  48  59  70  81   2  13  24  35

m11                                     Magic Square 11x11:
  68  81  94 107 120   1  14  27  40  53  66
  80  93 106 119  11  13  26  39  52  65  67
  92 105 118  10  12  25  38  51  64  77  79
 104 117   9  22  24  37  50  63  76  78  91
 116   8  21  23  36  49  62  75  88  90 103
   7  20  33  35  48  61  74  87  89 102 115
  19  32  34  47  60  73  86  99 101 114   6
  31  44  46  59  72  85  98 100 113   5  18
  43  45  58  71  84  97 110 112   4  17  30
  55  57  70  83  96 109 111   3  16  29  42
  56  69  82  95 108 121   2  15  28  41  54