There is a grid of r rows and c columns, and every cell starts at 0. The first line gives r, c and the number of stamps k, and each of the next k lines gives "y x". For each stamp, add 1 to the cell in row y, column x (both counted from 1). After all stamps, print the grid as r lines, each with c numbers separated by single spaces.
Input
1 ≤ r ≤ 100, 1 ≤ c ≤ 100, 0 ≤ k ≤ 10,000, 1 ≤ y ≤ r, 1 ≤ x ≤ c
Output
r lines of c numbers each
Example 1 · stdin
2 3 3
1 1
2 3
1 1
stdout
2 0 0
0 0 1
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
Each language makes a zero-filled 2D list differently. Java new int[r][c] and C# new int[r, c] start with every cell 0. In C a local array is not guaranteed to start at 0, so write int grid[100][100] = {0};. Without it the cells hold indeterminate values, and sometimes they happen to be 0 and the program seems to work (global and static arrays do start at 0). C++ std::vector<std::vector<int>> grid(r, std::vector<int>(c, 0)) copies the inner vector separately into each row. The traps are in Python and JavaScript. Python [[0] * c] * r holds the same row r times, so stamping one cell changes that column in every row; build each row anew with [[0] * c for _ in range(r)]. JavaScript/TypeScript new Array(r).fill(new Array(c).fill(0)) likewise fills every row with one shared array; write Array.from({ length: r }, () => new Array(c).fill(0)). Craykon NewList(r, NewList(c, 0)) copies a list value separately into each slot, so rows are not tied together.
Pythonmain.py · 7 lines
r, c, k = map(int, input().split())grid = [[0] * c for _ in range(r)] # 줄마다 새 목록for _ in range(k): y, x = map(int, input().split()) grid[y - 1][x - 1] += 1for row in grid: print(*row)
JavaScriptmain.js · 5 lines
const nums = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [r, c, k] = nums;const grid = Array.from({ length: r }, () => new Array(c).fill(0)); // 줄마다 새 배열for (let i = 0; i < k; i++) grid[nums[3 + 2 * i] - 1][nums[4 + 2 * i] - 1]++;console.log(grid.map((row) => row.join(" ")).join("\n"));
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const nums: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const [r, c, k] = nums;const grid: number[][] = Array.from({ length: r }, () => new Array<number>(c).fill(0)); // 줄마다 새 배열for (let i = 0; i < k; i++) grid[nums[3 + 2 * i] - 1][nums[4 + 2 * i] - 1]++;console.log(grid.map((row) => row.join(" ")).join("\n"));
JavaMain.java · 27 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()); int c = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); int[][] grid = new int[r][c]; // 칸은 처음부터 0 for (int i = 0; i < k; i++) { st = new StringTokenizer(in.readLine()); int y = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); grid[y - 1][x - 1]++; } StringBuilder out = new StringBuilder(); for (int[] row : grid) { for (int j = 0; j < c; j++) out.append(j > 0 ? " " : "").append(row[j]); out.append('\n'); } System.out.print(out); }}
C#Main.cs · 25 lines
using System;using System.Linq;using System.Text;class Program{ static void Main() { int[] rck = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); int r = rck[0], c = rck[1], k = rck[2]; int[,] grid = new int[r, c]; // 칸은 처음부터 0 for (int i = 0; i < k; i++) { int[] yx = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); grid[yx[0] - 1, yx[1] - 1]++; } var out_ = new StringBuilder(); for (int y = 0; y < r; y++) { for (int x = 0; x < c; x++) out_.Append(x > 0 ? " " : "").Append(grid[y, x]); out_.Append('\n'); } Console.Write(out_); }}
C++main.cpp · 17 lines
#include <iostream>#include <vector>int main() { std::ios::sync_with_stdio(false); int r, c, k, y, x; std::cin >> r >> c >> k; std::vector<std::vector<int>> grid(r, std::vector<int>(c, 0)); // 줄마다 따로 복사된다 for (int i = 0; i < k; i++) { std::cin >> y >> x; grid[y - 1][x - 1]++; } for (const auto& row : grid) { for (int j = 0; j < c; j++) std::cout << (j ? " " : "") << row[j]; std::cout << "\n"; }}
Cmain.c · 16 lines
#include <stdio.h>int main(void) { int r, c, k, y, x; int grid[100][100] = {0}; /* 함수 안 배열은 = {0} 이 있어야 0으로 시작한다 */ scanf("%d %d %d", &r, &c, &k); for (int i = 0; i < k; i++) { scanf("%d %d", &y, &x); grid[y - 1][x - 1]++; } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) printf(j ? " %d" : "%d", grid[i][j]); putchar('\n'); } return 0;}
Craykonmain.cray · 15 lines
rck = Console.ReadNumbers();r = rck[0];c = rck[1];k = rck[2];// 새목록에 목록을 주면 칸마다 따로 복사해 넣는다grid = NewList(r, NewList(c, 0));for (i = 0; i < k; i++){ yx = Console.ReadNumbers(); grid[yx[0] - 1][yx[1] - 1] += 1;}foreach (row in grid){ Console.WriteLine(row);}