| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1152 人关注过本帖
标题:盘龙数组怎样写啊??
只看楼主 加入收藏
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
结帖率:69.57%
收藏
已结贴  问题点数:10 回复次数:11 
盘龙数组怎样写啊??
第一行为一个整数,表示测试用例的个数。
其后的每一行,有个整数n(1 < = n < = 30 )。
Output
对每个n×n正方形数组,请参照输出样例,输出盘龙状数组。为了对齐,每个数组元素占4个字符宽度,并且,不足4个字符的按向右对齐输出。
每个数组的输出之后有一个空行。
Sample Input
3
1
2
5
Sample Output
   1

   1   2
   4   3

   1   2   3   4   5
  16  17  18  19   6
  15  24  25  20   7
  14  23  22  21   8
  13  12  11  10   9
搜索更多相关主题的帖子: 正方形 盘龙 元素 
2011-03-25 16:49
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:2 
这叫盘龙数组呀。之前看到有人说过。但好像没看到有程序。
2011-03-25 17:09
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:2 
百度里有! 忘了叫啥名啦! 挺难得!!!

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-03-25 19:24
新手写程序
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:62
专家分:120
注 册:2011-3-5
收藏
得分:2 
你就当作是一支笔在那里画,你就控制它的走向
2011-03-25 21:00
njkido
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:224
专家分:1184
注 册:2011-3-8
收藏
得分:0 
自己写的 没什么算法 好懂
程序代码:
#define RIGHT__ 0 
#define LEFT__  1
#define UP__    2
#define DOWN__  3

#define N 9

typedef void (*DragonArrayMoveMethodFn)(unsigned short Array[N][N],unsigned short*,unsigned short*);


void MoveDown(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)
{
    Array[*CurrentX+1][*CurrentY] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentX)++;
    return;
}
void MoveUp(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)
{
    Array[*CurrentX-1][*CurrentY] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentX)--;
    return;
}
void MoveLeft(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)

{
    Array[*CurrentX][*CurrentY-1] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentY)--;
    return;
}
void MoveRight(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)
{
    Array[*CurrentX][*CurrentY+1] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentY)++;
    return;
}

DragonArrayMoveMethodFn Move[]={
    MoveRight,
    MoveLeft,
    MoveUp,
    MoveDown
};

unsigned short GetNewAction(unsigned short LastAction)
{
    //clockwise
    switch (LastAction)
    {
    case RIGHT__:
        return DOWN__;
    case LEFT__:
        return UP__;
    case UP__:
        return RIGHT__;
    case DOWN__:
        return LEFT__;
    }
}

bool IsNeedChangeAction(unsigned short Array[N][N],unsigned short CurrentX,unsigned short CurrentY,unsigned short LastAction)
{
    switch (LastAction)
    {
    case DOWN__:
        return (CurrentX == N-1) || Array[CurrentX+1][CurrentY] != 0;
    case UP__:
        return (CurrentX == N-1) || Array[CurrentX-1][CurrentY] != 0;
    case RIGHT__:
        return (CurrentY == N-1) || Array[CurrentX][CurrentY+1] != 0;
    case LEFT__:
        return (CurrentY == N-1) || Array[CurrentX][CurrentY-1] != 0;
    }
}

void GenDragonArray()
{
    unsigned short CurrentX = 0; 
    unsigned short CurrentY = 0;
    unsigned short DragonArray[N][N] = {0};
    unsigned short CurrentAction = RIGHT__;
    unsigned short Count = 0;

    DragonArray[0][0] = 1;
    while(Count<(N*N)-1)
    {
        //Determine whether need change action
        if (IsNeedChangeAction(DragonArray,CurrentX,CurrentY,CurrentAction))
        {
            //Change Move Action
            CurrentAction = GetNewAction(CurrentAction);
        }

        //Move
        Move[CurrentAction](DragonArray,&CurrentX,&CurrentY);

        Count++;
    }

    {
        int i=0;
        int j=0;
        for (i=0;i<N;i++)
        {
            for (j=0;j<N;j++)
            {
                printf("%02d ",DragonArray[i][j]);
            }
            printf("\n");
        }
    }
}


结果:
01 02 03 04 05 06 07 08 09
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
请按任意键继续. . .

[ 本帖最后由 njkido 于 2011-3-28 14:57 编辑 ]
2011-03-28 14:55
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
收藏
得分:0 
回复 5楼 njkido
跟题目意思不相符!仔细看了!
2011-03-29 20:58
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:0 
以下是引用njkido在2011-3-28 14:55:53的发言:

自己写的 没什么算法 好懂

#define RIGHT__ 0
#define LEFT__  1
#define UP__    2
#define DOWN__  3

#define N 9

typedef void (*DragonArrayMoveMethodFn)(unsigned short Array[N][N],unsigned short*,unsigned short*);


void MoveDown(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)
{
    Array[*CurrentX+1][*CurrentY] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentX)++;
    return;
}
void MoveUp(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)
{
    Array[*CurrentX-1][*CurrentY] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentX)--;
    return;
}
void MoveLeft(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)

