Back
Close

Siamese Method

Statement

 Goal

For a grid of size n where n is always odd, you have to print the nxn 2D matrix with numbers ranging from 1 to n ^2.

The method followed like this: First you set number 1 to the centermost column of the top row. After this, move one row up and one column right, and place number 2 in that cell. We continue by moving one right, one up, and placing the number 3, etc.

You have to maintain two rules:
1) If the transition would leave us outside the bounds of the matrix, we jump to the opposite edge. In other words, if we go "over the right edge" we continue from the left edge, and if we go "over the top edge" we continue from the bottom.

2) if the cell up and right is already populated, then instead we populate the cell below the cell we last populated

For n =3, the centermost of the top cell would be 1 with position (0,1) then the next cell will be (2,2) because we can't move up so by following rule no. 1 we move down and right and fill (2,2) cell with 2. Again, following rule no. 1 we fill the cell (1,0) with 3. Then next cell will be (0,1) but that cell is already filled so move down and fill the cell (2,0) with 4. And so forth.
Input
Line 1: An integer n for the size of the matrix
Output
n lines: The matrix formed following the Siamese method. All numbers should be space-separated
Constraints
3<=n<=15
Example
Input
3
Output
8 1 6
3 5 7
4 9 2

Game modes
Fastest, Shortest

Test cases
Test 1 Test
Input
3
Output
8 1 6 3 5 7 4 9 2

Validator 1 Validator
Input
5
Output
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Test 2 Test
Input
7
Output
30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20

Validator 2 Validator
Input
9
Output
47 58 69 80 1 12 23 34 45 57 68 79 9 11 22 33 44 46 67 78 8 10 21 32 43 54 56 77 7 18 20 31 42 53 55 66 6 17 19 30 41 52 63 65 76 16 27 29 40 51 62 64 75 5 26 28 39 50 61 72 74 4 15 36 38 49 60 71 73 3 14 25 37 48 59 70 81 2 13 24 35

Test 3 Test
Input
11
Output
68 81 94 107 120 1 14 27 40 53 66 80 93 106 119 11 13 26 39 52 65 67 92 105 118 10 12 25 38 51 64 77 79 104 117 9 22 24 37 50 63 76 78 91 116 8 21 23 36 49 62 75 88 90 103 7 20 33 35 48 61 74 87 89 102 115 19 32 34 47 60 73 86 99 101 114 6 31 44 46 59 72 85 98 100 113 5 18 43 45 58 71 84 97 110 112 4 17 30 55 57 70 83 96 109 111 3 16 29 42 56 69 82 95 108 121 2 15 28 41 54

Validator 3 Validator
Input
13
Output
93 108 123 138 153 168 1 16 31 46 61 76 91 107 122 137 152 167 13 15 30 45 60 75 90 92 121 136 151 166 12 14 29 44 59 74 89 104 106 135 150 165 11 26 28 43 58 73 88 103 105 120 149 164 10 25 27 42 57 72 87 102 117 119 134 163 9 24 39 41 56 71 86 101 116 118 133 148 8 23 38 40 55 70 85 100 115 130 132 147 162 22 37 52 54 69 84 99 114 129 131 146 161 7 36 51 53 68 83 98 113 128 143 145 160 6 21 50 65 67 82 97 112 127 142 144 159 5 20 35 64 66 81 96 111 126 141 156 158 4 19 34 49 78 80 95 110 125 140 155 157 3 18 33 48 63 79 94 109 124 139 154 169 2 17 32 47 62 77

Test 4 Test
Input
15
Output
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120 138 155 172 189 206 223 15 17 34 51 68 85 102 119 121 154 171 188 205 222 14 16 33 50 67 84 101 118 135 137 170 187 204 221 13 30 32 49 66 83 100 117 134 136 153 186 203 220 12 29 31 48 65 82 99 116 133 150 152 169 202 219 11 28 45 47 64 81 98 115 132 149 151 168 185 218 10 27 44 46 63 80 97 114 131 148 165 167 184 201 9 26 43 60 62 79 96 113 130 147 164 166 183 200 217 25 42 59 61 78 95 112 129 146 163 180 182 199 216 8 41 58 75 77 94 111 128 145 162 179 181 198 215 7 24 57 74 76 93 110 127 144 161 178 195 197 214 6 23 40 73 90 92 109 126 143 160 177 194 196 213 5 22 39 56 89 91 108 125 142 159 176 193 210 212 4 21 38 55 72 105 107 124 141 158 175 192 209 211 3 20 37 54 71 88 106 123 140 157 174 191 208 225 2 19 36 53 70 87 104

Validator 4 Validator
Input
15
Output
122 139 156 173 190 207 224 1 18 35 52 69 86 103 120 138 155 172 189 206 223 15 17 34 51 68 85 102 119 121 154 171 188 205 222 14 16 33 50 67 84 101 118 135 137 170 187 204 221 13 30 32 49 66 83 100 117 134 136 153 186 203 220 12 29 31 48 65 82 99 116 133 150 152 169 202 219 11 28 45 47 64 81 98 115 132 149 151 168 185 218 10 27 44 46 63 80 97 114 131 148 165 167 184 201 9 26 43 60 62 79 96 113 130 147 164 166 183 200 217 25 42 59 61 78 95 112 129 146 163 180 182 199 216 8 41 58 75 77 94 111 128 145 162 179 181 198 215 7 24 57 74 76 93 110 127 144 161 178 195 197 214 6 23 40 73 90 92 109 126 143 160 177 194 196 213 5 22 39 56 89 91 108 125 142 159 176 193 210 212 4 21 38 55 72 105 107 124 141 158 175 192 209 211 3 20 37 54 71 88 106 123 140 157 174 191 208 225 2 19 36 53 70 87 104

Solution language

Solution

Stub generator input