其他分享
首页 > 其他分享> > AcWing 756. 蛇形矩阵

AcWing 756. 蛇形矩阵

作者:互联网

#include <bits/stdc++.h>

using namespace std;
const int N = 110;
int res[N][N], n, m, x, y, d = 0;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n*m; i++){
        res[x][y] = i;
        int a = x + dx[d], b = y + dy[d];
        if(a >= n || b >= m || a < 0 || b < 0 || res[a][b]){
            d = (d + 1) % 4;
            a = x + dx[d], b = y + dy[d];
        } 
        x = a, y = b;
    }
    
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++) cout << res[i][j] << ' ';
        cout << endl;
    }
    return 0;
}

 题目地址:https://www.acwing.com/activity/content/problem/content/1898/

 

标签:756,int,res,content,++,蛇形,dx,dy,AcWing
来源: https://www.cnblogs.com/xjtu-yzk/p/16369320.html