3*3 matrix, the value is limited to 1-9 is not repeated, known as the number of and, and an oblique value, to find the matrix, what is the fast way?

Source: Internet
Author: User



x x x 11
x x x 15
x x x 19

16 14 15 15


Discussion stickers:

http://bbs.csdn.net/topics/391816265


The exact matching method of three rows is first obtained:

Puzzl.cpp: Defines the entry point of the console application. #include "stdafx.h"//Puzzle.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include &lt ;vector> #include <set> #include <iostream>using namespace Std;int result[3][3] = {0};bool check (int a, int b, int c, int sum) {if ((A + B + c) = = sum) return true; else return false;} int lastvalue (int a, int b,int sum) {return (sum-a-B);} Min maxvector<vector<int> > Zuhe (int min,int max,int sum) {vector<vector<int> > Quanji; for ( int i = Min;i < Max;   ++i) {for (int j = 1;j < Max; ++j) {if (i + J > sum) continue;      else {for (int k = 1; k <max; ++k) {if (check (i,j,k,sum)) {vector<int> Ziji;      Ziji.push_back (i);      Ziji.push_back (j);      Ziji.push_back (k);      if (i!=j&&j!=k&&i!=k) {quanji.push_back (Ziji);     }} else {continue; }}}}} return Quanji;} Vector<vector<int> > Filter_zuhe (int key,vector<vector<int> > Temp) {vector<vector<int> >::iterator ITER; for (iter = Temp.begin (); Iter!=temp.end ();)  {if (*iter) [0]! = key) iter = Temp.erase (ITER); else ITER + +; } return temp;} BOOL IsOk () {set<int> myset;for (int i = 0;i <3; ++i) {for (int j = 0;j<3;++j) {if (result[i][j]>0&& RESULT[I][J]&LT;10) {Myset.insert (result[i][j]);}} if (Myset.size ()!=9) {return false;} Else{if (Myset.size () ==9) {for (int i = 0;i <3; ++i) {cout <<endl;for (int j = 0;j<3;++j) {cout<<result[i][ j]<< "";}} return true;}}} void Dayin () {for (int i = 0;i <3; ++i) {cout <<endl;for (int j = 0;j<3;++j) {cout<<result[i][j]<< ""; }}cout<<endl;} void Qingling () {for (int i = 0;i <3; ++i) {for (int j = 0;j<3;++j) {(result[i][j]=0);}}} int _tmain (int argc, _tchar* argv[]) {vector<vector<int> > Quanjiheng = Zuhe (1,9,11); vector<vector< int> > Quanjishu = Zuhe (1,9,16); Vector<vector<int> > Quanjixie = Zuhe (1,9,15); int Sizeheng = Quanjiheng.size (); int Sizeshu = Quanjishu.size (); int sizexie = Quanjixie.size (); int key = 0,last_key = 0; vector<vector<int> > Quanjishufilter; vector<vector<int> > Quanjixiefilter;   for (int i = 0;i < Sizeheng; ++i) {key = Quanjiheng[i][0];   if (key!=last_key) {quanjishufilter = Filter_zuhe (Key,quanjishu); quanjixiefilter = Filter_zuhe (Key,quanjixie);   } Last_key = key;    Assign a value for (int j = 0;j< 3;++j) {result[0][j] = quanjiheng[i][j];  } int sizeshu = Quanjishufilter.size ();  for (int k = 0; k< sizeshu;++k) {//Assign value to vertical lines for (int j = 0;j<3;++j) {result[j][0] = quanjishufilter[k][j];  } int sizexie = Quanjixiefilter.size ();  for (int x = 0, x < Sizexie; ++x) {for (int j = 0; j < 3; ++j) {result[j][j] = quanjixiefilter[x][j]; }RESULT[2][1] = 14-result[0][1]-result[1][1];result[1][2] = 15-result[1][0]-result[1][1];if (IsOk ()) {//dayin (); GetChar ();return 0;}   else{}}} qingling (); } getchar (); System ("pause"); return 0;}




Methods for arranging combinations:


Template <typename t>void swap (t* array, unsigned int i, unsigned int j) {T t = array[i];array[i] = Array[j];array[j] = t;} void FullArray (int* array, size_t array_size, unsigned int index) {if (index >= array_size) {if ((Array[0] + array[1] + a RRAY[2] = = one) && (Array[3] + array[4] + array[5] = =) && (array[6] + array[7] + array[8] = =) &&amp ; (Array[0] + array[3] + array[6] = = && (array[1] + array[4] + array[7] = =) && (array[2] + array[5] + a RRAY[8] = = && (array[0] + array[4] + array[8] = =)) {printf ("%d,%d,%d\n", Array[0], array[1], array[2]);p rintf ("%d,%d,%d\n", Array[3], array[4], array[5]);p rintf ("%d,%d,%d\n\n", Array[6], array[7], array[8]);} return;} for (unsigned int i = index; i < array_size; ++i) {swap (array, I, index); FullArray (array, array_size, index + 1); swap (array, I, index);}} int main (int argc, char* argv[]) {int value[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; FullArray (value, 9, 0);}



Imitate the person to do the method, arbitrarily fill in 3 empty space, guarantee not to walk different column, or not a slash. We can figure out the rest.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

3*3 matrix, the value is limited to 1-9 is not repeated, known as the number of and, and an oblique value, to find the matrix, what is the fast way?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.