【CCF】Z字扫描 与 Z字形打印矩阵

试题编号: 201412-2
试题名称: Z字形扫描
时间限制: 2.0s
内存限制: 256.0MB
问题描述:
问题描述
  在图像编码的算法中,需要将一个给定的方形矩阵进行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的正整数。

思路:

该题自己写了一个冗长的程序,在看了海岛blog的思路后,有种豁然开朗的感觉,为啥我的思路就那么复杂呢=.=


代码实现:

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

const int EAST = 0;
const int SOUTH = 1;
const int SOUTHWEST = 2;
const int NORTHEAST = 3;

struct D{
	int drow;
	int dcol;
}direct[] = {{0,1}, {1,0}, {1,-1}, {-1,1}};  //四个方向的前进规则 

int main(){
	int a[500][500];
	int n;
	scanf("%d", &n);
	for(int i = 0; i<n; i++){
		for(int j = 0; j<n; j++){
			scanf("%d", &a[i][j]);
		}
	}
	int row = 0, col = 0, next = EAST;   
	printf("%d", a[row][col]);
	while((row != n-1) || (col != n-1)){
		row += direct[next].drow;
		col += direct[next].dcol;
		printf(" %d", a[row][col]);
		
		if(next == EAST && row == 0){  //第一行向东走后,下一方向设为西南 
			next = SOUTHWEST;	
		}
		else if(next == EAST && row == n-1){  //最后一行向东走后,下一方向设为东北 
			next = NORTHEAST;	
		}
		else if(next == SOUTH && col == 0){
			next = NORTHEAST;
		}
		else if(next == SOUTH && col == n-1){
			next = SOUTHWEST;
		}
		else if(next == SOUTHWEST && row == n-1){
			next = EAST;
		}
		else if(next == SOUTHWEST && col == 0){
			next = SOUTH;
		}
		else if(next == NORTHEAST && col == n-1){
			next = SOUTH;
		}
		else if(next == NORTHEAST && row == 0){
			next = EAST;
		}
	}
	printf("\n");
	return 0;
} 


与该题类似的还有按照Z字形打印矩阵。

题意:

给定一个整数n,按照z字形打印一个n*n的矩阵

样例输入:

4

样例输出:


代码实现:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;

const int east = 0;
const int south = 1;
const int south_west = 2;
const int north_east = 3;

struct D{
	int d_row;
	int d_col;
}direct[] = {{0,1}, {1,0}, {1,-1}, {-1,1}};

int main(){
	int n, num = 1, arr[500][500];
	int row = 0, col = 0, next = south;
	scanf("%d", &n);
	arr[0][0] = num++;
	while((row != n-1) || (col != n-1)){
		row += direct[next].d_row;
		col += direct[next].d_col;
		arr[row][col] = num++;
		
		if(next == south && col == 0){
			next = north_east;
		}
		else if(next == south && col == n-1){
			next = south_west;
		}
		else if(next == east && row == 0){
			next = south_west;
		}
		else if(next == east && row == n-1){
			next = north_east;
		}
		else if(next == south_west && row == n-1){
			next = east;
		}
		else if(next == south_west && col == 0 ){
			next = south;
		}
		else if(next == north_east && col == n-1){
			next = south;
		}
		else if(next == north_east && row == 0){
			next = east;
		}
	}
	for(int i = 0; i<n; i++){
		for(int j = 0; j<n-1; j++){
			printf("%d ",arr[i][j]);
		}
		printf("%d\n",arr[i][n-1]);
	}
	return 0;
} 








参考原文:http://blog.csdn.net/tigerisland45/article/details/54773635






猜你喜欢

转载自blog.csdn.net/u014322206/article/details/78646468