[A] ACM training time complexity, recursion and enumerate

time complexity

Easy to understand explanation: https://blog.csdn.net/qq_41523096/article/details/82142747#commentsedit

Common time complexity (Big O notation)
(. 1) O (. 1): a constant order, run time constant
(2) O (logn): of the order, such as binary search algorithm
(3) O (n): Linear order, such as looking for the maximum number of n-
(. 4) O (nlogn): of the order, such as the fast sort algorithm
(5) O (n ^ 2 ): order of the square, such as selection sort, bubble sort
(6) O (n ^ 3): the cubic order, such as the multiplication of two matrices of order n
(7) O (2 ^ n ): exponential order, if all elements of the set of n sub-sets of algorithm
(8) O (n!) : factorial order, as all permutations of n elements algorithm

Recursive enumeration

Not explain the definition of
example
1, to improve the enumeration
enter a number N (0 <N <8) and a number K (0 <k <= 5 ), requires: 1 ~ N from the N numbers, randomly selected K digits (numerals may be repeated option), press the magnitude of the output of all programs.

#include <stdio.h>
int n,k;
int tt[9];
void di(int pt)
{
	int i;
	if(pt>k)
	{
		for(i=1;i<=k;i++)
		printf("%d",tt[i]);
		printf("\n");
	}
	else
	{
		for(i=1;i<=n;i++)
		{
		tt[pt]=i;
		di(pt+1);
	    }
	}
}
int main()
{
	scanf("%d%d",&n,&k);
	di(1);
	return 0;
}

2, the whole arrangement
enter a number N (0 <N <8) , the output from the full array of the N 1 ~ N digits, in ascending order.

#include<stdio.h>
#include<math.h>
int main(){
	int used[10];
	int N,i,j,k,s,t;
	int min=0,max=0;
	scanf("%d",&N);
	for(i=1;i<=N;i++)
    {
	min=min+i*pow(10,N-i);
	max=max+(N-i+1)*pow(10,N-i);
	}
	for(i=min;i<=max;i++)
	{
			used[1]=0;
			used[2]=0;
			used[3]=0;
			used[4]=0;
			used[5]=0;
			used[6]=0;
			used[7]=0;
			used[8]=0;
		for(j=1,k=i;j<=N;j++)
		{
		s=k/pow(10,N-j);
		k=k-(s*pow(10,N-j));
			switch(s)
				{
					case 0:used[0]=used[0]+1;break;
					case 1:used[1]=used[1]+1;break;
					case 2:used[2]=used[2]+1;break;
					case 3:used[3]=used[3]+1;break;
					case 4:used[4]=used[4]+1;break;
					case 5:used[5]=used[5]+1;break;
					case 6:used[6]=used[6]+1;break;
					case 7:used[7]=used[7]+1;break;
					case 8:used[8]=used[8]+1;break;
					case 9:used[9]=used[9]+1;break;
				}
		}
    for(j=1,k=0;j<=N;j++)
        {
            if(used[j]!=1)
            break;
            else 
            k=k+1;
        }
			if(k==N)
            printf("%d\n",i);
	}
}

3, N queens
in a checkerboard N * N N queens placed such that they do not attack each other (i.e., does not allow any two queens in the same row, same column, is not allowed to be at an angle with the board frame 45 on the diagonal.
your job is that for a given N, how many legitimate method of obtaining placed there.

#include <stdio.h>
#define NUMS 10
int N;
int chessboard[11][11];
int cal;
int dfs_check(int row,int column,int k)
{
    if(row>k)
    {
        cal++;
        return 1;
    }
    int i=0;
    int j=0;
    for( i = 1; i < row; i++)
       
    if(chessboard[i][column] == 1)
    return 0;

    /* 左右上方45度角检查是否可以*/
    /* 左上方*/
    for( i=row-1,j=column-1;i>0&&j>0;i--,j--)
    if(chessboard[i][j] == 1)
    return 0;
    /* 右上方*/
    for( i=row-1,j=column+1;i>0&&j<=k;i--,j++)
    if(chessboard[i][j] == 1)
    return 0;
    /*标记这个位置成功了*/
    chessboard[row][column] = 1;

    /*进行下一行搜索*/
    for( i=1;i<=k;i++)
    if(dfs_check(row+1,i,k)==1)
    break;

    chessboard[row][column] = 0;
    return 0;
}
int main()
{
    int i,j,k;
    int count[11];
    /*打表*/
    for(k=1;k<=NUMS;k++)
    {
        count[k] = 0;
        cal = 0;
        /*首先将棋盘初始化全部置为0*/
        for(i=0;i<=NUMS;i++)
        for(j=0;j<=NUMS;j++)
        chessboard[i][j]=0;
        for(i=1;i<=k;i++)
        dfs_check(1,i,k);
        count[k] = cal;
    }

    while(scanf("%d",&N)!=EOF&&N!=0)
    printf("%d\n",count[N]);
    return 0;
}

To be continued

Guess you like

Origin blog.csdn.net/slozer/article/details/94644907