#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int m,n;
cin >>m >> n;
vector<vector<int> > vec(m, vector<int>(n));
int count = 1;
int startx = 0;
int starty = 0;
int endx = m - 1;
int endy = n - 1;
int i, j;
while (startx <= endx && starty<=endy){
for (j = starty; j <= endy; j++){
vec[startx][j] = count++;
}
for (i = startx+1; i <= endx; i++){
vec[i][endy] = count++;
}
for (j = endy - 1; j >= starty; j--){
vec[endx][j] = count++;
}
for (i = endx - 1; i > startx; i--){
vec[i][starty] = count++;
}
startx++;
starty++;
endx--;
endy--;
}
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
cout << vec[i][j] << ends;
}
cout << endl;
}
return 0;
}
蛇形矩阵(百度2016实习招聘)
最新推荐文章于 2024-06-29 05:03:58 发布
本文介绍了一个使用C++实现的算法,该算法能够按螺旋顺序填充一个二维矩阵。通过定义四个边界来控制填充范围,并在每一轮填充后更新边界,确保填充路径正确无误。
&spm=1001.2101.3001.5002&articleId=51055855&d=1&t=3&u=872795312ae94d6d9dab1be9cb6bf1ab)
427

被折叠的 条评论
为什么被折叠?



