您的位置:首页 > 其它

蛇形填数

2016-04-04 10:16 274 查看
输入:3

输出:

1 2 3

8 9 4

7 6 5

#include<iostream>
#include<string.h>
using namespace std;
int A[1000][1000];
int main(){
int N;
cin>>N;
int p=1;
int total = N*N;
int x = 0;
int y = 0;
memset(A, 0, sizeof(A));
A[0][0] = 1;
while(p<total){
while(y+1<N&&A[x][y+1]==0){ A[x][++y] = ++p; }
while(x+1<N&&A[x+1][y]==0){ A[++x][y] = ++p; }
while(y-1>=0&&A[x][y-1]==0){ A[x][--y] = ++p; }
while(x-1>=0&&A[x-1][y]==0){ A[--x][y] = ++p;}
}

for(int i=0;i<N;++i){
for(int j=0;j<N-1;++j){
cout<<A[i][j]<<" ";
}
cout<<A[i][N-1]<<endl;
}
cout<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: