Problem:
JPEG Compression The fundamental idea in the JPEG compression algorithm is to sort coeffi-
cient of given image by zigzag path and encode it. In this problem, we don’t
discuss about details of the algorithm, but you are asked to make simple pro-
gram. You are given single integer N , and you must output zigzag path on
a matrix where size is N by N . The zigzag scanning is start at the upper-left
corner (0, 0) and end up at the bottom-right corner. See the following Figure
and sample output to make sure rule of the zigzag scanning. For example, if you
are given N = 8, corresponding output should be a matrix shown in right-side
of the Figure. This matrix consists of visited time for each element.   Input Several test cases are given. Each test case consists of one integer N (0 < N <
10) in a line. The input will end at a line contains single zero. Output For each input, you must output a matrix where each element is the visited
time. All numbers in the matrix must be right justified in a field of width 3.
Each matrix should be prefixed by a header “Case x:” where x equals test case
number. Sample Input 3
4
0 Output for the Sample Input Case 1:
  1  2  6
  3  5  7
  4  8  9
Case 2:
  1  2  6  7
  3  5  8 13
  4  9 12 14
 10 11 15 16

Solution (Python):
case = 1
while True:
    N = int(raw_input())
    if N == 0: break
    
    mat = [[0 for i in range(N)] for j in range(N)]
    
    n = 1
    for i in range(N): 
        for j in range(i,-1,-1):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
                
    for i in range(N,2*N-1): 
        for j in range(N-1,i-N,-1):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
                
    print 'Case %d:'%(case);  case += 1
    
    for s in mat:
        print ''.join(map(str,[str(st).rjust(3) for st in s]))

Solution (Python):


