import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; // filling backwards should work anyway but it is easier to code this way // (assumed that for X steps we fill the entire rows/columns) public class zamboni_darko { private int[] dr = { -1, 0, 1, 0 }; private int[] dc = { 0, 1, 0, -1 }; private char[] c = new char[26]; private static final int STEPS_LIMIT = 50001; private void work() { for (char cc = 'A'; cc <= 'Z'; cc++) { c[cc - 'A'] = cc; } Scanner sc = new Scanner(new BufferedReader(new InputStreamReader( System.in))); int m = sc.nextInt(); int n = sc.nextInt(); int row = sc.nextInt() - 1; int col = sc.nextInt() - 1; long numOfSteps = sc.nextLong(); if (m < 1 || m > 2000 || n < 1 || n > 2000) { throw new RuntimeException( String.format( "number of rows/cols should be in [1,300], but we are given a %d X %d board\n", m, n)); } if (row < 0 || row >= m || col < 0 || col > n) { throw new RuntimeException(String.format( "Starting position out of board: (%d,%d)\n", row, col)); } if (numOfSteps < 1 || numOfSteps > 1000000000000000000L) { throw new RuntimeException(String.format( "Number of steps out of range: %d\n", numOfSteps)); } char[][] g = new char[m][n]; for (int i = 0; i < m; i++) { Arrays.fill(g[i], '.'); } if (numOfSteps < STEPS_LIMIT) { simulate(m, n, row, col, numOfSteps, g); } else { backFill(m, n, row, col, numOfSteps, g); } for (int i = 0; i < m; i++) { System.out.println(new String(g[i])); } } private void simulate(int m, int n, int row, int col, long numOfSteps, char[][] g) { int dir = 0; int stepSize = 1; int typeLimit = Math.max(m, n) + 1; for (int step = 0; step < numOfSteps; step++) { char color = c[step % 26]; if (step <= typeLimit) { for (int i = 0; i < stepSize; i++) { g[row][col] = color; row += dr[dir]; if (row < 0) row = m - 1; if (row == m) row = 0; col += dc[dir]; if (col < 0) col = n - 1; if (col == n) col = 0; } } else { switch (dir) { case 0: row = ((row - stepSize) % m + m) % m; for (int i = 0; i < m; i++) { g[i][col] = color; } break; case 1: col = (col + stepSize) % n; for (int i = 0; i < n; i++) { g[row][i] = color; } break; case 2: row = (row + stepSize) % m; for (int i = 0; i < m; i++) { g[i][col] = color; } break; case 3: col = ((col - stepSize) % n + n) % n; for (int i = 0; i < n; i++) { g[row][i] = color; } break; } } dir = (dir + 1) & 3; stepSize++; } g[row][col] = '@'; } private void backFill(int m, int n, int row, int col, long numOfSteps, char[][] g) { long[] diff = getDiffBySteps(numOfSteps); int dir = (int) ((numOfSteps - 1) & 3); dir = (dir + 2) & 3; // opposite direction int toCover = m * n - 1; row = (int) (((row + diff[0]) % m + m) % m); col = (int) (((col + diff[1]) % n + n) % n); g[row][col] = '@'; // in this case it is entire row/column... is it? while (toCover > 0 && numOfSteps > 0) { char color = c[(int) ((numOfSteps - 1) % 26)]; switch (dir) { case 0: row = (int) (((row - numOfSteps) % m + m) % m); for (int i = 0; i < m; i++) { if (g[i][col] == '.') { toCover--; g[i][col] = color; } } break; case 1: col = (int) ((col + numOfSteps) % n); for (int i = 0; i < n; i++) { if (g[row][i] == '.') { toCover--; g[row][i] = color; } } break; case 2: row = (int) ((row + numOfSteps) % m); for (int i = 0; i < m; i++) { if (g[i][col] == '.') { toCover--; g[i][col] = color; } } break; case 3: col = (int) (((col - numOfSteps) % n + n) % n); for (int i = 0; i < n; i++) { if (g[row][i] == '.') { toCover--; g[row][i] = color; } } break; } numOfSteps--; dir = (dir + 3) & 3; } } // rows change by sequence -1, +3, -5, +7, ... // sums are -1, 2, -3, 4,... // delta_row = (-1)^((steps+1)/2)*((steps+1)/2) // columns change by 2, -4, 6, -8, ... // sums are 2, -2, 4, -4, ... // delta_col = (-1)^((steps/2)+1)*(2*(((steps/2)+1)/2)) private long[] getDiffBySteps(long numOfSteps) { long delta_row = (numOfSteps + 1) / 2; if ((delta_row & 1) != 0) { delta_row = -delta_row; } long delta_col = 2 * (((numOfSteps / 2) + 1) / 2); if (((numOfSteps / 2) & 1) == 0) { delta_col = -delta_col; } return new long[] { delta_row, delta_col }; } public static void main(String[] args) { long startTime = System.currentTimeMillis(); new zamboni_darko().work(); System.err.printf("run time: %.2f seconds\n", 0.001 * (System.currentTimeMillis() - startTime)); } }