关于蛇形填数问题求解

来源:互联网 发布:泉州招聘 福建网络 编辑:程序博客网 时间:2024/05/28 18:44

在n*n方阵里填入1, 2, 3, 4, 5...........n*n,填成蛇形,如:

n = 4时

10 11 12 1

9 16 13 2

8 15 14 3

7 6 5 4 

代码如下

#include <cstdio>#include <cstring>#define MAXN 10int a[MAXN][MAXN];//using namespace std;int main(){int n, x, y, m, tot;//cin >> n; scanf("%d",&n);memset(a, 0, sizeof(a)) ;//数组清零tot = a[x=0][y=n-1]=1;while(tot<n*m){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(int i=0;i<n;++i){for(int j=0;j<n;++j)printf("%3d", a[i][j]);printf("\n");}return 0;}

检查结果;

Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: C:\Documents and Settings\Administrator\My Documents\未命名1.exe
- Output Size: 500.2529296875 KiB
- Compilation Time: 0.34s

运行时输入n后

结果却是一片空白

是不是哪里有错

求指导

谢谢

0 0