case = 1
while True:
    N = int(raw_input())
    if N == 0: break
    mat = [[0 for i in xrange(N)] for j in xrange(N)]
    n = 1
    for i in range(N): 
        for j in reversed(xrange(i+1)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
                 
    for i in range(N,2*N-1): 
        for j in reversed(xrange(i-N+1,N)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
    print 'Case %d:'%(case); case += 1
    for s in mat:
        print ''.join(map(str,[str(st).rjust(3) for st in s]))

Solution (Python):


case = 1
while True:
    N = int(raw_input())
    if N == 0: break
    mat = [[0 for i in xrange(N)] for j in xrange(N)]
    n = 1
    for i in xrange(N): 
        for j in reversed(xrange(i+1)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
                 
    for i in xrange(N,2*N-1): 
        for j in reversed(xrange(i-N+1,N)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
    print 'Case %d:'%(case); case += 1
    for s in mat:
        print ''.join(map(str,[str(st).rjust(3) for st in s]))

Solution (Python):

case = 1
while True:
    N = int(raw_input())
    if N == 0: break
    mat = [[0 for i in xrange(N)] for j in xrange(N)]
    n = 1
    for i in xrange(N): 
        for j in reversed(xrange(i+1)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
                 
    for i in xrange(N,2*N-1): 
        for j in reversed(xrange(i-N+1,N)):
            if i%2==0:
                mat[j][i-j] = n
                n += 1
            elif i%2==1:
                mat[i-j][j] = n
                n += 1
    print 'Case %d:'%(case); case += 1
    for s in mat:
        print ''.join(map(str,[str(st).rjust(3) for st in s]))

Solution (Python):

    

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


def xyz(num):
    num = str(num)
    if(len(num)==1):
        return "  "+num
    return " "+num


n = 0
while 1:
    N = int(raw_input())
    if(N==0):break
    n +=1
    hairetu = [[0 for i in range(N)] for j in range(N)]
    for i in range(N):
        for j in range(N):
            if(i+j<N):hairetu[i][j]=otehon[i][j]
    
    for i in range(N):
        for j in range(N):
            if(i+j>=N):hairetu[i][j]=N*N +1 - hairetu[N-1-i][N-1-j]
    
    print "Case "+str(n)+":"
    for i in range(N):
            strn = ""
            for j in range(N):
                strn += xyz(hairetu[i][j])
            print strn

Solution (Python):
case = 1
while True:
    n = int(input())
    if not n:
        break
    jpeg = [[0] * n for _ in range(n)]
    n1 = n - 1
    px, cur = [0, 0], 1
    while px[0] < n:
        i, j = px
        jpeg[i][j] = cur
        odd = (i + j) % 2
        if px[not odd] == n1:
            px[odd] += 1
        elif not px[odd]:
            px[not odd] += 1
        else:
            px[not odd] += 1
            px[odd] -= 1
        cur += 1

    print('Case {}:'.format(case))
    for row in jpeg:
        print(''.join('{:>3}'.format(pixel) for pixel in row))

    case += 1

Solution (Python):

q=1
while(1):

    n=int(raw_input())
    if n==0:    break
    A=[[0 for i in range(n)] for i in range(n)]
    up=1
    m=1
    for i in range(n):
        for j in range(i+1):
            if up:
                A[i-j][j]=m
            else:
                A[j][i-j]=m
            m+=1
        up=not up
    for i in range(n,(n-1)*2+1):
        for j in range(i-n+1,n):
            if up:
                A[i-j][j]=m
            else:
                A[j][i-j]=m
            m+=1
        up=not up
    print 'Case '+str(q)+':'
    for a in A:
        print ''.join([ '  '+str(x) if x<10 else ' '+str(x) for x in a])
    q+=1

Solution (Python):
import sys

def p(t):
    for l in t:
        for n in l:
            print("{0:>3}".format(n),end="")
        print("")

c = 0
for n in sys.stdin:
    n = int(n)

    if n == 0:
        break

    t = [ [ 0 for _ in range(n) ] for _ in range(n) ]

    c += 1
    print("Case {}:".format(c))

    x = 0
    y = 0
    up= True
    for i in range(1,n*n+1):

        t[y][x] = i
        if up:
            if y == 0:
                if x < n-1:
                    x += 1
                    up = False
                else:
                    y += 1
                    up = False
            else:
                if x < n-1:
                    x += 1
                    y -= 1
                else:
                    y += 1
                    up = False
                    
        else:
            if x == 0:
                if y < n-1:
                    y += 1
                    up = True
                else:
                    x += 1
                    up = True
            else:
                if y < n-1:
                    x -= 1
                    y += 1
                else:
                    x += 1
                    up = True

    p(t)

Solution (Python):
case=1
while True:
    n=input()
    if n==0:break
    value=1
    grid=[[0]*n for i in range(n)]
    x=y=0
    if n==1:
        grid[y][x]=1
    while value<n*n:
        grid[y][x]=value
        if x<n-1:
            x+=1
            value+=1
            grid[y][x]=value
        else:
            y+=1
            value+=1
            grid[y][x]=value
        while x>0 and y<n-1:
            y+=1
            x-=1
            value+=1
            grid[y][x]=value
        if y<n-1:
            y+=1
            value+=1
            grid[y][x]=value
        else:
            x+=1
            value+=1
            grid[y][x]=value
        while y>0 and x<n-1:
            y-=1
            x+=1
            value+=1
            grid[y][x]=value
    print "Case %d:"%case
    for i in range(n):
        print "%3d"*n%tuple(grid[i])
    case+=1

Solution (Python):
if __name__ == '__main__':
    TC = 1
    while True:
        N = int(input())
        if N == 0:
            break

        arr = [ [ 0 for _ in range(N) ] for _ in range(N) ]

        i = 0
        j = 0
        val = 1
        prevMove = "UR"

        while True:
            arr[i][j] = val
            val += 1

            if i == N - 1 and j == N - 1:
                break
            elif i == 0:
                if prevMove == "UR":
                    if j != N - 1:
                        j += 1
                        prevMove = "R"
                    else:
                        i += 1
                        prevMove = "D"
                else:
                    i += 1
                    j -= 1
                    prevMove = "DL"
            elif i == N - 1:
                if prevMove == "DL":
                    j += 1
                    prevMove = "R"
                else:
                    i -= 1
                    j += 1
                    prevMove = "UR"
            elif j == 0:
                if prevMove == "DL":
                    i += 1
                    prevMove = "D"
                else:
                    i -= 1
                    j += 1
                    prevMove = "UR"
            elif j == N - 1:
                if prevMove == "UR":
                    i += 1
                    prevMove = "D"
                else:
                    i += 1
                    j -= 1
                    prevMove = "DL"
            else:
                if prevMove == "DL":
                    i += 1
                    j -= 1
                else:
                    i -= 1
                    j += 1

        print("Case {}:".format(TC))
        TC += 1

        for i in range(N):
            for j in range(N):
                print("{:3d}".format(arr[i][j]), end='')
            print()

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

for hoge in range(1, 100):
    n = input()
    if n == 0:
        break
    s, t = 1 - n, 1
    cnt = 1
    sign = -1
    A = ['' for i in range(n)]
    for loop in range(n * 3):
        if sign == 1:
            r = range(max(s, 0), min(t, n))
        else:
            r = range(min(t - 1, n - 1), max(s - 1, -1), -1)
        for i in r:
            A[i] += str(cnt).rjust(3)
            cnt += 1
        s += 1
        t += 1
        sign *= -1
    print "Case " + str(hoge) + ":"
    for i in range(n):
        print A[i]

Solution (Python):
t = 1
while 1:
    N = int(input())
    if N == 0:
        break
    print("Case %d:" % t); t += 1
    M = [[0]*N for i in range(N)]
    c = 1
    for i in range(2*N-1):
        for j in range(max(i-N+1, 0), min(N, i+1)):
            if i % 2 == 0:
                M[i-j][j] = "%3d" % c
            else:
                M[j][i-j] = "%3d" % c
            c += 1
    for l in M:
        print(*l,sep='')

Solution (Python):



import sys
from sys import stdin
input = stdin.readline

cno = 0
while True:
	n = int(input())
	if n == 0: break
	cno += 1
	a = [[0 for j in range(12)] for i in range(12)]
	m = k = f = 1;
	while True:
		if f:
			for r in range(k-1, -1, -1):
				a[r][k-1-r] = m
				m += 1
		else:
			for c in range(k-1, -1, -1):
				a[k-1-c][c] = m
				m += 1
		f = 1-f
		k += 1
		if k > n: break
	k = n-1
	while True:
		if f:
			for c in range(n-k, n):
				a[2*n-1-k-c][c] = m
				m += 1
		else:
			for r in range(n-k, n):
				a[r][2*n-1-k-r] = m
				m += 1
		f = 1-f
		k -= 1
		if k < 1: break
	print("Case ", cno, ":", sep='')
	for r in range(n):
		for c in range(n): print(format(a[r][c], "3d"), end='')
		print()