Triển khai Các thuật toán Mảng 2D
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| nested-loop/ˈnestɪd luːp/ | vòng lặp lồng nhau |
| main diagonal/meɪn daɪˈæɡənl/ | đường chéo chính |
| indexed/ˈɪndekst/ | có chỉ mục |
Grid algorithms
- 2-D algorithms combine the array patterns with nested-loop 嵌套循环 traversal.
- Sum / count / max over the whole grid: accumulate inside the inner loop.
- Search a grid: return the
[row][col]where found (or a "not found" signal). - The same accumulator/max logic, now over
rows × columnscells.
Row and column sums
- One row: fix
r, loopcover the columns, summingg[r][c]. - One column: fix
c, looprover the rows, summingg[r][c]. - Choosing which index to fix and which to loop is the key decision.
- A row sum sweeps across; a column sum sweeps down.

Row-major and column-major traversal of a 2D array
The diagonal
- The main diagonal 主对角线 of a square grid is the cells where row == column.
- Loop one index:
for (int i = 0; i < g.length; i++) { ... g[i][i] ... } g[0][0], g[1][1], g[2][2], …— a single loop, both indices equal.- Useful for square grids (identity checks, board diagonals).
Modifying cells
- To change a cell, use the indexed 带下标 nested loops and assign
g[r][c] = .... - The for-each version can't write back to the grid.
- Watch the two bounds — a wrong length reads or writes the wrong cell.
- Trace
[r][c]carefully, especially when rows and columns differ in count.
Decide which index to fix and which to loop — that's the difference between a row sum and a column sum. A row sum fixes r and loops c (g[r][c] across); a column sum fixes c and loops r (g[r][c] down). And modifying cells needs the indexed nested loops (g[r][c] = …); the for-each form can only read.
Summing column 0 of a grid:
int sum = 0;for (int r = 0; r < g.length; r++) { sum += g[r][0]; }- Fixes column
0, loops down the rows — a column sum.
2-D algorithms apply array patterns over nested loops: whole-grid sum/count/max/search, a row sum (fix r, loop c), a column sum (fix c, loop r), or the diagonal (g[i][i]). Modifying cells needs the indexed loops (g[r][c] = …); mind both bounds (g.length rows, g[0].length columns).
Tính tổng cột 0
g[0][0]=1, g[1][0]=4, g[2][0]=7: tổng chạy 1, 5, 12.
Để tính tổng MỘT hàng r, bạn...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tổng hàng cố định hàng, duyệt các cột.
Để tính tổng MỘT cột c, bạn...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tổng cột cố định cột, duyệt các hàng.
Đường chéo chính của một lưới vuông là các ô mà...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
g[0][0], g[1][1], g[2][2] — chỉ số duy nhất, cả hai bằng nhau.
Cấu trúc for-each có thể sửa đổi (ghi vào) các ô lưới.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
For-each chỉ đọc; hãy dùng vòng lặp theo chỉ số (g[r][c] = ...) để ghi.
Cột 0 của {{1,2},{4,5},{7,8}} là 1, 4, 7. Tổng của nó là bao nhiêu?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
1 + 4 + 7 = 12.
Sắp xếp thứ tự các bước để tính tổng cột c.
Độ khó 3/5 · 200 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Khởi tạo, duyệt hàng, tích lũy, trả về.