paiza POH Vol.5
解答(ミナミ)
#!/usr/bin/env ruby file = $stdin lines = file.readlines file.close x, y = lines[0].split.map{|a| a.to_i} t = lines[1, y].map{|b| b.split.map{|a| a.to_i}} count_list = [] for i in 0..x - 1 count = 0 for j in 0..y - 1 count += 1 if t[j][i] == 1 end count_list += [count] end for j in 0..y - 1 line = "" for i in 0..x - 1 c = count_list[i] > y - 1 - j ? 1 : 0 line += c.to_s + " " end puts line.rstrip end
解答(レナ)
#!/usr/bin/env ruby file = $stdin lines = file.readlines file.close x, y, n = lines[0].split.map{|a| a.to_i} t = lines[1, y].map{|b| b.split.map{|a| a.to_i}} rects = lines[y + 1, n].map{|b| b.split.map{|a| a.to_i}} sum = 0 for rect in rects for pos_y in (rect[1] - 1)..(rect[3] - 1) for pos_x in (rect[0] - 1)..(rect[2] - 1) sum += t[pos_y][pos_x] t[pos_y][pos_x] = 0 end end end puts sum