1. 程式人生 > >【CCF】Z字掃描 與 Z字形列印矩陣

【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