The definition of a two-dimensional array is similar to that of a one-dimensional array.

Type name Array name[][]; example: int a[3][3];

Sometimes a two-dimensional array is called a matrix, and the first square brackets are called the rows of the rectangle, and the second square brackets are called the columns of the rectangle.

It seems a bit pale to just talk about the theory, but the main thing is to realize the charm of two-dimensional arrays in doing the questions.

### Example 1: Guo Yuan picking apples

topic description

Guo Yuan came to an apple grove one day, and each tree in it had different numbers of apples. Guo Yuan could only take the apples from the same tree. Drop and pick the apples on the tree where he is and take them away (assuming that Guo Yuan will walk through every apple tree), ask Guo Yuan during the whole process of picking apples, what is the difference between the maximum number of apples he carries and the minimum number of apples? How many?

enter

m, n (that is, the number of rows and columns of fruit trees in the apple grove, 0<n, m<=10) m rows and n columns of data (that is, the number of apples on each tree, no more than 100)

output

1 number (the difference between the maximum number of apples and the minimum number of apples that Guo Yuan carried on his body during the whole process of picking apples)

sample input

4 3 2 6 5 1 3 7 5 3 5 1 7 12

sample output

11

AC code:

#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ULL; const int MAXN=10000012; const int N=1e6+7; const int INF=0x3f3f3f3f; const int P=131; //This question is to find the difference between the largest element and the smallest element in the two-dimensional array, and compare the water. int apple[20][20],maxn,minn,n,m; int main(){ maxn=0,minn=999; //Due to the need to compare later, set the value of maxn to be smaller and the value of minn to be larger cin >> n >> m; for(int i=0;i<n;i++){ //Two loops are required to enter the array elements for(int j=0;j<m;j++){ cin >> apple[i][j]; if(maxn<apple[i][j]){ //Compare to find the largest element in the array maxn=apple[i][j]; } if(minn>apple[i][j]){ //Compare to find the smallest element in an array minn=apple[i][j]; } } } cout << maxn-minn << "\n"; return 0; }

### Example 2: Solving for the number of mines

topic description

Please program to calculate how many landmines are buried in this area.

For example: there is a 5 * 6 mine distribution map below, and the area where the mines are buried is marked with a gray background. There are 10 mines in this area.

12 | 35 | 90 | 21 | 1 | 9 |

91 | 3892 | 8749 | 342 | 3421 | 89 |

1881 | 1111 | 39 | 4 | 1 | 34 |

9 | 93 | 34 | 12 | 45 | 0 |

67 | 4 | 34 | 19 | 235 | 32 |

AC code:

#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ULL; const int MAXN=10000012; const int N=1e6+7; const int INF=0x3f3f3f3f; const int P=131; int n,m; int dl[120][120]; int main(){ int cnt=0; cin >> n >> m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin >> dl[i][j]; int t=0; while(dl[i][j]>0){ //Find the sum of the digits of the elements t+=dl[i][j]%10; dl[i][j]/=10; } if(t%2==1){ //If it is odd, add one to cnt cnt++; } } } cout << cnt << "\n"; return 0; }

### Example 3: Number of Bullseyes

topic description

James found out that there is such a type of number in the two-dimensional array. This number is just larger than the numbers in the four directions (up, down, left, and right) (because it needs to be larger than the numbers in the four directions, this number cannot be in the first row. , the last row, the first column, and the last column), James calls them bullseye numbers, please program to find out the bullseye numbers of a two-dimensional array, and output them.

enter

The first line contains two integers n and m (both n and m are integers between 4 and 100), which means that the next two-dimensional array has n rows and m columns.

Next n lines, each line has m integers.

output

Please output the number of bullseyes that meet the conditions in the order of input, one per line.

sample input

4 4 1 2 3 4 5 6 5 8 9 1 11 10 13 4 5 16

sample output

6 11

#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ULL; const int MAXN=10000012; const int N=1e6+7; const int INF=0x3f3f3f3f; const int P=131; int n,m; int a[120][120]; int main(){ cin >> n >> m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin >> a[i][j]; } } for(int i=1;i<n-1;i++){ for(int j=1;j<m-1;j++){ if(a[i][j]>a[i-1][j]&&a[i][j]>a[i+1][j]&&a[i][j]>a[i][j-1]&&a[i][j]>a[i][j+1]){ //Compare the surrounding array elements, and output if the conditions are met cout << a[i][j] << "\n"; } } } return 0; }

### Example 4: Digital Trends II

topic description

Input an integer N and output the corresponding square matrix.

enter

an integer N. ( 0 < n < 10 )

output

A square matrix with field width 3 for each number.

sample input

5

sample output

21 22 23 24 25 16 17 18 19 20 11 12 13 14 15 6 7 8 9 10 1 2 3 4 5

#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ULL; const int MAXN=10000012; const int N=1e6+7; const int INF=0x3f3f3f3f; const int P=131; int n; int a[12][12]; int main(){ cin >> n; int t=1; for(int i=n-1;i>=0;i--){ for(int j=0;j<n;j++){ a[i][j]=t++; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("%3d",a[i][j]); } cout << "\n"; } return 0; }

Note: These are relatively simple questions in two-dimensional arrays. The main function is to further understand the application of two-dimensional arrays. If you want to solve more difficult questions, it is recommended to practice on Luogu or other platforms.