[Timus] 1313 Some Words about Sport

Problem statement is here: http://acm.timus.ru/problem.aspx?space=1&num=1313

This is an easy problem of simulation. Try to find out how to solve this manually and then code as it is. The solution is to traverse in the matrix diagonally and print as you traverse.

package codes;

import FastIO.InputReader;
import FastIO.OutputWriter;

public class Task1313 {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.nextInt();
        int[][] screen = new int[n][n];
        for (int i = 0; i < n; i++) {
            screen[i] = in.nextIntArray(n);

        // Final result is stored here.
        StringBuilder result = new StringBuilder();

        // We have to traverse first element of each row and then each 
        // element of the last row.
        // From the starting element we have to move upwards to the right.
        // 2*n-1 == number of elements at the left and down border of the
        // matrix.
        // To move back keep incrementing the column number.
        for (int i = 0; i < 2 * n - 1; i++) {
            int row = i;
            int col = 0;
            // If traversing the matrix along the bottom border.
            if (i >= n) {
                // Increment the steps to get the column number.
                while (row >= n) {

            // Starting from the current element keep going up and right
            // diagonally.
            while (row >= 0 && col < n) {
                result.append(" ");


One thought on “[Timus] 1313 Some Words about Sport

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.