fork download
  1. def pixel(n, x,y):
  2. ma = max(x,y)+1
  3. mi = min(x,y)
  4. o= min(n-ma,mi)
  5. l= max(n-2*(o+1),0)
  6. p= x+y - 2*o
  7. if x<y: p= 4*(l+1)-p
  8. return p+l*l
  9.  
  10.  
  11. from sys import stdout as out
  12.  
  13. def print_grid(n):
  14. print '\nGenerating for N = %d\n' %n
  15. for y in xrange(n):
  16. for x in xrange(n):
  17. out.write(' %2d'%pixel(n,x,y))
  18. print
  19.  
  20. for n in [1,2,3,4,5,6,7,8,9]: print_grid(n)
Success #stdin #stdout 0.09s 10840KB
stdin
Standard input is empty
stdout
Generating for N = 1

  0

Generating for N = 2

  0  1
  3  2

Generating for N = 3

  1  2  3
  8  0  4
  7  6  5

Generating for N = 4

  4  5  6  7
 15  0  1  8
 14  3  2  9
 13 12 11 10

Generating for N = 5

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

Generating for N = 6

 16 17 18 19 20 21
 35  4  5  6  7 22
 34 15  0  1  8 23
 33 14  3  2  9 24
 32 13 12 11 10 25
 31 30 29 28 27 26

Generating for N = 7

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

Generating for N = 8

 36 37 38 39 40 41 42 43
 63 16 17 18 19 20 21 44
 62 35  4  5  6  7 22 45
 61 34 15  0  1  8 23 46
 60 33 14  3  2  9 24 47
 59 32 13 12 11 10 25 48
 58 31 30 29 28 27 26 49
 57 56 55 54 53 52 51 50

Generating for N = 9

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