class="tags" href="/C.html" title=
c>
content_views"
class="tags" href="/C.html" title=
c>
class="markdown_views prism-tomorrow-night">
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/class="tags" href="/C.html" title=c>cb7f73class="tags" href="/C.html" title=c>cda1a14130840e356class="tags" href="/C.html" title=c>cb0b6f9e4.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="100" />
<
class="tags" href="/C.html" title=
c>
center>
博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳]
class="tags" href="/C.html" title=
c>
center>
<
class="tags" href="/C.html" title=
c>
center>
本文专栏: C++
class="tags" href="/C.html" title=
c>
center>
class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />
💯前言
- 在 C++ 编程领域class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>递归class="tags" href="/C.html" title=c>code> 是一种不可或缺的编程范式。它尤其适用于分层结构或<class="tags" href="/C.html" title=c>code>递归性质class="tags" href="/C.html" title=c>code>的问题class="tags" href="/C.html" title=c>c;为解决复杂问题提供了优雅的框架。本文将全面剖析 递归填充矩阵问题class="tags" href="/C.html" title=c>c;深入探讨<class="tags" href="/C.html" title=c>code>代码实现class="tags" href="/C.html" title=c>code>、理论背景、递归特性解析及其<class="tags" href="/C.html" title=c>code>优化与拓展class="tags" href="/C.html" title=c>code>。与此同时class="tags" href="/C.html" title=c>c;还将对递归算法的核心思想进行更深层次的理论分析class="tags" href="/C.html" title=c>c;并提供实际应用中的<class="tags" href="/C.html" title=c>code>潜在扩展方案class="tags" href="/C.html" title=c>code>。
C++ 参考手册
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/f0db7class="tags" href="/C.html" title=c>c348331440fa266b3class="tags" href="/C.html" title=c>c24f558694.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />
💯问题描述
构造一个矩阵class="tags" href="/C.html" title=c>c;其中每一层以顺时针方向填充数字class="tags" href="/C.html" title=c>c;从矩阵的外圈向内递归class="tags" href="/C.html" title=c>c;直至整个矩阵被完整填充。这一问题表面看似简单class="tags" href="/C.html" title=c>c;但其实隐藏了复杂的分层计算逻辑及递归思想的实践。
示例输入与输出
对于一个 5 × 5 的矩阵class="tags" href="/C.html" title=c>c;按照递归方式填充后class="tags" href="/C.html" title=c>c;结果如下:
<class="tags" href="/C.html" title=c>code>1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
class="tags" href="/C.html" title=c>code>
外圈的数字从左上角按顺时针顺序递增class="tags" href="/C.html" title=c>c;内圈依次类推class="tags" href="/C.html" title=c>c;直到填充完毕。这种从外向内、逐层递归的填充方法展示了递归算法的分解和简化过程。本文将以递归方法实现这一填充方案class="tags" href="/C.html" title=c>c;并进一步探讨其可能的扩展方式。
💯递归实现
以下为核心递归实现代码:
FillMatrix函数:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">FillMatrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token keyword">int sizeclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token keyword">int numclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token keyword">int offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 递归终止条件1:矩阵大小为0class="tags" href="/C.html" title=c>c;返回class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(size class="tags" href="/C.html" title=c>class="token operator">== class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token keyword">returnclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 递归终止条件2:矩阵大小为1class="tags" href="/C.html" title=c>c;填充中心点class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(size class="tags" href="/C.html" title=c>class="token operator">== class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= numclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">returnclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 填充外圈的四部分class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; i class="tags" href="/C.html" title=c>class="token operator">< size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; iclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= num class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 左侧一列matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= num class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 底部一行matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1 class="tags" href="/C.html" title=c>class="token operator">- iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= num class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">2 class="tags" href="/C.html" title=c>class="token operator">* class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 右侧一列matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1 class="tags" href="/C.html" title=c>class="token operator">- iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= num class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">3 class="tags" href="/C.html" title=c>class="token operator">* class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 顶部一行class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 递归调用class="tags" href="/C.html" title=c>c;进入内圈class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">FillMatrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">2class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, num class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">4 class="tags" href="/C.html" title=c>class="token operator">* class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, offset class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
main函数:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>const class="tags" href="/C.html" title=c>class="token keyword">int N class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">5class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 定义矩阵大小class="tags" href="/C.html" title=c>class="token keyword">int matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 初始化矩阵为全 0class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 调用递归函数填充矩阵class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">FillMatrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 打印矩阵class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; i class="tags" href="/C.html" title=c>class="token operator">< Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; iclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int j class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; j class="tags" href="/C.html" title=c>class="token operator">< Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; jclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">setwclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token number">4class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator"><< matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[jclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 格式化输出class="tags" href="/C.html" title=c>c;确保对齐class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/9b08class="tags" href="/C.html" title=c>ca1d82724class="tags" href="/C.html" title=c>c759class="tags" href="/C.html" title=c>c752d24class="tags" href="/C.html" title=c>c221class="tags" href="/C.html" title=c>cfd5.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />
💯参数解析
函数参数详解
- <class="tags" href="/C.html" title=c>code>int matrix[N][N]class="tags" href="/C.html" title=c>code>:目标矩阵class="tags" href="/C.html" title=c>c;大小固定为 <class="tags" href="/C.html" title=c>code>N × Nclass="tags" href="/C.html" title=c>code>。
- <class="tags" href="/C.html" title=c>code>int sizeclass="tags" href="/C.html" title=c>code>:当前需要填充的子矩阵大小。
- <class="tags" href="/C.html" title=c>code>int numclass="tags" href="/C.html" title=c>code>:填充的起始数字。
- <class="tags" href="/C.html" title=c>code>int offsetclass="tags" href="/C.html" title=c>code>:当前子矩阵在整体矩阵中的偏移位置。
填充顺序分析
- 左侧一列:从顶部到底部依次填充:<class="tags" href="/C.html" title=c>code>matrix[offset + i][offset]class="tags" href="/C.html" title=c>code>。
- 底部一行:从左到右依次填充:<class="tags" href="/C.html" title=c>code>matrix[offset + size - 1][offset + i]class="tags" href="/C.html" title=c>code>。
- 右侧一列:从底部到顶部依次填充:<class="tags" href="/C.html" title=c>code>matrix[offset + size - 1 - i][offset + size - 1]class="tags" href="/C.html" title=c>code>。
- 顶部一行:从右到左依次填充:<class="tags" href="/C.html" title=c>code>matrix[offset][offset + size - 1 - i]class="tags" href="/C.html" title=c>code>。
递归终止条件
- <class="tags" href="/C.html" title=c>code>size == 0class="tags" href="/C.html" title=c>code>:无需再填充class="tags" href="/C.html" title=c>c;递归结束。
- <class="tags" href="/C.html" title=c>code>size == 1class="tags" href="/C.html" title=c>code>:矩阵缩小至单个点class="tags" href="/C.html" title=c>c;直接填充中心数字。
通过这样的顺序class="tags" href="/C.html" title=c>c;我们可以层层缩小矩阵的计算范围class="tags" href="/C.html" title=c>c;最终实现完整的矩阵填充。递归的设计核心在于其逻辑的分层性和简洁性class="tags" href="/C.html" title=c>c;使得代码能够以最少的复杂度完成较为繁琐的操作。
💯示例解析
以下以 <class="tags" href="/C.html" title=c>code>FillMatrix(matrix, 5, 1, 0)class="tags" href="/C.html" title=c>code> 为例:
第一层递归
- <class="tags" href="/C.html" title=c>code>size = 5class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>offset = 0class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>c;起始数字为 <class="tags" href="/C.html" title=c>code>1class="tags" href="/C.html" title=c>code>:
- 左侧一列:1, 2, 3, 4, 5
- 底部一行:6, 7, 8, 9
- 右侧一列:10, 11, 12, 13
- 顶部一行:14, 15, 16
第二层递归
- 调用 <class="tags" href="/C.html" title=c>code>FillMatrix(matrix, 3, 17, 1)class="tags" href="/C.html" title=c>code>:
- 左侧一列:17, 18, 19
- 底部一行:20, 21
- 右侧一列:22, 23
- 顶部一行:24
第三层递归
- 调用 <class="tags" href="/C.html" title=c>code>FillMatrix(matrix, 1, 25, 2)class="tags" href="/C.html" title=c>code>:
最终输出
<class="tags" href="/C.html" title=c>code>1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
class="tags" href="/C.html" title=c>code>
通过该示例class="tags" href="/C.html" title=c>c;我们能够直观理解递归在每一步是如何逐步分解问题并完成填充的。
💯递归算法解析
理论基础
递归算法通过将问题分解为规模更小的子问题class="tags" href="/C.html" title=c>c;从而达到解决整体问题的目的。其核心包括以下三要素:
- 递归终止条件:防止无限递归。
- 在本例中class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>size == 0class="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>size == 1class="tags" href="/C.html" title=c>code> 即为递归的终止条件。
- 递归子问题:将问题缩小为更简单的版本。
- 在本例中class="tags" href="/C.html" title=c>c;矩阵每次递归缩小外圈class="tags" href="/C.html" title=c>c;最终进入中心点。
- 递归递推关系:问题规模如何递减class="tags" href="/C.html" title=c>c;以及状态如何传递。
- 本例中递归公式为:<class="tags" href="/C.html" title=c>code>FillMatrix(matrix, size - 2, num + 4 * (size - 1), offset + 1)class="tags" href="/C.html" title=c>code>。
递归算法之所以高效class="tags" href="/C.html" title=c>c;是因为它能够自然地实现问题的分治思想class="tags" href="/C.html" title=c>c;避免了手动控制循环的复杂性。尤其在分层结构中class="tags" href="/C.html" title=c>c;递归能够显著减少代码冗余并提升逻辑的简洁性。
优点
递归实现能够以最少的代码完成复杂的分层逻辑class="tags" href="/C.html" title=c>c;其结构直观class="tags" href="/C.html" title=c>c;便于扩展。其应用场景广泛class="tags" href="/C.html" title=c>c;从树形结构的遍历到动态规划中的状态转移class="tags" href="/C.html" title=c>c;递归几乎无处不在。
缺点
递归可能因调用栈深度过大导致性能下降或栈溢出。在实际使用中class="tags" href="/C.html" title=c>c;需要对递归深度进行评估class="tags" href="/C.html" title=c>c;并通过优化递归设计来降低潜在风险。
💯优化与拓展
1. 动态内存分配
对于运行时动态确定矩阵大小的情况class="tags" href="/C.html" title=c>c;可以采用如下动态分配方式:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token keyword">intclass="tags" href="/C.html" title=c>class="token operator">*class="tags" href="/C.html" title=c>class="token operator">* matrix class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token keyword">new class="tags" href="/C.html" title=c>class="token keyword">intclass="tags" href="/C.html" title=c>class="token operator">*class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; i class="tags" href="/C.html" title=c>class="token operator">< Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; iclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token keyword">new class="tags" href="/C.html" title=c>class="token keyword">intclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>
这种方式能够显著提升程序的灵活性class="tags" href="/C.html" title=c>c;使其能够适应更大规模的矩阵计算需求。
2. 非递归实现
递归可以转换为迭代算法以避免调用栈问题:
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">FillMatrixIterativeclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[Nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token keyword">int nclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int num class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int offset class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; offset class="tags" href="/C.html" title=c>class="token operator">< class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(n class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">/ class="tags" href="/C.html" title=c>class="token number">2class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; offsetclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int size class="tags" href="/C.html" title=c>class="token operator">= n class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">2 class="tags" href="/C.html" title=c>class="token operator">* offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; i class="tags" href="/C.html" title=c>class="token operator">< size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; iclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= numclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= numclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1 class="tags" href="/C.html" title=c>class="token operator">- iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= numclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;matrixclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offsetclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">]class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[offset class="tags" href="/C.html" title=c>class="token operator">+ size class="tags" href="/C.html" title=c>class="token operator">- class="tags" href="/C.html" title=c>class="token number">1 class="tags" href="/C.html" title=c>class="token operator">- iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= numclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
通过迭代方式实现矩阵填充class="tags" href="/C.html" title=c>c;避免了递归可能导致的栈溢出问题class="tags" href="/C.html" title=c>c;同时在某些场景下也能提升性能。
3. 拓展填充模式
除顺时针填充外class="tags" href="/C.html" title=c>c;还可以实现:
- 逆时针填充。
- 对角线填充。
- 双向递归:同时从外圈和内圈向中间填充class="tags" href="/C.html" title=c>c;提升并行性。
通过拓展填充模式class="tags" href="/C.html" title=c>c;可以满足不同的应用需求class="tags" href="/C.html" title=c>c;例如图形处理、数据加密中的分层结构设计等。
💯小结
- class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/4class="tags" href="/C.html" title=c>cf2beba4eclass="tags" href="/C.html" title=c>c24f6fbclass="tags" href="/C.html" title=c>cb17b5class="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>c7609927.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />
本文深入探讨了 递归填充矩阵 的<class="tags" href="/C.html" title=c>code>实现与理论背景class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>c;并结合实例展示了递归在解决分层问题中的独特优势。尽管递归在解决复杂问题时表现出色class="tags" href="/C.html" title=c>c;但需注意其<class="tags" href="/C.html" title=c>code>潜在的性能瓶颈class="tags" href="/C.html" title=c>code>与调用栈深度限制。在实际应用中class="tags" href="/C.html" title=c>c;可根据具体需求选择递归或非递归实现。同时class="tags" href="/C.html" title=c>c;通过<class="tags" href="/C.html" title=c>code>动态内存分配class="tags" href="/C.html" title=c>code>与模式拓展class="tags" href="/C.html" title=c>c;能够进一步提升算法的适用性。
通过对本文内容的学习class="tags" href="/C.html" title=c>c;希望读者能够更全面地理解 递归的本质 及其在实际问题中的应用潜力class="tags" href="/C.html" title=c>c;并能够在未来的实践中<class="tags" href="/C.html" title=c>code>灵活运用class="tags" href="/C.html" title=c>code>这些方法与思想来解决更复杂的工程问题。
class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />