Snippets

cia_rana 【誕生日問題10月】金山をたくさんプレゼント

Created by cia_rana last modified
import numpy as np
import sys

# kernel size
SIZE = 5

# input, convert each character of an inputed string into a binary digit(if that is G, then 1; otherwize 0)
# and calculate a cumulative sum sequentially for each axis.
# cm: Cumulative sum Map
cm = np.array([[int(c == "G") for c in line.rstrip()] for line in sys.stdin]).cumsum(0).cumsum(1)

# calculate the number of G at each area.
# ea: Each Area
height, width = cm.shape
ea = cm[:height-SIZE, :width-SIZE] + cm[SIZE:height+SIZE, SIZE:width+SIZE] \
         - cm[:height-SIZE, SIZE:width+SIZE] - cm[SIZE:height+SIZE, :width-SIZE]

# argmax of ea.
# mp: Max Point
mp = np.unravel_index(ea.argmax(), ea.shape)

# output.
print('{"x":%d,"y":%d,"g":%d}' % (mp[1] + 1, mp[0] + 1, ea[mp]))
AREA_SIZE = 5

class Array
  def cumsum
    sum = 0
    self.map{|e| sum += e }
  end
end

area_map = $<.map{|line|
  line.chomp.chars.map{|c|c==?w?0:1}
}
height = area_map.size
width = area_map[0].size

cumulative_sum = area_map.transpose.map(&:cumsum).transpose.map(&:cumsum)

max_g = 0
max_point = [0, 0] # (x, y)
(height-AREA_SIZE).times{|i|
  (width-AREA_SIZE).times{|j|
    area_g = cumulative_sum[i+AREA_SIZE][j+AREA_SIZE] - cumulative_sum[i+AREA_SIZE][j] - cumulative_sum[i][j+AREA_SIZE] + cumulative_sum[i][j]
    if max_g < area_g
      max_g = area_g
      max_point = [j, i]
    end
  }
}
puts '{"x":%d,"y":%d,"g":%d}' % (max_point.map(&:succ) + [max_g])
wwGwwwwwGG
Gwwwwwwwww
wwwwwwwwww
Gwwwwwwwww
wwwwGwwGww
wGwwwwwwww
wwwGGwwwww
wwwwwwGwww
wwwwGGwwww
GwwwGGwGwG

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.