# Encoding

 Sorter: Uncategorized GNYR ICPC 2007 "http://www.acmgnyr.org/year2007/c.pdf"
 This problem has been solved by Mfv2.

This is problem C from the GNYR ICPC 2007

```
import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
public static void main(String[] args) throws Exception {
/*Scanner sc = new Scanner(System.in);
int lim = sc.nextInt();
int n = 1;*/
int n = 1;
while (lim-- > 0) {
//int r = sc.nextInt();
//int c = sc.nextInt();
//String in = sc.next();
int r = Integer.parseInt(s[0]);
int c = Integer.parseInt(s[1]);
String in = "";
if (s.length > 2) {
for (int y = 2; y < s.length; y++) {
in += s[y] + " ";
}
in.trim();
}
//System.out.println("in = " + in);
String o = "";
for (int i = 0; i < in.length(); i++) {
o += getb(in.charAt(i));
}
for (int i = o.length(); i < r * c; i++) {
o += "0";
}

String x = "";
char[][] m = new char[r][c];
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[0].length; j++) {
m[i][j] = 'X';
}
}

//R, D, L, U
//first 3, -0
//next 2, -1
//next 1, -1
//next 1, -1
int i = 0;
int row = 0;
int col = 0;
//System.out.println("r = "  +r + " c = " + c + " o = " + o + " o.length = " + o.length());
wloop:
while (i < o.length()) {
for (; col < c; col++) {
//System.out.println("R col = " + col + " row = "  +row);
if (m[row][col]  != 'X') break;
m[row][col] = o.charAt(i++);
}
row++;
col--;
for (; row < r; row++) {
//System.out.println("D col = " + col + " row = "  +row);
if (m[row][col]  != 'X') break;
m[row][col] = o.charAt(i++);

}
col--;
row--;
for (; col >= 0; col--) {
//System.out.println("L col = " + col + " row = "  +row);
if (m[row][col]  != 'X') break;
m[row][col] = o.charAt(i++);
}
row--;
col++;
for (; row >- 0; row--) {
//System.out.println("U col = " + col + " row = "  +row);
if (m[row][col]  != 'X') break;
m[row][col] = o.charAt(i++);
}
row++;
col++;
for (int k = 0; k < r; k++) {
for (int j = 0; j < c; j++) {
if (m[k][j] == 'X') continue wloop;
}
}
break;
}
/*for (int k = 0; k < r; k++) {
for (int j = 0; j < c; j++) {
System.out.print(m[k][j]);
}
System.out.println();
}*/
System.out.print(n++ + " ");
for (int k = 0; k < r; k++) {
for (int j = 0; j < c; j++) {
System.out.print(m[k][j]);
}
}
System.out.println();
/*for (int i = 0; i < R + c; i++) {
if (i % 4 == 0) {
for (int j = 0; j < r; j++) {

}
}
}*/
}
}

public static String getb(char a) {
int n = 0;
if (a == ' ')  {
n = 0;
} else {
n = a - 'A' + 1;
}
String o = BigInteger.valueOf((long)n).toString(2);
String x = new String(o);
switch (o.length()) {
case 0: x = "0" + x;
case 1: x = "0" + x;
case 2: x = "0" + x;
case 3: x = "0" + x;
case 4: x = "0" + x;
}
return x;
}
}

```