#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int M[100][100]={0};
int x,y;
int tot=M[x=0][y=0]=1;
int n,m;
cin>>n>>m;
while(tot<n*m)
{
while(y+1<m&&!M[x][y+1]) M[x][y=y+1]=++tot;
while(x+1<n&&!M[x+1][y]) M[x=x+1][y]=++tot;
while(y-1>=0&&!M[x][y-1]) M[x][y=y-1]=++tot;
while(x-1>=0&&!M[x-1][y]) M[x=x-1][y]=++tot;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<M[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
蛇形矩阵是基础,一定要掌握.