Young's matrix definition and its search implementation C ++, Young's matrix Definition
First, we will introduce the definition of this data structure,Young TableauThere isM * n matrixAnd then there is an arrayA [k], WhereK <= m * n,ThenA [k]Enter the number inM * n matrixThe filling rule is:
1. Each row has a columnStrictly monotonic increasing(For other versions, the principle is the same ).
2. If there is still space in the matrix after entering the number in a [k], enter∞.
Example:
1 # pragma once 2 // YoungTableau. h 3 # include <iostream> 4 class YoungTableau 5 {6 public: 7 YoungTableau (int rows, int columns, int ** newTable) 8 {9 x = rows; // The number of rows is 10 y = columns; // The number of columns is 11 12 if (newTable! = NULL) 13 {14 table = (int **) (new int [rows * columns]); 15 for (int I = 0; I <rows * columns; I ++) 16 17 {18 (int *) table) [I] = (int *) newTable) [I]; 19 std :: cout <(int *) table) [I] <std: endl; 20} 21 22} 23 24}; 25 ~ YoungTableau (); 26 private: int x; 27 private: int y; 28 private: int ** table; 29 30 public: 31 void print_matrix (void) 32 {33 std :: cout <x <y <std: endl; 34 if (table! = NULL) 35 {36 for (int I = 0; I <x; I ++) 37 for (int j = 0; j <y; j ++) 38 std:: cout <(int *) table) [I * y + j] <std: endl; 39} 40} 41 public: int get_element (int rows, int columns, int * res) 42 {43 if (res! = NULL) 44 {45 if (rows> = this-> x | columns> = this-> y) 46 {47 * res = 0; 48 return-1; 49} 50 else 51 {52 * res = (int *) table) [rows * y + columns]; 53 return 0; 54} 55} 56 else 57 {58 * res = 0; 59 return-2; 60} 61} 62 private: bool IsEqualAtLeftTop (int data, int rownum, int columnnum) 63 {64 if (this-> table! = NULL) 65 {66 if (rownum> = x | columnnum> = y | (rownum = x-1 & columnnum = 0 & data! = (Int *) this-> table) [rownum * y + columnnum]) 67 {68 std: cout <"illgal column" <std: endl; 69 return false; 70} 71 else if (data = (int *) this-> table) [rownum * y + columnnum]) 72 {73 return true; 74} 75 else if (data <(int *) this-> table) [rownum * y + columnnum]) 76 {77 std :: cout <"next column" <std: endl; 78 if (columnnum = 0) 79 return false; 80 else 81 return IsEqualAtLeftTop (data, ro Wnum, columnnum-1); 82} 83 else 84 {85 std: cout <"next row" <std: endl; 86 if (rownum = x-1) 87 return false; 88 else 89 return IsEqualAtLeftTop (data, rownum + 1, columnnum); 90} 91 92} 93 else 94 {95 return false; 96} 97} 98 public: bool IsExistence (int data) 99 {100 if (this-> table! = NULL) 101 {102/* for (int I = 0; I <x; I ++) 103 for (int j = 0; j <y; j ++) 104 {105 if (data = (int *) table) [I * y + j]) 106 return true; 107} 108 return false; */109 return IsEqualAtLeftTop (data, 0, y-1); 110} 111 return false; 112} 113 114 };
1 // Young_Tableau.cpp: defines the entry point of the console application. 2 // 3 4 # include "stdafx. h "5 # include" YoungTableau. h "6 7 int _ tmain (int argc, _ TCHAR * argv []) 8 {9 int tmp_table [3] [3] = 10 {11 {1, 3, 4}, 12 {2, 3, 6}, 13 {5, 8, 10} 14}; 15 YoungTableau matrix (3,3, (int **) tmp_table); 16 matrix. print_matrix (); 17 int tmp = 0; 18 matrix. get_element (2,2, & tmp); 19 std: cout <tmp <std: endl; 20 21 if (matrix. isExistence (5) = true) 22 std: cout <"true" <std: endl; 23 else std: cout <"false" <std:: endl; 24 while (1); 25 return 0; 26}
1 # include "stdafx. h" 2 # include "YoungTableau. h" 3 // YoungTableau. cpp: class implementation 4/5 6 7 YoungTableau ::~ YoungTableau () 8 {9 10}