CCF Z字形扫描

感觉和LeetCode中的ZigZag还是有一些不一样的。

题目描述

在图像编码的算法中,需要将一个给定的方形矩阵进行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字形扫描的结果。

输入格式

  1. 输入的第一行包含一个整数n,表示矩阵的大小。
  2. 输入的第二行到第n+1行每行包含n个正整数,由空格分隔,表示给定的矩阵。

输出格式

  1. 输出一行,包含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>
# define MAX 510 int n;
int i,j;
int m[MAX][MAX];
typedef struct{
int x,y;
} pos; // define a poit struct void show(pos p)
{
printf("%d ",m[p.x][p.y]);
} int main()
{
pos current = {0,0};
scanf("%d",&n);
for(i = 0;i < n; ++i){
for(j = 0; j < n;++j){
scanf("%d", &m[i][j]);
}
}//end of for i
// data collect end
show(current);
while(1){
if (current.x == current.y && current.x == n-1){
// the end condition
break;
}
if (current.x > n - 1 || current.y > n-1 ) break;
// right or down
if (current.y == n-1){
// down
current.x += 1;
show(current);
}else{
// right
current.y += 1;
show(current);
}
// then go left down
while(current.x < n - 1 && current.y > 0){
current.x +=1;
current.y -=1;
show(current);
}
// go down or right
if (current.x == n - 1 && current.y < n-1){
// can not go down then right
current.y += 1;
show(current);
}else{
// go down
current.x += 1;
show(current);
}
// up and right
while(current.x > 0 && current.y < n -1){
current.x -=1;
current.y +=1;
show(current);
}
}
printf("\n");
return 0;
}

[CCF] Z字形扫描的更多相关文章

  1. CCF——Z字形扫描问题

    试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...

  2. CCF CSP 201412-2 Z字形扫描

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-2 Z字形扫描 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫 ...

  3. CCF真题之Z字形扫描

    201412-2 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 ...

  4. CCF系列之Z字形扫描(201412-2)

    试题编号:201412-2试题名称:Z字形扫描时间限制: 2.0s内存限制: 256.0MB 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n ...

  5. CSP201412-2:Z字形扫描

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  6. Z字形扫描(201412-2)

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  7. Z字形扫描矩阵

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  8. 201412-2 Z字形扫描(c语言)

    问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...

  9. CCF201412-2 Z字形扫描 java(100分)

    试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...

随机推荐

  1. $.getJSON JSONP的新坑

    神坑1:返回的内容必须是正规的json数据.如 { "firstName": "Bill", "lastName": "Gates ...

  2. java之main函数(笔记)

    1.标准的main函数形式 对于main函数,只要是 public static void main(String[] args) public static void main(String... ...

  3. 【iCore3 双核心板】例程五:SYSTICK定时器实验——定时点亮LED

    实验指导书及代码包下载: http://pan.baidu.com/s/1eQsKcEY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  4. 【转】oracle回闪操作

    在9i上执行的操作 查询test表中记录select * from test;删除test表中记录delete from test;获得过去的会话exec dbms_flashback.disable ...

  5. array_diff的参数前后循序的区别

    //如果两个参数之间有区别.array_diff会返回第二个参数的差值 $oldData = array(0=>'德国',1=>'deguo'); $newData = array(0=& ...

  6. thinkphp3.2 cli模式的正确使用方法

    最近要使用thinkphp3.2版本的cli模式,手动执的话没有问题,比如php /www/index.php home/article/get 这样没有问题,但是一般用cli模式都是定时任务比较多, ...

  7. JS 的trim()

    去除字符串左右两端的空格,在vbscript里 可  用 trim.ltrim 或 rtrim,但 js 却没有这 3个 内置方法,需 手工编写.下面的实现方法  用到 正则表达式,效率不错, 把 三 ...

  8. Metasploitable 2系列教程:信息收集

    Metasploitable 2 系统是一个基于ubuntu 的系统.其设计的最初目的为安全工具测试和常见漏洞攻击演示.而在这篇关于 Metasploit 的教程中,我们将列举有关 Metasploi ...

  9. python file模块 替换输入内容脚本

    root@python-10:/home/liujianzuo/python/test# ls passwd rc.local test1 root@python-10:/home/liujianzu ...

  10. Force StyleCop to Ignore a File

    You can quickly force StyleCop to ignore files in a project by manually modifying the project file, ...