-
Notifications
You must be signed in to change notification settings - Fork 0
/
xoanoc.cpp
147 lines (132 loc) · 2.82 KB
/
xoanoc.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
int n;
int s[100];
int result [100][100];
struct toado {
int h,c;
};
int flag[100][100];
toado currentLocation;
toado huong[5] = {
-1,0,
0,1,
1,0,
0,-1
};
int hCurrent;
int huongByChar(char c){
int h = 0;
if(c == 'E'){
h = 1;
}else if(c == 'S'){
h = 2;
}else if(c == 'W'){
h = 3;
}else if(c == 'L'){
h = (hCurrent+3)%4;
}else if(c == 'R'){
h = (hCurrent+1)%4;
}
return h;
}
toado doMove(int huongNumber){
toado newLocation;
newLocation.h = currentLocation.h + huong[huongNumber].h;
newLocation.c = currentLocation.c + huong[huongNumber].c;
return newLocation;
}
void input()
{
hCurrent = 3;
memset(s, 0, sizeof(s));
memset(result, 0, sizeof(result));
scanf("%d",&n);
for (int i=0; i< n * n; i++){
scanf("%d",&s[i]);
}
}
bool cmp (int n1, int n2)
{
if(n1 < n2){
return 1;
}
return 0;
}
bool isTurnLeft(){
int currentH = currentLocation.h;
int currentC = currentLocation.c;
bool isTurnLeft = false;
if(flag[currentH+1][currentC-1] == 1 ){
isTurnLeft = true;
}
if(flag[currentH-1][currentC-1] == 1){
isTurnLeft = true;
}
if(flag[currentH-1][currentC+1] == 1 ){
isTurnLeft = true;
}
if(flag[currentH+1][currentC+1] == 1){
isTurnLeft = true;
}
if(isTurnLeft == true){
int newH = (hCurrent+3)%4;
toado newLocation = doMove(newH);
if(result[newLocation.h][newLocation.c] != 0){
isTurnLeft = false;
}
}
return isTurnLeft;
}
void solve()
{
sort(s, s+(n*n),cmp);
toado newLocation;
for (int i=0; i< n * n; i++){
if(i == 0){
currentLocation.h = n/2;
currentLocation.c = n/2;
flag[currentLocation.h][currentLocation.c] = 1;
}else if(i == 1){
currentLocation = doMove(hCurrent);
hCurrent = huongByChar('L');
flag[currentLocation.h][currentLocation.c] = 1;
}else{
currentLocation = doMove(hCurrent);
if(isTurnLeft() == true){
hCurrent = huongByChar('L');
flag[currentLocation.h][currentLocation.c] = 1;
}
}
result[currentLocation.h][currentLocation.c] = s[i];
}
}
void output()
{
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
printf("%2d ",result[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
freopen("xoanoc.inp","r",stdin);
int ntest ;
scanf("%d",&ntest);
for(int i = 0; i < ntest; i++)
{
input();
solve();
output();
}
return 0;
}