A patchwork has R rows and C columns, and each cell holds one lowercase English letter.
Rows are numbered from 1 at the top, and columns from 1 at the left.
The cells whose (row number + column number − 1) equals d form stripe d. Stripes run from 1 to R + C − 1.
To read one stripe, start at its cell in the lowest row and go up row by row (from the lower left to the upper right), joining the letters.
Starting at stripe a and stepping by s, read in order the stripes numbered a, a + s, a + 2 × s, … that are at most b.
The first line gives R and C separated by a single space.
Each of the next R lines gives a string of length C.
The last line gives a, b and s separated by single spaces.
For each stripe read, print the joined string on one line, from the smallest stripe number.
Do not look at cells outside the stripe: compute where the loop starts and ends and visit only the cells of that stripe.
Input
1 ≤ R ≤ 100. 1 ≤ C ≤ 100. 1 ≤ a ≤ b ≤ R + C − 1. 1 ≤ s ≤ R + C − 1. All letters are lowercase English letters
Output
One line for each stripe read
Example 1 · stdin
3 4
abcd
efgh
ijkl
2 6 2
stdout
eb
jgd
l
Example 2 · stdin
4 2
ab
cd
ef
gh
1 5 1
stdout
a
cb
ed
gf
h
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
A loop that counts with a step is Python for d in range(a, b + 1, s), and for (d = a; d <= b; d += s) in the other languages (Craykon for (d = a; d <= b; d += s)). Inside one stripe the 0-based row r and column c satisfy r + c = d − 1, so count the row backward from min(d - 1, R - 1) to max(0, d - C) and get the column as d - 1 - r. Counting backward is Python range(low, high - 1, -1) and for (r = low; r >= high; r--) elsewhere. Smaller and larger are Python min/max, JS/TS/Java Math.min/Math.max, C# Math.Min/Math.Max, C++ std::min/std::max, Craykon Mathf.Min/Mathf.Max, and in C you write a < b ? a : b. Trap 1: Python range leaves out the end value. With range(a, b, s), stripe b is missing whenever (b − a) is a multiple of s (in the first example the last line, l, is not printed). Counting backward is the same: range(low, high, -1) misses the top cell (the first example becomes e, jg and a line with nothing). Trap 2: the top row is decided by the number of columns C. Using R, as in max(0, d - R), is right only for a square; on a wide patchwork letters are missing, and on a tall one cells that do not exist are read. Trap 3: if you do not compute the range but go through every row and check only c < C, the cells with a negative c remain. Python reads grid[r][-1] without an error as the last letter of that row, so the first line of the first example becomes leb. JS/TS give undefined, so "undefined" is appended; Java and C# stop with an exception; Craykon also stops, saying the text only has positions 0 to 3; C and C++ read outside the array. Trap 4: going the other way (from the upper row down) reverses the string. Trap 5: writing d++ instead of d += s does not skip, so every stripe is printed.
Pythonmain.py · 20 lines
import sysdef main(): lines = sys.stdin.read().split() R, C = int(lines[0]), int(lines[1]) grid = lines[2:2 + R] a, b, s = (int(x) for x in lines[2 + R:5 + R]) out = [] for d in range(a, b + 1, s): # range는 끝을 넣지 않으므로 b + 1까지 # d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 low = min(d - 1, R - 1) # 가장 아래 줄 high = max(0, d - C) # 가장 위 줄 # 거꾸로 셀 때도 끝은 들어가지 않는다: high - 1까지 out.append("".join(grid[r][d - 1 - r] for r in range(low, high - 1, -1))) print("\n".join(out))main()
JavaScriptmain.js · 18 lines
const lines = require("fs").readFileSync(0, "utf8").split(/\s+/).filter((s) => s !== "");const R = Number(lines[0]);const C = Number(lines[1]);const grid = lines.slice(2, 2 + R);const [a, b, s] = lines.slice(2 + R, 5 + R).map(Number);const out = [];for (let d = a; d <= b; d += s) { // d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 const low = Math.min(d - 1, R - 1); // 가장 아래 줄 const high = Math.max(0, d - C); // 가장 위 줄 let line = ""; for (let r = low; r >= high; r--) { line += grid[r][d - 1 - r]; } out.push(line);}console.log(out.join("\n"));
TypeScriptmain.ts · 22 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split(/\s+/).filter((s) => s !== "");const R: number = Number(lines[0]);const C: number = Number(lines[1]);const grid: string[] = lines.slice(2, 2 + R);const a: number = Number(lines[2 + R]);const b: number = Number(lines[3 + R]);const s: number = Number(lines[4 + R]);const out: string[] = [];for (let d = a; d <= b; d += s) { // d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 const low: number = Math.min(d - 1, R - 1); // 가장 아래 줄 const high: number = Math.max(0, d - C); // 가장 위 줄 let line = ""; for (let r = low; r >= high; r--) { line += grid[r][d - 1 - r]; } out.push(line);}console.log(out.join("\n"));
JavaMain.java · 30 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); int R = Integer.parseInt(st.nextToken()), C = Integer.parseInt(st.nextToken()); String[] grid = new String[R]; for (int r = 0; r < R; r++) { grid[r] = in.readLine().trim(); } st = new StringTokenizer(in.readLine()); int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken()), s = Integer.parseInt(st.nextToken()); StringBuilder out = new StringBuilder(); for (int d = a; d <= b; d += s) { // d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 int low = Math.min(d - 1, R - 1); // 가장 아래 줄 int high = Math.max(0, d - C); // 가장 위 줄 for (int r = low; r >= high; r--) { out.append(grid[r].charAt(d - 1 - r)); } out.append('\n'); } System.out.print(out); }}
C#Main.cs · 32 lines
using System;using System.Text;class Program{ static void Main() { string[] head = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int R = int.Parse(head[0]), C = int.Parse(head[1]); string[] grid = new string[R]; for (int r = 0; r < R; r++) { grid[r] = Console.ReadLine().Trim(); } string[] tail = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int a = int.Parse(tail[0]), b = int.Parse(tail[1]), s = int.Parse(tail[2]); var sb = new StringBuilder(); for (int d = a; d <= b; d += s) { // d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 int low = Math.Min(d - 1, R - 1); // 가장 아래 줄 int high = Math.Max(0, d - C); // 가장 위 줄 for (int r = low; r >= high; r--) { sb.Append(grid[r][d - 1 - r]); } sb.Append('\n'); } Console.Write(sb); }}
C++main.cpp · 26 lines
#include <algorithm>#include <iostream>#include <string>#include <vector>int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int R, C; std::cin >> R >> C; std::vector<std::string> grid(R); for (auto& row : grid) std::cin >> row; int a, b, s; std::cin >> a >> b >> s; for (int d = a; d <= b; d += s) { // d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 int low = std::min(d - 1, R - 1); // 가장 아래 줄 int high = std::max(0, d - C); // 가장 위 줄 std::string line; for (int r = low; r >= high; r--) { line += grid[r][d - 1 - r]; } std::cout << line << '\n'; }}
Cmain.c · 23 lines
#include <stdio.h>static char grid[100][101];int main(void) { int R, C, a, b, s; scanf("%d %d", &R, &C); for (int r = 0; r < R; r++) { scanf("%100s", grid[r]); } scanf("%d %d %d", &a, &b, &s); for (int d = a; d <= b; d += s) { /* d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 */ int low = d - 1 < R - 1 ? d - 1 : R - 1; /* 가장 아래 줄 */ int high = d - C > 0 ? d - C : 0; /* 가장 위 줄 */ for (int r = low; r >= high; r--) { putchar(grid[r][d - 1 - r]); } putchar('\n'); } return 0;}
Craykonmain.cray · 25 lines
head = Console.ReadNumbers();R = head[0];C = head[1];grid = [];for (r = 0; r < R; r++){ grid.Add(Console.ReadLine().Trim());}tail = Console.ReadNumbers();a = tail[0];b = tail[1];s = tail[2];for (d = a; d <= b; d += s){ // d번 사선의 칸은 (0부터 센) 줄 r + 칸 c = d - 1을 만족한다 low = Mathf.Min(d - 1, R - 1); high = Mathf.Max(0, d - C); line = ""; for (r = low; r >= high; r--) { line += grid[r][d - 1 - r]; } Console.WriteLine(line);}