思路:之前做过一个蛇形矩阵,一样的道理,控制输出即可
#include <bits/stdc++.h>
using namespace std;
int a[205][205];
int main(){
int m, n;
int i, j, num = 0;
memset(a, -1, sizeof(a));
scanf("%d%d", &m, &n);
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &a[i][j]);
int tot = 0, x = -1, y = 0;
while(tot < m * n)
{
while(x + 1 < m && a[x + 1][y] != -1)
{
printf("%d ", a[++x][y]);
a[x][y] = -1;
++tot;
}
while(y + 1 < n && a[x][y + 1] != -1)
{
printf("%d ", a[x][++y]);
a[x][y] = -1;
++tot;
}
while(x - 1 >= 0 && a[x - 1][y] != -1)
{
printf("%d ", a[--x][y]);
a[x][y] = -1;
++tot;
}
while(y - 1 >= 0 && a[x][y - 1] != -1)
{
printf("%d ", a[x][--y]);
a[x][y] = -1;
++tot;
}
}
return 0;
}