Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1286111
  • 博文数量: 168
  • 博客积分: 3483
  • 博客等级: 中校
  • 技术积分: 1696
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 13:17
文章分类

全部博文(168)

文章存档

2015年(6)

2014年(9)

2013年(47)

2012年(11)

2011年(13)

2010年(18)

2009年(11)

2008年(42)

2007年(11)

分类:

2009-06-29 21:48:39

旋转方向(2) x 升降序(2) x 起始点(4) == 16
(算上起始点不在四个角的情况就更多了,,)
usage:
    ./lx.awk [-b] [-r] [-e | -s | -w | -n] [side length]
---------------------------------------------------------------------
#!/usr/bin/awk -f

BEGIN {
    i = 0;
    j = 0;

    for (i = 1; i < ARGC; i++) {
        if (ARGV[i] == "-b")
            bdirectf = 1;
        else if (ARGV[i] == "-r")
            reverse = 1;
        else if (ARGV[i] == "-e")
            direct = "east";
        else if (ARGV[i] == "-s")
            direct = "south";
        else if (ARGV[i] == "-w")
            direct = "weast";
        else if (ARGV[i] == "-n")
            direct = "north";
        else
            x = int(ARGV[i]);
    }
    if (x == 0) x = 9;

    if (bdirectf == 1) {
        bdirect(array, x, direct, reverse);
    } else {
        fdirect(array, x, direct, reverse);
    }

    for (i = 0; i < x; i++) {
        for (j = 0; j < x; j++) {
            if ((i == j) || ((i+j) == (x-1))) {
                printf("\033[1;34m" "%.3d " "\033[0m", array[i*x+j]);
            } else {
                printf("%.3d ", array[i*x+j]);
            }
        }
        printf("\n");
    }
}

function fdirect(array, xlen, direct, rf,  gnum, gc, k, stat, stat1, stat2, m, n, max)
{
    max = xlen*xlen;
    gnum = (xlen-1)*2;
    gc = gnum;

    if (direct == "south") {
        gc -= 0;
    m = 0; n = x-1;
    }
    else if (direct == "weast") {
        gc -= 1;
    m = x-1; n = x-1;
    }
    else if (direct == "north") {
        gc -= 2;
    m = x-1; n = 0;
    }

    for (k = 1; k <= max; k++) {
        stat = gnum - gc;
        stat1 = int(stat/2)%2;
        stat2 = stat%2;

        if (stat1 == 0) {
            if (stat2 == 0) {
                array[m*x + n] = ring(k, max, rf); n++;       # 向东拐
                if ((array[m*x + n] != 0) || (n >= x)) {
                    n--; m++;
                    gc--;
                }
            } else {
                array[m*x + n] = ring(k, max, rf); m++;       # 向南拐
                if ((array[m*x + n] != 0) || (m >= x)) {
                    m--; n--;
                    gc--;
                }
            }
        } else {
            if (stat2 == 0) {
                array[m*x + n] = ring(k, max, rf); n--;       # 向西拐
                if ((array[m*x + n] != 0) || (n < 0)) {
                    n++; m--;
                    gc--;
                }
            } else {
                array[m*x + n] = ring(k, max, rf); m--;       # 向北拐
                if ((array[m*x + n] != 0) || (m < 0)) {
                    m++; n++;
                    gc--;
                }
            }
        }
    }
}

function bdirect(array, xlen, direct, rf,   gnum, gc, k, stat, stat1, stat2, m, n, max)
{
    max = xlen*xlen;
    gnum = (xlen-1)*2;
    gc = gnum;

    if (direct == "east") {
        gc -= 0;
    m = x-1; n = 0;
    }
    else if (direct == "north") {
        gc -= 1;
    m = x-1; n = x-1;
    }
    else if (direct == "weast") {
        gc -= 2;
    m = 0; n = x-1;
    }

    for (k = 1; k <= xlen*xlen; k++) {
        stat = gnum - gc;
        stat1 = int(stat/2)%2;
        stat2 = stat%2;

        if (stat1 == 0) {
            if (stat2 == 0) {
                array[m*x + n] = ring(k, max, rf); m++;       # 向南拐
                if ((array[m*x + n] != 0) || (m >= x)) {
                    m--; n++;
                    gc--;
                }
            } else {
                array[m*x + n] = ring(k, max, rf); n++;       # 向东拐
                if ((array[m*x + n] != 0) || (n >= x)) {
                    n--; m--;
                    gc--;
                }
            }
        } else {
            if (stat2 == 0) {
                array[m*x + n] = ring(k, max, rf); m--;       # 向北拐
                if ((array[m*x + n] != 0) || (m < 0)) {
                    m++; n--;
                    gc--;
                }
            } else {
                array[m*x + n] = ring(k, max, rf); n--;       # 向西拐
                if ((array[m*x + n] != 0) || (n < 0)) {
                    n++; m++;
                    gc--;
                }
            }
        }
    }
}

function ring(value, max, flag)
{
    if (flag == 1)
        return (max - value + 1);
    return value;
}


阅读(1197) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~