fork(9) download
  1. #define log(x) printf(#x": %d\n", x)
  2. a,b,c,d,e,i,j,x,y,**z;
  3. p(){
  4. j=y;
  5. while(j--){
  6. i=x;
  7. while(i--)printf("%3d ",z[x-i-1][y-j-1]);
  8. puts("");
  9. }
  10. }
  11. f(){
  12. if(c>0&&!d&&x-i-1==j){
  13. c=0;
  14. d=1;
  15. }
  16. else if(!c&&d>0&&x-i-1-y%2==y-j-1){
  17. c=-1;
  18. d=0;
  19. }
  20. else if(c<0&&!d&&i+!(y%2)-(x<y?!(x%2):0)==y-j-1){
  21. c=0;
  22. d=-1;
  23. }
  24. else if(!c&&d<0&&i-(x<y?!(x%2):0)==j){
  25. c=1;
  26. d=0;
  27. }
  28. i+=c;
  29. j+=d;
  30. }
  31. main(){
  32. scanf("%d%d",&x,&y);
  33. z=malloc(x*4);
  34. i=x;
  35. while(i--)z[i]=malloc(y*4);
  36.  
  37. if(x<y){
  38. a=x/2;
  39. b=y/2;
  40. a=a<b?a:b;
  41. b=(a<b?a:b)-!(x%2);
  42. d=1;
  43. }
  44. else{
  45. a=x/2-!(x%2);
  46. b=y/2-!(y%2);
  47. a=a<b?a:b;
  48. b=a<b?a:b;
  49. c=1;
  50. }
  51.  
  52. i=a;
  53. j=b;
  54.  
  55. while(e++<x*y){
  56. z[i][j]=e;
  57. f();
  58. }
  59.  
  60. p();
  61. }
Success #stdin #stdout 0s 1968KB
stdin
20 18
stdout
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 
304 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 325 
303 238 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 257 326 
302 237 180 131 132 133 134 135 136 137 138 139 140 141 142 143 144 197 258 327 
301 236 179 130  89  90  91  92  93  94  95  96  97  98  99 100 145 198 259 328 
300 235 178 129  88  55  56  57  58  59  60  61  62  63  64 101 146 199 260 329 
299 234 177 128  87  54  29  30  31  32  33  34  35  36  65 102 147 200 261 330 
298 233 176 127  86  53  28  11  12  13  14  15  16  37  66 103 148 201 262 331 
297 232 175 126  85  52  27  10   1   2   3   4  17  38  67 104 149 202 263 332 
296 231 174 125  84  51  26   9   8   7   6   5  18  39  68 105 150 203 264 333 
295 230 173 124  83  50  25  24  23  22  21  20  19  40  69 106 151 204 265 334 
294 229 172 123  82  49  48  47  46  45  44  43  42  41  70 107 152 205 266 335 
293 228 171 122  81  80  79  78  77  76  75  74  73  72  71 108 153 206 267 336 
292 227 170 121 120 119 118 117 116 115 114 113 112 111 110 109 154 207 268 337 
291 226 169 168 167 166 165 164 163 162 161 160 159 158 157 156 155 208 269 338 
290 225 224 223 222 221 220 219 218 217 216 215 214 213 212 211 210 209 270 339 
289 288 287 286 285 284 283 282 281 280 279 278 277 276 275 274 273 272 271 340 
360 359 358 357 356 355 354 353 352 351 350 349 348 347 346 345 344 343 342 341