文章
61
粉丝
137
获赞
18
访问
39.7k
#include<bits/stdc++.h>
using namespace std;
int main(){
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1}; //下右上左
int n;
cin >> n;
int ans[n+2][n+2];
int visit[n+2][n+2];
memset(visit, 0, sizeof(visit));
for(int i = 0; i < n+2; i++){
visit[i][0] = 1;
visit[i][n+1] = 1;
visit[0][i] = 1;
visit[n+1][i] = 1;
}
int x = 1;
int y = 1;
int cnt = 0;
int judge = 0;
while(cnt != n*n){
//访问该方向上的所有未被访问的格子
while(visit[x][y] == 0 && cnt != n*n){
ans[x][y] = ++cnt;
visit[x][y] = 1;
x += dir[judge][0];
y += dir[judge][1];
}
//该方向上遇到已经访问的格子,从方向回退一格
x -= dir[judge][0];
y -= dir[judge][1];
//改变方向
judge = (judge + 1) % 4;
x += dir[judge][0];
y += dir[judge][1];
}
//输出旋转矩阵
for(int i = 1;i <= n;i++){
for...
登录后发布评论
暂无评论,来抢沙发