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 in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Text;

class TEST{
	static void Main(){
		int t=0;
		while(true){
			var d=int.Parse(Console.ReadLine());
			if(d==0)break;
			Sol mySol=new Sol();
			t++;
			mySol.Ans(d,t);
		}
	}
}


class Sol{
	public void Ans(int d,int Case){
		Console.WriteLine("Case {0}:",Case);
		switch(d){
			case 1:
				Console.WriteLine("  1");
				break;
			case 2:
				Console.WriteLine("  1  2");
				Console.WriteLine("  3  4");
				break;
			case 3:
				Console.WriteLine("  1  2  6");
				Console.WriteLine("  3  5  7");
				Console.WriteLine("  4  8  9");
				break;
			case 4:
				Console.WriteLine("  1  2  6  7");
				Console.WriteLine("  3  5  8 13");
				Console.WriteLine("  4  9 12 14");
				Console.WriteLine(" 10 11 15 16");
				break;
			case 5:
				Console.WriteLine("  1  2  6  7 15");
				Console.WriteLine("  3  5  8 14 16");
				Console.WriteLine("  4  9 13 17 22");
				Console.WriteLine(" 10 12 18 21 23");
				Console.WriteLine(" 11 19 20 24 25");
				break;
			case 6:
				Console.WriteLine("  1  2  6  7 15 16");
				Console.WriteLine("  3  5  8 14 17 26");
				Console.WriteLine("  4  9 13 18 25 27");
				Console.WriteLine(" 10 12 19 24 28 33");
				Console.WriteLine(" 11 20 23 29 32 34");
				Console.WriteLine(" 21 22 30 31 35 36");
				break;
			case 7:
				Console.WriteLine("  1  2  6  7 15 16 28");
				Console.WriteLine("  3  5  8 14 17 27 29");
				Console.WriteLine("  4  9 13 18 26 30 39");
				Console.WriteLine(" 10 12 19 25 31 38 40");
				Console.WriteLine(" 11 20 24 32 37 41 46");
				Console.WriteLine(" 21 23 33 36 42 45 47");
				Console.WriteLine(" 22 34 35 43 44 48 49");
				break;
			case 8:
				Console.WriteLine("  1  2  6  7 15 16 28 29");
				Console.WriteLine("  3  5  8 14 17 27 30 43");
				Console.WriteLine("  4  9 13 18 26 31 42 44");
				Console.WriteLine(" 10 12 19 25 32 41 45 54");
				Console.WriteLine(" 11 20 24 33 40 46 53 55");
				Console.WriteLine(" 21 23 34 39 47 52 56 61");
				Console.WriteLine(" 22 35 38 48 51 57 60 62");
				Console.WriteLine(" 36 37 49 50 58 59 63 64");
				break;
			case 9:
				Console.WriteLine("  1  2  6  7 15 16 28 29 45");
				Console.WriteLine("  3  5  8 14 17 27 30 44 46");
				Console.WriteLine("  4  9 13 18 26 31 43 47 60");
				Console.WriteLine(" 10 12 19 25 32 42 48 59 61");
				Console.WriteLine(" 11 20 24 33 41 49 58 62 71");
				Console.WriteLine(" 21 23 34 40 50 57 63 70 72");
				Console.WriteLine(" 22 35 39 51 56 64 69 73 78");
				Console.WriteLine(" 36 38 52 55 65 68 74 77 79");
				Console.WriteLine(" 37 53 54 66 67 75 76 80 81");
				break;
			default:
				break;
		}
	}
}