50127. Connect the Numbers
難度:3/51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <stdio.h>
#include <stdlib.h>
#include "constructPointerArray.h"
typedef struct{
int x;
int y;
int num;
} helper;
int cmp(const void* a, const void* b){
helper* ha = (helper*)a;
helper* hb = (helper*)b;
return (ha->num) - (hb->num);
}
helper arr[1024 * 1024];
void constructPointerArray(const int a[1024][1024], const int *b[1024][1024], int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
arr[i * n + j].x = i;
arr[i * n + j].y = j;
arr[i * n + j].num = a[i][j];
}
}
qsort(arr, n * n, sizeof(helper), cmp);
for(int i = 0; i < n * n - 1; i++){
b[arr[i].x][arr[i].y] = &(a[arr[i + 1].x][arr[i + 1].y]);
}
b[arr[n * n - 1].x][arr[n * n - 1].y] = &(a[arr[0].x][arr[0].y]);
}