List examples

If you don’t know what an enumeration is:Portal

P2089

Piggy Hanke especially likes to eat roasted chicken (they are the same animals, so why rush to roast them!) Hanke eats chicken very special, why is it special? Because he has 10 10 10 ingredients (mustard, cumin, etc.), each ingredient can be placed 1 1 1to 3 3 3 grams, the deliciousness of any roasted chicken is the sum of the mass of all ingredients. Now, Hanke wants to know, if you give you a deliciousness level n n n,请输入这 10 10 All combinations of 10 ingredients.

Input format
A positive integer n n n, Appears to be delicious. n ≤ 5000 n \leq 5000 n5000

Output format
The first line, the total number of solutions
The second line to the end, 10 10 a>The number of 10 represents the mass of each ingredient, arranged in dictionary order.
If there is no method that meets the requirements, just output one on the first line 0 0 0

Example of importation
11

Example of export
10
1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 2 1
1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 2 1 1 1
1 1 1 1 1 2 1 1 1 1
1 1 1 1 2 1 1 1 1 1
1 1 1 2 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1

Idea: This is a typical violent enumeration question, because the data range is very small, we only need ten nested for loops to enumerate every possibility.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int INF = 0x7fffffff;


int main() {  
    int a, b, c, d, e, f, g, h, i, j, n, res=0;  
    cin >> n;  
    for(a = 1; a <= 3; a++) {  
        for(b = 1; b <= 3; b++) {  
            for(c = 1; c <= 3; c++) {  
                for(d = 1; d <= 3; d++) {  
                    for(e = 1; e <= 3; e++) {  
                        for(f = 1; f <= 3; f++) {  
                            for(g = 1; g <= 3; g++) {  
                                for(h = 1; h <= 3; h++) {  
                                    for(i = 1; i <= 3; i++) {  
                                        for(j = 1; j <= 3; j++) { 
                                            if(a+b+c+d+e+f+g+h+i+j == n) {  
                                                res++;  
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
    cout << res << endl;  
    for(a = 1; a <= 3; a++) {  
        for(b = 1; b <= 3; b++) {  
            for(c = 1; c <= 3; c++) {  
                for(d = 1; d <= 3; d++) {  
                    for(e = 1; e <= 3; e++) {  
                        for(f = 1; f <= 3; f++) {  
                            for(g = 1; g <= 3; g++) {  
                                for(h = 1; h <= 3; h++) {  
                                    for(i = 1; i <= 3; i++) {  
                                        for(j = 1; j <= 3; j++) { 
                                            if(a+b+c+d+e+f+g+h+i+j == n) {  
                                    			cout << a << " " << b << " "; 
                                                 cout << c << " " << d << " ";
                                                 cout << e << " " << f << " ";
                                   				cout << g << " " << h << " ";
                                                 cout << i << " " << j << endl; 
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }  
	return 0;  
}  

P3654 First Step

Import n , m , k n,m,k n,m,k, import one piece n × m n\times m n×The character matrix of m can be output as follows: k k kThe number of empty spaces for people to line up in a row, in either row or row. n , m ≤ 100 , k < m i n ( n , m ) n,m\leq 100, k < min(n,m) n,m100,k<min(n,m)

Idea: The data range in the question is very small. We can enumerate it directly, starting from each point, and search horizontally and vertically k k k, if no obstacles are encountered, add 1 1 1, of course we need special considerations when k = 1 k=1 k=When 1, the horizontal and vertical directions are the same, which is equivalent to us calculating the amount twice, so we need to specially judge and output the answer / 2 /2 /2

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int INF = 0x7fffffff;

int n, m, k, res;
char ch[110][110];

int main(){
	cin >> n >> m >> k;
	for(int i = 1;  i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> ch[i][j];
		} 
	}
	for(int i = 1; i <= n; i++){//横向判断
		for(int j = 1; j <= m; j++){
			if(ch[i][j] != '#'){
				bool b = true;
				for(int a = 0; a < k; a++){
					if(ch[i][j+a] != '.'){
						b = false;
						break;
					} 
				}
				if(b) res++;
			}
		}
	}
	for(int i = 1; i <= n; i++){//竖向判断
		for(int j = 1; j <= m; j++){
			if(ch[i][j] != '#'){
				bool b = true;
				for(int a = 0; a < k; a++){
					if(ch[i+a][j] != '.'){
						b = false;
						break;
					} 
				}
				if(b) res++;
			}
		}
	}
	if(k == 1) cout << res / 2 << endl;//特判当k=1
	else cout << res << endl;
	return 0;
}

P3392 Paint the national flag

The law of a certain country stipulates that as long as one is composed of N × M N\times M N×A flag composed of M small squares that meets the following rules is a legal national flag.

  • The grids in several rows (at least one row) from the top are all white;
  • The grids in the next few rows (at least one row) are all blue;
  • The remaining rows (at least one row) are all red;

There is a checkerboard-shaped cloth, divided into N N N M M M columns of grids, each grid is one of white, blue, and red. Little a hopes to change this cloth into the national flag of the country by applying paint on some grids. , covering the previous color. Little A is very lazy and hopes to paint the least number of checks to make this piece of cloth a legal national flag.

Input format

The first line is two integers N N N, M M M N , M ≤ 50 N,M \leq 50 N,M50
coming N N The N row is a matrix, and each small square of the matrix is ​​one of W (white), B (blue), and R (red).

Output format

An integer indicating the minimum number of blocks to be painted.

Input example Export example
4 5 11
WRWRW
BWRWB
WRWRW
RWBWR

Idea: We know that the data range is very small, so we decisively choose violent enumeration. According to the question, we can open three number arrays like this W [ i ] , B [ i ] , R [ i ] W[i],B[i],R[i] W[i],B[i],R[i]Separate display i i iThe number of grids required to paint the rows white, blue, and red. Assume that 1 1 1Going first i i irow is white, number i + 1 i+1 i+1Going first j j jThe line is blue, the first j + 1 j+1 j+1Going first n n The row n is red, then the grid to be painted at this time is W [ i ] + B [ j ] − B [ i ] + R [ n ] − R [ j ] W[i] + B[j]-B[i]+R[n]-R[j] W[i]+B[j]B[i]+R[n]R[j]. We just need one i , j i,j i,j, just take the minimum value.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int INF = 0x7fffffff;

int n, m, res = INF;
string str;
int W[55], B[55], R[55];

inline int check(char ch){
	int res = 0;
	for(int i = 0; i < m; i++){
		if(str[i] != ch) res++;
	}
	return res;
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		cin >> str;
		W[i] = W[i-1] + check('W');
		B[i] = B[i-1] + check('B');
		R[i] = R[i-1] + check('R');
	}
	for(int i = 1; i < n - 1; i++){
		for(int j = i + 1; j < n; j++){
			res = min(res,W[i]+B[j]-B[i]+R[n]-R[j]);
		}
	}
	cout << res << endl;
	return 0;
} 

P1149 Matchstick Equation

​ 给你 n n nHow many shapes can you spell out with a match stick A + B = C A+B=C A+B=The equation of C? A A A B B B C C C is an integer spelled out with matchsticks (if the number is non-zero, the highest bit cannot be 0 0 0)。
​ 注意:

  1. The plus sign and the equal sign each require two matchsticks
  2. Result $A\neq B ,则 ,则 ,则A+B=C give give andB+A=C are treated as different equations. Treated as different equations is treated as a different equation (A,B,C\geq 0)$.
  3. n nnAll matchsticks must be used

Input format: an integer n n n ( n ≤ 24 ) (n \leq 24) (n24)

Output format: an integer, the number of different equations that can be formed.

Idea: Because there is only 20 20 at most20A match is used to spell three numbers, so we only need to record 1 1 1to 1000 1000 All numbers in 1000 are counted using matches.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int INF = 0x7fffffff;

int main()
{
	int arr[2001]={6},n,src[10]={6,2,5,5,4,5,6,3,7,6},sum,i,j;
	cin>>n;
	for(i=1;i<=2000;i++)
	{
		j=i;
		while(j>=1)
		{
			arr[i]+=src[j%10];
			j/=10;
		}
	}
	for(i=0;i<1000;i++)
		for(j=0;j<1000;j++)
			if(arr[i]+arr[j]+arr[i+j]+4==n) sum++;
	cout<<sum<<endl;
	return 0;
 }  

Guess you like

Origin blog.csdn.net/weixin_45652283/article/details/131244467