題目來源:judgegirl from ntu prof. pangfeng Liu
Task Description
Write a program to sort a matrix of integer into snake order as follows.
Your need to write the following function to sort a matrix into snake order.
1 void
snake(
const
int
*ptr_array[100][100],
int
m);
Note that the first parameter is a two dimensional array of pointers to constant integer, so do not try to change the value pointed by the pointer in the array. Instead you should exchange the pointers in order to sort. Your function should work with the following main program.
1234567891011121314151617181920212223242526272829303132 #include <stdio.h>
#include <assert.h>
void
snake(
const
int
*ptr_array[100][100],
int
m);
int
main()
{
int
array[100][100], check[100][100];
const
int
*ptr_array[100][100];
int
i, j, m;
scanf
(
"%d"
, &m);
for
(i = 0; i < m; i++)
for
(j = 0; j < m; j++) {
ptr_array[i][j] = &(array[i][j]);
scanf
(
"%d"
, &(array[i][j]));
check[i][j] = array[i][j];
}
snake(ptr_array, m);
for
(i = 0; i < m; i++) {
for
(j = 0; j < m; j++) {
assert
(check[i][j] == array[i][j]);
assert
((ptr_array[i][j] >= &array[0][0]) && (ptr_array[i][j] <= &array[99][99]));
printf
(
"%d "
, *(ptr_array[i][j]));
}
printf
(
"\n"
);
}
return
0;
}
Input
The first line of the input has the . Each of the next lines has the numbers in a row of the matrix.
Output
The output has lines. Each of the lines is a row in the sorted matrix in snake order.
Sample input
3
59 33 2
14 45 16
51 4 27
Sample output
2 4 14
33 27 16
45 51 59
Hint
You can think of the two dimensional array as a single one dimensional array, and perform a bubble sort on this one dimensional array.