<div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold; font-family: 宋体; font-size: 14px;">问题描述</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 宋体; font-size: 14px;"> 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan)。给定一个n×n的矩阵,Z字形扫描的过程如下图所示:<br style="margin: 0px; padding: 0px;" /><img src="http://115.28.138.223/RequireFile.do?fid=Nad2EgMG" width="143" height="145" alt="" style="margin: 0px; padding: 0px; border: 0px;" /><br style="margin: 0px; padding: 0px;" /> 对于下面的4×4的矩阵,<br style="margin: 0px; padding: 0px;" /> 1 5 3 9<br style="margin: 0px; padding: 0px;" /> 3 7 5 6<br style="margin: 0px; padding: 0px;" /> 9 4 6 4<br style="margin: 0px; padding: 0px;" /> 7 3 1 3<br style="margin: 0px; padding: 0px;" /> 对其进行Z字形扫描后得到长度为16的序列:<br style="margin: 0px; padding: 0px;" /> 1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3<br style="margin: 0px; padding: 0px;" /> 请实现一个Z字形扫描的程序,给定一个n×n的矩阵,输出对这个矩阵进行Z字形扫描的结果。</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold; font-family: 宋体; font-size: 14px;">输入格式</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 宋体; font-size: 14px;"> 输入的第一行包含一个整数n,表示矩阵的大小。<br style="margin: 0px; padding: 0px;" /> 输入的第二行到第n+1行每行包含n个正整数,由空格分隔,表示给定的矩阵。</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold; font-family: 宋体; font-size: 14px;">输出格式</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 宋体; font-size: 14px;"> 输出一行,包含n×n个整数,由空格分隔,表示输入的矩阵经过Z字形扫描后的结果。</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold; font-family: 宋体; font-size: 14px;">样例输入</div><div class="pddata" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 'Courier New'; font-size: 16px;">4<br style="margin: 0px; padding: 0px;" />1 5 3 9<br style="margin: 0px; padding: 0px;" />3 7 5 6<br style="margin: 0px; padding: 0px;" />9 4 6 4<br style="margin: 0px; padding: 0px;" />7 3 1 3</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold; font-family: 宋体; font-size: 14px;">样例输出</div><div class="pddata" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 'Courier New'; font-size: 16px;">1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3</div><div class="pdsec" style="margin: 12px 0px 20px; padding: 0px; font-weight: bold; font-family: 宋体; font-size: 14px;">评测用例规模与约定</div><div class="pdcont" style="margin: 0px 0px 0px 32px; padding: 0px; font-family: 宋体; font-size: 14px;"> 1≤n≤500,矩阵元素为不超过1000的正整数。</div>
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; list-style-type: none; color: rgb(51, 51, 51); font-family: Verdana, Arial, Tahoma; font-size: 14px; line-height: 25px; background-color: rgb(204, 232, 207);"></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;">在</span><span style="color:#333333;">CCF</span><span style="color:#333333;">认证考试的五道题里面,该题排在第二的位置,显然难度不是很大的,不宜恋战,应当速战速决。</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;">考试当时想到的第一步就是看图找规律。发现矩阵</span><span style="color:#333333;">Z</span><span style="color:#333333;">字形扫描从</span><span style="color:#333333;">(0,0)</span><span style="color:#333333;">位置开始,前进的方向有</span><span style="color:#333333;">4</span><span style="color:#333333;">个,分别是</span><span style="color:red;">右、下,左下、右上</span><span style="color:#333333;">。不难看出,</span><span style="color:#333333;">Z</span><span style="color:#333333;">字形扫描向右、向下两个方向,</span><span style="color:#00B050;">只前进一步</span><span style="color:#333333;">就会变向,改变后的方向因情况而异;而左下和右上两个方向则会</span><span style="color:#00B050;">一直延伸</span><span style="color:#333333;">,直至前面无法继续直行时再</span><span style="color:#00B050;">变向</span><span style="color:#333333;">为向右或者向下。</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;">然而值得注意的时:</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;">1.</span><span style="color:#333333;">左下方向因无法继续直行需要变向时,要先判断是否能往下走,若不能往下走再往右走;</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;">2.</span><span style="color:#333333;">右上方向因无法继续直行需要变向时,要先判断是否能往右走,若不能往右走再往下走。</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:red;">到这里可以总结出:下一步的方向,由上一步的方向以及周围的情况(是否临界)决定。</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;"> </span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#333333;">想到这些,我就想模拟一个人在</span><span style="color:#333333;">(0,0)</span><span style="color:#333333;">这个位置出发,沿题目要求的路径走到</span><span style="color:#333333;">(n-1,n-1)</span><span style="color:#333333;">的位置。由于下一步的方向需要上一步的方向来参与决策,因此需要引入一个方向变量</span><span style="color:red;">direction</span><span style="color:#333333;">,分别用值</span><span style="color:#333333;">1,2,3,4</span><span style="color:#333333;">代表</span><span style="color:#333333;">4</span><span style="color:#333333;">个方向:右,下,左下,右上。编程的时候可以分解为两个步骤:</span><span style="color:#333333;"> </span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#00B050;">1.</span><span style="color:#00B050;">根据上一步的方向以及是否到达边界位置来决定下一步方向;</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#00B050;">2.</span><span style="color:#00B050;">根据步骤</span><span style="color:#00B050;">1</span><span style="color:#00B050;">得到的方向前进一步,读取该位置的数字。</span></p><p align="left" style="background:rgb(204, 232, 207);"><span style="color:#00B0F0;">循环直至到达终点</span><span style="color:#00B0F0;">(n-1, n-1)</span></p>
#include<iostream>
#include<assert.h>
using namespace std;
#define RIGHT 1 //向右走
#define DOWN 2 //向下走
#define LEFTDOWN 3 //向做下走
#define RIGHTUP 4 //向右上走
int main()
{
int n;
int array[501][501];
assert(freopen("input1.txt", "r", stdin));
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cin >> array[i][j];
}
int x = 0;
int y = 0;
while (!((x == n - 1) && (y == n - 1)))
{
//從第一個位置開始,x为橫坐標,y为縱坐標,注意x,y在二維數組中的位置
//輸出要求有空格隔開
printf("%d ", array[y][x]);
//方向變量的初始值
int direction = 0;
//下面就開始出發走咯
while (!(x == n - 1 && y == n - 1)){//循環直至到達終點(最右下角的位置)
//先判斷下一步往哪個方向走
if (direction == 0){ //为0說明還沒走出第一步,所以接着應該往右邊走一步
direction = RIGHT;
}
else if (direction == RIGHT){ //上一次方向向右,下一步應該向左下或者右上
if (x - 1 >= 0 && y + 1 < n){ //左下可走
direction = LEFTDOWN;
}
else { //只能走右上了
direction = RIGHTUP;
}
}
else if (direction == DOWN){ //上一次方向向下,下一步應該向左下或者右上
if (x - 1 >= 0 && y + 1 < n){ //左下可走
direction = LEFTDOWN;
}
else { //只能走右上了
direction = RIGHTUP;
}
}
else if (direction == LEFTDOWN){ //上一次向左下,如果可以,下一步應該繼續向左下,否則 向右或者向下走
if (y + 1 < n && x - 1 >= 0){ //先判斷能否繼續向左下
direction = LEFTDOWN;
}
else if (y + 1 < n){ //然後判斷能否向下走
direction = DOWN;
}
else { //最後只能向右走了
direction = RIGHT;
}
}
else if (direction == RIGHTUP){ //上一次向右上,如果可以,下一步應該繼續向右上,否則向右或者下走
if (x + 1 < n && y - 1 >= 0){ //先判斷能否繼續向右上
direction = RIGHTUP;
}
else if (x + 1 < n){ //然後判斷能否向右走
direction = RIGHT;
}
else{ //最後只能向下走了
direction = DOWN;
}
}
//根據上面確定的方向來走出下一步
switch (direction){
case RIGHT: x = x + 1; break;
case DOWN: y = y + 1; break;
case LEFTDOWN: x = x - 1; y = y + 1; break;
case RIGHTUP: x = x + 1; y = y - 1; break;
}
//讀取當前走到位置的數字.注意x和y的位置
printf("%d ", array[y][x]);
}
}
return 0;
}