Backtracking algorithm basics

Backtracking

Backtracking is also known as heuristics, which first temporarily abandon restrictions on the issue of size, the solution candidate and issue some order to enumerate one by one and test. When a candidate solution can not be found in the current solution, the next candidate on the choice of a solution; if the current candidate when the lifting of the scale of the problem does not meet the requirements, meet all other requirements, to continue to expand the scale of the current candidate solutions, and continue to test. If the current candidate solution meets all the requirements, including the scale of the problem, the candidate is a solution to the problem solution. In backtracking, the solution to give up the current candidate, the next process to find a candidate solution is called backtracking. Expand the scale of the current candidate solutions to continue to test the process is called forward tentatively.

[Problem] crossword Problem Description: The 3 × 3 matrix of squares to be filled within a 9-digit number from 1 to N (N≥10), a fill each square integer, like all two adjacent integers in the two squares and a prime number. Try to obtain the full range of digital mapping method to meet this requirement.

Backtracking algorithms classic problem-solving ideas:

Text Description : First fill in the number for the first box and fill in the correct values in the current premise for the next box filled reasonable value, in order to fill in the box next value, if the current value does not reasonable, press a certain order (usually in ascending order) modify the value, and remove the answer to all the solutions of (pruning, the solution after the branch is not considered), all values if the current cell is not in line with required, the adjustment back to a square value until 9 squares are populated with the correct answers to the current value is recorded, and then adjust the value of the current cell (box 9) in order to strike the correct solution if the current Solutions of all the squares have to traverse a square back to the adjustment value until the value back to the first grid and the first grid can not be readjusted, then the answer is complete.

Logic flow diagram

① fill 1st value;
② fill the second value;
③ not meet the requirements, modifying the current value in a certain order (usually in ascending order), and the result of all solutions removed (pruned);
④ if already the maximum value still does not satisfy the requirement, back to the previous square, a square value before modification sequentially;
⑤ meet the requirements, continue to fill values (steps 3 and 4 during the repetitive inspection);
⑥ fill the last one square;
⑦ to meet the requirements, records the current solution, in order to modify the current value, and repeat steps 3 and 4 of the check;
⑧ if still not back to a first modified value box, traversal should end.

【program】

# include <stdio.h>
# define N 12
void write(int a[ ])
{ 
    int i,j;
    for (i=0;i<3;i++)
    { 
        for (j=0;j<3;j++)
            printf(“%3d”,a[3*i+j]);
        printf(“\n”);
    }   
scanf(“%*c”);
}
int b[N+1];
int a[10];
int isprime(int m)
{ 
    int i;
    int primes[ ]={2,3,5,7,11,17,19,23,29,-1};
    if (m==1||m%2=0) return 0; 
        for (i=0;primes[i]>0;i++)
    if (m==primes[i]) return 1;
        for (i=3;i*i<=m;)
        { 
        if (m%i==0) return 0;
            i+=2;
        }
return 1;
}

int checkmatrix[ ][3]={ {-1},{0,-1},{1,-1},{0,-1},{1,3,-1},{2,4,-1},{3,-1},{4,6,-1},{5,7,-1}};

int selectnum(int start)
{ 
int j;
for (j=start;j<=N;j++)
    if (b[j]) 
        return j;
return 0;
}

int check(int pos)
{ 
    int i,j;
    if (pos<0) 
        return 0;
    for (i=0;(j=checkmatrix[pos][i])>=0;i++)
        if (!isprime(a[pos]+a[j])
            return 0;
return 1;
}

int extend(int pos)
{ 
    a[++pos]=selectnum(1);
    b[a][pos]]=0;
    return pos;
}

int change(int pos)
{ 
    int j;
    while (pos>=0&&(j=selectnum(a[pos]+1))==0)
        b[a[pos--]]=1;
    if (pos<0) 
        return1;
    b[a[pos]]=1;
    a[pos]=j;
    b[j]=0;
    return pos;
}

void find()
{ 
    int ok=0,pos=0;
    a[pos]=1;
    b[a[pos]]=0;
    do {
    if (ok)
        if (pos==8)
        { 
            write(a);
            pos=change(pos);
        }
        else pos=extend(pos);
    else pos=change(pos);
    ok=check(pos);
    } while (pos>=0)
}

void main()
{ 
    int i;
    for (i=1;i<=N;i++)
        b[i]=1;
    find();
}

The basic framework of backtracking algorithm

If a program to find all the solutions, then the output will find the solution, should continue to adjust integer fill the last position on the release of the next attempt to find a solution, the corresponding algorithm is as follows:

{ 
    int m=0,ok=1;
    int n=8;
    do{
        if (ok)
            {
                if (m==n)
                    {
                        输出解;
                        调整;  
                    }
                else
                    扩展;
            }
        else
            调整;
      ok=检查前m个整数填放的合理性;        
      }while (m!=0);
}
Published 10 original articles · won praise 11 · views 5988

Guess you like

Origin blog.csdn.net/disILLL/article/details/78072157