Jpeg(模拟)
Description
- Statements
In computing, JPEG is a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography . The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and image quality,and JPEG typically achieves 10:1 compression with little perceptible loss in image quality. Entropy coding is a special form of lossless data compression. It involves arranging the image components in a "zigzag" order employing run-length encoding (RLE) algorithm that groups similar frequencies together, inserting length coding zeros, and then using Huffman coding on what is left.
Now i am so busy ,so i will give you a square matrix that represents pixel intensities of the image.
Your task is simple: reconstruct the image so that the value in the ith row and jth column of the resulting image is the value of the (i * N + j)thpixel visited in the zigzag sequence .
Input
Your program will be tested on one or more test cases. The first line of the input contains a single integer T (1 ≤ T ≤ 100) the number of test cases. Followed by T test cases.
Each test case consists of N+1 lines. The first line contains an integer N (2 ≤ N ≤ 100). The next lines consists of an squared pixel matrix.
Output
For each test case print the required transformed matrix.
Sample Input
1 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 2 6 11 7 3 4 8 12 16 21 17 13 9 5 10 14 18 22 23 19 15 20 24 25
题解:水题,按照途中的划线访问,模拟下就好了;
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int MAXN = 110; int num[MAXN * MAXN]; int mp[MAXN][MAXN]; int main(){ int T, N; scanf("%d", &T); while(T--){ scanf("%d", &N); for(int i = 1; i <= N; i++){ for(int j = 1; j <= N; j++){ scanf("%d", &mp[j][i]); } } int tp = 0; int x = 1, y = 1; num[tp++] = mp[x][y]; while(tp < N*N){ if(x + 1 <= N) num[tp++] = mp[++x][y]; else if(y + 1 <= N) num[tp++] = mp[x][++y]; while(x - 1 >= 1 && y + 1 <= N){ num[tp++] = mp[--x][++y]; } if(x == 1 && y + 1 <= N) num[tp++] = mp[x][++y]; else if(y == N && x + 1 <= N) num[tp++] = mp[++x][y]; while(x + 1 <= N && y - 1 >= 1){ num[tp++] = mp[++x][--y]; } } for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ mp[i][j] = num[i * N + j]; } } for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ if(j)printf(" "); printf("%d", mp[i][j]); } puts(""); } } return 0; }