{
    Array[*CurrentX][*CurrentY-1] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentY)--;
    return;
}
void MoveRight(unsigned short Array[N][N],unsigned short *CurrentX,unsigned short *CurrentY)
{
    Array[*CurrentX][*CurrentY+1] = Array[*CurrentX][*CurrentY] + 1;
    (*CurrentY)++;
    return;
}

DragonArrayMoveMethodFn Move[]={
    MoveRight,
    MoveLeft,
    MoveUp,
    MoveDown
};

unsigned short GetNewAction(unsigned short LastAction)
{
    //clockwise
    switch (LastAction)
    {
    case RIGHT__:
        return DOWN__;
    case LEFT__:
        return UP__;
    case UP__:
        return RIGHT__;
    case DOWN__:
        return LEFT__;
    }
}

bool IsNeedChangeAction(unsigned short Array[N][N],unsigned short CurrentX,unsigned short CurrentY,unsigned short LastAction)
{
    switch (LastAction)
    {
    case DOWN__:
        return (CurrentX == N-1) || Array[CurrentX+1][CurrentY] != 0;
    case UP__:
        return (CurrentX == N-1) || Array[CurrentX-1][CurrentY] != 0;
    case RIGHT__:
        return (CurrentY == N-1) || Array[CurrentX][CurrentY+1] != 0;
    case LEFT__:
        return (CurrentY == N-1) || Array[CurrentX][CurrentY-1] != 0;
    }
}

void GenDragonArray()
{
    unsigned short CurrentX = 0;
    unsigned short CurrentY = 0;
    unsigned short DragonArray[N][N] = {0};
    unsigned short CurrentAction = RIGHT__;
    unsigned short Count = 0;

    DragonArray[0][0] = 1;
    while(Count<(N*N)-1)
    {
        //Determine whether need change action
        if (IsNeedChangeAction(DragonArray,CurrentX,CurrentY,CurrentAction))
        {
            //Change Move Action
            CurrentAction = GetNewAction(CurrentAction);
        }

        //Move
        Move[CurrentAction](DragonArray,&CurrentX,&CurrentY);

        Count++;
    }

    {
        int i=0;
        int j=0;
        for (i=0;i<N;i++)
        {
            for (j=0;j<N;j++)
            {
                printf("%02d ",DragonArray[j]);
            }
            printf("\n");
        }
    }
}


结果:
01 02 03 04 05 06 07 08 09
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
请按任意键继续. . .


wonderful!!!bookmark。

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-03-29 21:18
虾B写
Rank: 8Rank: 8
来 自:湖北
等 级:蝙蝠侠
威 望:3
帖 子:395
专家分:922
注 册:2009-10-1
收藏
得分:0 
你们仔细看就会发现规律2N-1=9

16+2=15+3=14+4....请斜着看  24+18=21*2

可以用简单的历遍填出来,不用写这老长的代码。

白娘故意下雨骗许仙的伞。祝英台十八里相送时装疯卖傻调戏梁山伯。七仙女挡住了董永的去路。牛郎趁织女洗澡时拿走了她的衣服。。。这些故事告诉我们;伟大爱情的开始,总归的有一个要先耍流氓!
2011-03-29 21:38
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:0 
看下我在TC下写的代码,完全适合题目要求!(代码很简单,懒得写注释了)
程序代码:
main()
{
   int i,n[100];
   scanf("%d",&n[0]);
   for(i=1;i<=n[0];i++)
      scanf("%d",&n[i]);
   for(i=1;i<=n[0];i++)
      GenDragon(n[i]);
}
int GenDragon(int n)
{
   int a[25][25]={0},i=1,j=1,iold,jold,d=1,x;
   for(x=1;x<=n*n;x++)
   {
       a[i][j]=x;
       iold=i;
       jold=j;
       if(d%4==1)j++;
       if(d%4==2)i++;
       if(d%4==3)j--;
       if(d%4==0)i--;
       if( i>n || j>n || i*j==0 || a[i][j]!=0 )
       {
          d++;
          i=iold;
          j=jold;
          if(d%4==1)j++;
          if(d%4==2)i++;
          if(d%4==3)j--;
          if(d%4==0)i--;
       }
   }
   for(i=1;i<=n;i++)
   {
      for(j=1;j<=n;j++)
         printf("%4d",a[i][j]);
      printf("\n");
   }
   printf("\n");
}
运行时:
程序代码:
3
1
2
5
    1

    1    2
    4    3

    1    2    3    4    5
   16   17   18   19    6
   15   24   25   20    7
   14   23   22   21    8
   13   12   11   10    9



[ 本帖最后由 jack10141 于 2011-4-1 22:35 编辑 ]

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2011-04-01 22:26
hoodlum1980
Rank: 2
来 自:浙江大学
等 级:论坛游民
威 望:2
帖 子:289
专家分:23
注 册:2008-2-24
收藏
得分:0 
cnblog上好像我看到有别人专门研究过这个问题,还有数学方法。

my tech blog: /hoodlum1980
2011-04-02 07:39
快速回复:盘龙数组怎样写啊??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042625 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved