C + + implementation scheme of Latin matrix problem using backtracking method

Source: Internet
Author: User
Tags readfile

These two days just in the rush algorithm design of the job, here to do a few need to write code to put up the problem, convenient to view later.
1. Topic Requirements

2. Algorithmic thinking
The basic idea of this topic is to use backtracking, for M row n columns, essentially a two-dimensional array, we can write the solution of the problem as x[1],x[2],x[3] ... x[m*n], then for each point X[i] the value is actually [1, n], the use of backtracking algorithm framework, here the Constraints, that is, peers, the same column does not have the same value, and there is no optimization target, as long as the problem like N to find all the solution.

In addition, back to the boundary conditions, need to pay special attention to, because we have two-dimensional array as a one-dimensional array to implement, subscript starting from 0, theoretically to m*n-1 should end, not much attention at first, resulting in debugging a long time *23333333333*

3. The algorithm realizes the source code:

3.1 LatiMatrix.h

//-------------------------"CHP 5 HW Latin matrix gem Arrangement"----------------------//@ author:zhyh2010//@ date:20150524//@ version:1.0//@ Description: There are n different shapes of gems, each of which is more than enough, and is now required to arrange them into//m row n column matrix (M <= N) makes each row, in each column, a different shape of the gem//M,n How many algorithms are available for a given number of times//@ idea: Backtracking//-----------------------------------"End Tip"-------------------------------#pragma onceClass clatimatrix{ Public:Clatimatrix(); ~clatimatrix ();intSolve ();Private:void BackTrace(intID);voidOutput ();BOOLIsOK (intRowintCOL);voidReadFile ();voidWriteFile ();Private:intM_m;intM_n;int* * M_MATRIX;intM_solution_num;};

3.2.latimatrix.cpp

#include "LatiMatrix.h"#include <cstdio>#include <cstdlib>#include <fstream>Clatimatrix::clatimatrix () {m_m =0; M_n =0;    M_matrix = NULL; M_solution_num =0;} Clatimatrix::~clatimatrix () { for(inti =0; I! = m_n; i++) {if(M_matrix[i])DeleteM_matrix[i]; }if(M_matrix)Delete[] M_matrix;}voidClatimatrix::backtrace (intID) {//=============== "There was a problem when I first wrote it without adding the = sign" =================    if(ID >= m_m * m_n) {output ();return; }introw = Id/m_n;intCol = id% M_n; for(intK =0; K! = M_n; k++) {M_matrix[row][col] = k;if(IsOK (Row, col))            {id++;            BackTrace (ID);        id--; }    }}voidClatimatrix::output () {m_solution_num++;}BOOLClatimatrix::isok (intRowintCol) {intk = M_matrix[row][col]; for(inti =0; I! = col; i++)if(k = = M_matrix[row][i])return false; for(inti =0; I! = row; i++)if(k = = M_matrix[i][col])return false;return true;}voidClatimatrix::readfile () {STD:: Ifstream infile ("Input.txt");    infile >> m_m >> m_n; M_matrix =New int*[M_M]; for(inti =0; I! = m_n; i++) {M_matrix[i] =New int[M_n];memset(M_matrix[i],0,sizeof(M_matrix[i])); }}voidClatimatrix::writefile () {STD:: Ofstream outfile ("Output.txt"); outfile << M_solution_num <<STD:: Endl;}intClatimatrix::solve () {ReadFile (); BackTrace (0); WriteFile ();returnM_solution_num;}

3.3.main.cpp

//-------------------------"CHP 5 HW Latin matrix gem Arrangement"----------------------//@ author:zhyh2010//@ date:20150524//@ version:1.0//@ Description: There are n different shapes of gems, each of which is more than enough, and is now required to arrange them into//m row n column matrix (M <= N) makes each row, in each column, a different shape of the gem//M,n How many algorithms are available for a given number of times//@ idea: Backtracking//-----------------------------------"End Tip"-------------------------------#include "LatiMatrix.h"#include <iostream>voidMain () {Clatimatrix instance;intres = Instance.solve ();STD::cout<<"res ="<< Res <<STD:: Endl;}

C + + implementation scheme of Latin matrix problem using backtracking method

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.