756
题目分析 :
要点如下:
1. 矩阵遍历:确定遍历方向
2. 确定遍历边界
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n,m;
int res[N][N];
int main()
{
cin>>n>>m;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
for(int x = 0,y = 0 ,k = 1,d = 0;k<=n*m;k++)
{
res[x][y] = k;
int a = x+dx[d],b = y + dy[d];
if(a<0 || a>=n ||b<0||b>=m||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;
}