Last active
May 18, 2017 05:53
-
-
Save parrot-studio/568a4df14644bc46a1ddbaace08b8b7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://paiza.jp/logic_summoner/challenges/logics_skill_4005 | |
| def mark(board, table, w, max, color, base, ind) | |
| return if table[ind] | |
| return unless color == board[ind] | |
| r = ind % w | |
| table[ind] = base | |
| mark(board, table, w, max, color, base, ind - 1) if r > 0 # left | |
| mark(board, table, w, max, color, base, ind + 1) if r < w - 1 # right | |
| mark(board, table, w, max, color, base, ind - w) if ind >= w # up | |
| mark(board, table, w, max, color, base, ind + w) if ind + w <= max # down | |
| end | |
| def wipe(board, table, w, max, color, base, ind) | |
| return unless board[ind] == color | |
| return unless table[ind] == base | |
| board[ind] = nil | |
| r = ind % w | |
| wipe(board, table, w, max, color, base, ind - 1) if r > 0 # left | |
| wipe(board, table, w, max, color, base, ind + 1) if r < w - 1 # right | |
| wipe(board, table, w, max, color, base, ind - w) if ind >= w # up | |
| wipe(board, table, w, max, color, base, ind + w) if ind + w <= max # down | |
| end | |
| def fall(board, w, max, ind) | |
| return unless board[ind] | |
| target = ind + w | |
| loop do | |
| break if target > max | |
| break if board[target] | |
| board[target] = board[ind] | |
| board[ind] = nil | |
| target += w | |
| ind += w | |
| end | |
| end | |
| # main | |
| w, h, spell = gets.split(' ').map(&:to_i) | |
| board = h.times.map{ gets.chomp.chars }.flatten | |
| max = w*h-1 | |
| ans = [] | |
| loop do | |
| table = [] | |
| board.each.with_index do |c, i| | |
| next unless c | |
| mark(board, table, w, max, c, i, i) | |
| end | |
| point, size = table.compact.sort.chunk_while{|l, r| l == r}.map{|l| [l.first, l.size]}.max_by(&:last) | |
| break if size <= 1 | |
| ans << point | |
| break if ans.size >= spell | |
| wipe(board, table, w, max, board[point], point, point) | |
| (max-w).downto(0){|i| fall(board, w, max, i) } | |
| end | |
| puts ans.size | |
| ans.each do |n| | |
| puts "#{n % w + 1} #{n/w + 1}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment