Z字形扫描

问题描述
  在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan)。给定一个n×n的矩阵,Z字形扫描的过程如下图所示:

  对于下面的4×4的矩阵,
  1 5 3 9
  3 7 5 6
  9 4 6 4
  7 3 1 3
  对其进行Z字形扫描后得到长度为16的序列:
  1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
  请实现一个Z字形扫描的程序,给定一个n×n的矩阵,输出对这个矩阵进行Z字形扫描的结果。
输入格式
  输入的第一行包含一个整数n,表示矩阵的大小。
  输入的第二行到第n+1行每行包含n个正整数,由空格分隔,表示给定的矩阵。
输出格式
  输出一行,包含n×n个整数,由空格分隔,表示输入的矩阵经过Z字形扫描后的结果。
样例输入
4
1 5 3 9
3 7 5 6
9 4 6 4
7 3 1 3
样例输出
1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3
评测用例规模与约定
  1≤n≤500,矩阵元素为不超过1000的正整数。


#include<stdio.h>
#include<stdlib.h>
#define RIGHT 1       //4个方向 
#define DOWN 2
#define LEFTDOWN 3
#define RIGHTUP 4

int main()
{
    int row,col;
    int n=0;
    int **array;
    scanf("%d",&n);
    if(n<1 || n>500)
    {
        return 0;
    }
    array=(int**)malloc(sizeof(int*)*(n+1));  //行列下标从都从1开始 
    int i;
    for(i=1;i<=n;i++)
    {
        array[i]=(int*)malloc(sizeof(int)*(n+1)); //array[i]是一个指针,指向含有n个元素的数组  
    }
    for(row=1;row<=n;row++)
    {
        for(col=1;col<=n;col++)
        {
            scanf("%d",&array[row][col]);
            if(array[row][col]<0 || array[row][col]>1000)
            {
                return 0;
            }
        }
    }
    row=1;
    col=1;
    int direction=0;
    int *resultArray;
    resultArray=(int*)malloc(sizeof(int)*n*n);
    int a;
    resultArray[0]=array[row][col];
    for(a=1;a<n*n && !(row==n && col==n);a++)
    {
        if(direction==0)                 //方向判断 
        {
            direction=RIGHT;
        }
        else if(direction==RIGHT)
        {
            if(row+1<=n && col-1>=1)
            {
                direction=LEFTDOWN;
            }
            else
            {
                direction=RIGHTUP;
            }
        }
        else if(direction==LEFTDOWN)
        {
            if(row+1<=n && col-1>=1)
            {
                direction=LEFTDOWN;
            }
            else if(row+1<=n)
            {
                direction=DOWN;
            }
            else
            {
                direction=RIGHT;
            }
        }
        else if(direction==RIGHTUP)
        {
            if(row-1>=1 && col+1<=n)
            {
                direction=RIGHTUP;
            }
            else if(col+1<=n)
            {
                direction=RIGHT;
            }
            else
            {
                direction=DOWN;
            }
        }
        else
        {
            if(row+1<=n && col-1>=1)
            {
                direction=LEFTDOWN;
            }
            else
            {
                direction=RIGHTUP;
            }
        }

        if(direction==RIGHT)      //判断下标变化 
        {
            ++col;
        }
        else if(direction==DOWN)
        {
            ++row;
        }
        else if(direction==LEFTDOWN)
        {
            ++row;
            --col;
        }
        else
        {
            --row;
            ++col;
        }
        resultArray[a]=array[row][col];

    }
    for(a=0;a<n*n;a++)
    {
        printf("%d ",resultArray[a]);
    }
    free(resultArray);     
    int j;
    for(j=1;j<=n;j++)
    {
        free(array[j]);  //array[j]是一个指针,指向含有n个元素的数组 
    }
    free(array);
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值