九宫格 左下斜行法 编程实现 改下数据就可实现N宫格

来源:互联网 发布:阿里云域名如何备案 编辑:程序博客网 时间:2024/05/01 13:45

   九宫格  左下斜行发  2009-12-12  by Yeook
                                                       369
    35  24  13  2    81 70 59  48  37 369
    25  14  3    73  71 60 49  38  36 369
    15  4    74  72  61 50 39  28  26 369
    5    75  64  62  51 40 29  27  16 369
    76  65  63  52  41 30 19  17  6   369
    66  55  53  42  31 20 18  7    77 369
    56  54  43  32  21 10  8   78  67 369
    46  44  33  22  11  9  79  68  57 369
    45  34  23  12  1   80 69  58  47 369
    369 369 369 369 369 369 369 369 369 369

 

 

 

#include "stdafx.h"
#include <stdio.h>

 

int x,y;

int nine[9][9]={0};

//到底部时调用函数
int iS_firstLine()
{
    if (0==y)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}


int is_lastRow()
{
    if (8==x)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
//判断斜下角的那位是否已经填充了数字   如果是则返回1  说明要向现在的头上填充
//                                     如果是空的则返回0 ,下次就朝该位子填充
is_biasNumber()
{
    if (0 == nine[x+1][y-1])
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main(int argc, char* argv[])
{
 
   
    //设置足为  就是1的位置
    y=9/2;
    x=8;
    int nowNumber=1;
    for (nowNumber=1;nowNumber<=9*9;nowNumber++)
    {
        //最后一行  但是不是第一列   则要往前一列的第一行开始填充
        if (is_lastRow() && !iS_firstLine())
        {
            nine[x][y]=nowNumber;
            x=0;
            y--;
            continue;
        }
        //是最后一行且是第一列  则下次向自己头上填充
        if (is_lastRow() && iS_firstLine())
        {
            nine[x][y]=nowNumber;
            x--;
            continue;
        }
        //不是最后一行  但是是第一列  则从下一行的最后一个数字开始填充
        if (!is_lastRow() && iS_firstLine())
        {
            nine[x][y]=nowNumber;
            x++;
            y=8;
            continue;
        }
        //不是最后一行  也不是第一列
        if (!is_lastRow() && !iS_firstLine())
        {
            //斜下角位置为空
            if (is_biasNumber())
            {
                nine[x][y]=nowNumber;
                x++;
                y--;
            }
            //斜下角位置不为空 则向头上填充
            else
            {
                nine[x][y]=nowNumber;
                x--;
            }
            continue;

        }

           
    }

    int a,b;
    for (a=0;a<9;a++)
    {
        for (b=0;b<9;b++)
        {
            printf("%5d",nine[a][b]);
        }
        printf("/r/n");
    }
 return 0;
}

 

原创粉丝点击