void CreateMatrix(void) {
	int a, b, i, j;
	//Build toroidal linklist matrix according to data bitmap
	for (a = 0; a < nCol; a++) {
		for (b = 0; b < nRow; b++) {
			if (Data[a][b] != 0) {
				// Left pointer
				i = a;
				j = b;
				do {
					i = dataLeft(i);
				} while (Data[i][j] == 0);
				Matrix[a][b].Left = &Matrix[i][j];
				// Right pointer
				i = a;
				j = b;
				do {
					i = dataRight(i);
				} while (Data[i][j] == 0);
				Matrix[a][b].Right = &Matrix[i][j];
				// Up pointer
				i = a;
				j = b;
				do {
					j = dataUp(j);
				} while (Data[i][j] == 0);
				Matrix[a][b].Up = &Matrix[i][j];
				// Down pointer
				i = a;
				j = b;
				do {
					j = dataDown(j);
				} while (Data[i][j] == 0);
				Matrix[a][b].Down = &Matrix[i][j];
				// Header pointer
				Matrix[a][b].Header = &Matrix[a][nRow - 1];
				Matrix[a][b].IDNum = b;
				//Row Header
				RowHeader[b] = &Matrix[a][b];
			}
		}
	}
	for (a = 0; a < nCol; a++) {
		Matrix[a][nRow - 1].IDName = 'C';
		Matrix[a][nRow - 1].IDNum = a;
	}
	//Insert root
	Root.IDName = 'R';
	Root.Left = &Matrix[nCol - 1][nRow - 1];
	Root.Right = &Matrix[0][nRow - 1];
	Matrix[nCol - 1][nRow - 1].Right = &Root;
	Matrix[0][nRow - 1].Left = &Root;
}
Exemple #2
0
void CreateMatrix(INT *Data, INT nb_rows, INT nb_cols, INT verbose_level)
{
	INT f_v = (verbose_level >= 1);
	INT a, b, i, j;

	nRow = nb_rows;
	nCol = nb_cols;
	dlx_nb_sol = 0;

	if (f_v) {
		cout << "dlx.C: CreateMatrix" << endl;
		cout << "The " << nb_rows << " x " << nb_cols << " matrix is:" << endl;
		for (i = 0; i < nb_rows; i++) {
			for (j = 0; j < nb_cols; j++) {
				cout << Data[i * nb_cols + j];
				}
			cout << endl;
			}
		//INT_matrix_print(Data, nb_rows, nb_cols);
		cout << endl;
		}

	Matrix = new dlx_node[nRow * nCol];
	Root = new dlx_node;
	//RowHeader = new pdlx_node[nRow];

	
	Result = NEW_INT(nRow);
	Nb_choices = NEW_INT(nRow);
	Cur_choice = NEW_INT(nRow);
	Nb_col_nodes = NEW_INT(nCol);

	for (j = 0; j < nCol; j++) {
		Nb_col_nodes[j] = 0;
		}

	
	// Build toroidal linklist matrix according to data bitmap
	for (a = 0; a < nRow; a++) {
		for (b = 0; b < nCol; b++) {
			Matrix[a * nCol + b].row = a;
			Matrix[a * nCol + b].col = b;
			}
		}
	
	// Connect the coefficients which are nonzero to their up and down and left and right neighbors:

	for (a = 0; a < nRow; a++) {
		for (b = 0; b < nCol; b++) {
			if (Data[a * nCol + b] != 0) {
				// Left pointer
				i = a; j = b; do { j = dataLeft(j); } while (Data[i * nCol + j] == 0);
				Matrix[a * nCol + b].Left = &Matrix[i * nCol + j]; 
				// Right pointer
				i = a; j = b; do { j = dataRight(j); } while (Data[i * nCol + j] == 0);
				Matrix[a * nCol + b].Right = &Matrix[i * nCol + j];
				// Up pointer
				i = a; j = b; do { i = dataUp(i); } while (Data[i * nCol + j] == 0);
				Matrix[a * nCol + b].Up = &Matrix[i * nCol + j];
				// Down pointer
				i = a; j = b; do { i = dataDown(i); } while (Data[i * nCol + j] == 0);
				Matrix[a * nCol + b].Down = &Matrix[i * nCol + j]; 

#if 0
				cout << "at " << a << "/" << b << ":";
				cout << " Left="; print_position(Matrix[a * nCol + b].Left);
				cout << " Right="; print_position(Matrix[a * nCol + b].Right);
				cout << " Up="; print_position(Matrix[a * nCol + b].Up);
				cout << " Down="; print_position(Matrix[a * nCol + b].Down);
				cout << endl;
#endif
				// Header pointer at the very bottom:
				Matrix[a * nCol + b].Header = &Matrix[(nRow - 1) * nCol + b];
				//Row Header
				//RowHeader[a] = &Matrix[a * nCol + b];
				}
			}
		}

	// Count the number of nodes in each column (i.e. the number of ones in the column of the matrix)
	for (j = 0; j < nCol; j++) {
		Nb_col_nodes[j] = 0;

		dlx_node *ColNode, *RowNode;

		// this is the RHS
		ColNode = &Matrix[(nRow - 1) * nCol + j];
		
		for (RowNode = ColNode->Down; RowNode != ColNode; RowNode = RowNode->Down) {
			Nb_col_nodes[j]++;
			}
		}
	
#if 0
	for (a = 0; a < nCol; a++) {
		Matrix[(nRow - 1) * nCol + a].row = nRow - 1;
		Matrix[(nRow - 1) * nCol + a].col = a;
		}
#endif
	//Insert root at the end of all RHS nodes:
	Root->Left = &Matrix[(nRow - 1) * nCol + (nCol - 1)];
	Root->Right = &Matrix[(nRow - 1) * nCol + 0];
	Matrix[(nRow - 1) * nCol + nCol - 1].Right = Root;
	Matrix[(nRow - 1) * nCol + 0].Left = Root;
	Root->row = -1;
	Root->col = -1;
}