#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define magic 15 using namespace std; typedef vector< vector > MATRIKS; typedef vector< MATRIKS > matrixvec; /* magic square : distinct numbers 5 in the center (2,6,4,8) in corners (1,3,7,9) middle 8 possible magic square in 3x3 */ matrixvec generatemagic() { matrixvec matrixvector; vectorcorners ={2,4,6,8}; map quadmap; map latsymmap; quadmap.insert(make_pair(2,8)); quadmap.insert(make_pair(4,6)); quadmap.insert(make_pair(8,2)); quadmap.insert(make_pair(6,4)); latsymmap.insert(make_pair(2,4)); latsymmap.insert(make_pair(4,2)); latsymmap.insert(make_pair(6,8)); latsymmap.insert(make_pair(8,6)); MATRIKS mtrx={{0,0,0},{0,0,0},{0,0,0}}; for (std::vector::iterator it=corners.begin();it!=corners.end();++it) { mtrx[0][0]=*it; mtrx[2][2]=quadmap.find(*it)->second; mtrx[1][1]=5; mtrx[0][2]=latsymmap.find(*it)->second; mtrx[2][0]=quadmap.find(mtrx[0][2])->second; mtrx[0][1]=15-(mtrx[0][0]+mtrx[0][2]); mtrx[1][0]=15-(mtrx[0][0]+mtrx[2][0]); mtrx[2][1]=15-(mtrx[2][0]+mtrx[2][2]); mtrx[1][2]=15-(mtrx[0][2]+mtrx[2][2]); matrixvector.push_back(mtrx); mtrx[0][0]=*it; mtrx[2][2]=quadmap.find(*it)->second; mtrx[1][1]=5; mtrx[2][0]=latsymmap.find(*it)->second; mtrx[0][2]=quadmap.find(mtrx[2][0])->second; mtrx[0][1]=15-(mtrx[0][0]+mtrx[0][2]); mtrx[1][0]=15-(mtrx[0][0]+mtrx[2][0]); mtrx[2][1]=15-(mtrx[2][0]+mtrx[2][2]); mtrx[1][2]=15-(mtrx[0][2]+mtrx[2][2]); matrixvector.push_back(mtrx); } return matrixvector; } unsigned int algo(MATRIKS& mtrx, MATRIKS& magicm) { unsigned int cost=0; for (int i=0;i<3;++i) for(int j=0;j<3;++j) cost+=abs(mtrx[i][j]-magicm[i][j]); return cost; } int main() { int n =3 ; MATRIKS matrix; std::vector v; std::map< int,pair > mmap; int a=0; int b=0; int c=0; unsigned int c_cost=100; //std::numeric_limits::max(); for(int j=0;j>a>>b>>c; matrix.push_back({a,b,c}); } matrixvec magicvector=generatemagic(); for (matrixvec::iterator it=magicvector.begin();it!=magicvector.end();++it) { unsigned int vcost=algo(matrix,*it); c_cost= (c_cost