Code:

#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[101][101];
int fx[5] = { 0,0,1,0,-1 }, fy[5] = { 0,1,0,-1,0 };
void fun(int x, int y, int k) {
    a[x][y] = k;
    int tx, ty;
    for (int i = 1; i <= 4; i++) {
        tx = x + fx[i];
        ty = y + fy[i];
        if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] == 0) {
            fun(tx, ty, k + 1);
        }
    }
}
int main() {
    cin >> n >> m;
    fun(1, 1, 1);
    int i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            if (a[i][j] < 10) {
                cout << " ";
            }
            cout << " ";
            cout << a[i][j];
        }
        cout << endl;
    }
    return 0;
}

合格证:

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