AtCoder Regular Contest D - Grid Coloring Difficulty: 855
This theme, simulation
I don't think the content is difficult, but it is a problem that seems to take time to implement.
1 2 2 3 3
4 4 4 4 3
5 5 5 5 5
It is OK if you fill left and right in order from the top with the given numerical value. Ruby Array
ruby.rb
h, w = gets.split.map(&:to_i)
_ = gets.to_i
a = gets.split.map(&:to_i)
m = Array.new(h){Array.new(w, 0)}
y, x, lr = 0, 0, 1
a.each_with_index do |n, v|
n.times do
m[y][x] = v + 1
x += lr
if x == w
y += 1
x = w - 1
lr = -1
elsif x < 0
y += 1
x = 0
lr = 1
end
end
end
h.times do |i|
puts m[i].join(' ')
end
join.rb
puts m[i].join(' ')
puts m[i] * ' '
As I recently learned, join
can also be written as above.
Ruby Class
ruby.rb
class MASS
def initialize(h, w)
@h = h
@w = w
@m = Array.new(@h){Array.new(@w, 0)}
@x = 0
@y = 0
@LR = 1
end
def draw(v, n)
n.times do
@m[@y][@x] = v + 1
@x += @LR
if @x == @w
@y += 1
@x = @w - 1
@LR = -1
elsif @x < 0
@y += 1
@x = 0
@LR = 1
end
end
end
def out
@h.times do |i|
puts @m[i].join(' ')
end
end
end
h, w = gets.split.map(&:to_i)
_ = gets.to_i
a = gets.split.map(&:to_i)
m = MASS.new(h, w)
a.each_with_index do |v, i|
m.draw(i, v)
end
m.out
In this problem, it's not very effective, but I tried using class
for learning.
The code length has nearly doubled, but the execution time doesn't seem to change.
Python
python.py
from sys import stdin
def main():
input = stdin.readline
h, w = map(int, input().split())
_ = int(input())
a = list(map(int, input().split()))
m = [[0] * w for _ in range(h)]
x, y, LR, v = 0, 0, 1, 0
for n in a:
v += 1
for _ in range(n):
m[y][x] = str(v)
x += LR
if x == w:
y += 1
x = w - 1
LR = -1
elif x < 0:
y += 1
x = 0
LR = 1
for i in range(h):
print(" ".join(m[i]))
main()
Is the reason why the code length of *** Python *** is long is that it also counts half-width spaces?
Ruby Array | Ruby Class | Python | |
---|---|---|---|
Code length(Byte) | 392 | 631 | 603 |
Execution time(ms) | 17 | 18 | 25 |
memory(KB) | 2428 | 3836 | 3828 |
Recommended Posts