蛇形填数
2016-04-04 10:16
274 查看
输入:3
输出:
1 2 3
8 9 4
7 6 5
输出:
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; }
相关文章推荐
- 83. Remove Duplicates from Sorted List
- HDU 1160 DP最长子序列
- 【bzoj3620】【似乎在梦中见过的样子】【kmp】
- [从头学数学] 第160节 相似
- dipole antenna simulation by HFSS
- 驱动模块和桩模块的概念和区别
- nginx的 CPU参数worker_processes和worker_cpu_affinity使用说明
- wpf揭秘
- JS 字符串的操作
- nginx的 CPU参数worker_processes和worker_cpu_affinity使用说明
- nginx的 CPU参数worker_processes和worker_cpu_affinity使用说明
- Android课程---Android设置透明效果的三种方法(转)
- java网络编程
- vc6.0如何把一个对话框编辑框的值传递给另外一个对话框编辑框
- Android消息机制
- json和字符串
- java多线程--线程池的使用
- 编辑图文里面添加音乐
- 移动开发(IOS) – Objective-C-10-KVC、谓词、KVO与通知
- Android网络请求框架之Retrofit实践