蛇形填数
来源:互联网 发布:vs2017编程教程 编辑:程序博客网 时间:2024/06/04 19:45
#include<stdio.h>
#include<string.h>
#define maxn 20
int a[maxn][maxn];
int main()
{
int n, x, y, tot = 0;
scanf("%d",&n);
memset(a, 0, sizeof(a));
tot = a[x=0][y=n-1]=1;
while(tot<n*n)
{
while(x+1<n&&!a[x+1][y]) a[++x][y]= ++tot;
while(y-1>=0&&!a[x][y-1]) a[x][--y]= ++tot;
while(x-1>=0&&!a[x-1][y]) a[--x][y]= ++tot;
while(y+1<n&&!a[x][y+1]) a[x][++y]= ++tot;
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)printf("%3d",a[x][y]);
printf("\n");
}
return 0;
}
#include<string.h>
#define maxn 20
int a[maxn][maxn];
int main()
{
int n, x, y, tot = 0;
scanf("%d",&n);
memset(a, 0, sizeof(a));
tot = a[x=0][y=n-1]=1;
while(tot<n*n)
{
while(x+1<n&&!a[x+1][y]) a[++x][y]= ++tot;
while(y-1>=0&&!a[x][y-1]) a[x][--y]= ++tot;
while(x-1>=0&&!a[x-1][y]) a[--x][y]= ++tot;
while(y+1<n&&!a[x][y+1]) a[x][++y]= ++tot;
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)printf("%3d",a[x][y]);
printf("\n");
}
return 0;
}
先是往下走,到不能填为止,然后是左,接着是上,最后是右。“不能填”是指再走就出界,或者再走就要走到以前填过的格子。
如果把所有格子初始化为0,就能很方便的加以判断。
阅读全文
0 0
- 蛇形填数
- 算法:蛇形填数
- 蛇形填数
- 蛇形填数
- 蛇形填数
- 蛇形填数问题
- 蛇形填数
- C++蛇形填数
- 蛇形填数
- 蛇形填 数
- 蛇形填数
- NYOJ - 蛇形填数
- 蛇形填数
- 各种蛇形填数
- 蛇形填数
- 蛇形填数
- ACM-蛇形填数
- 蛇形填数
- TextView一行最多显示8个 其他的字符自动滚动
- 机器学习基石-02-1-yes/no
- What's new in Xcode 9
- Jmeter之Bean shell使用(一)
- 函数的使用与返回值
- 蛇形填数
- hdu 5120 Intersection (容斥原理+圆的相交面积模板)
- hdu-1548-A strange lift(bfs)
- 行人属性“Generative Adversarial Models for People Attribute Recognition in Surveillance”
- Java全套学习资料
- Jmeter之Bean shell使用(二)
- 【1134】亲密数
- eclipse项目转Android studio
- Python高阶:使用Wheel打包