結果

問題 No.440 2次元チワワ問題
ユーザー tana_twtr
提出日時 2016-10-29 14:16:18
言語 Ruby
(3.4.1)
結果
RE  
実行時間 -
コード長 1,092 bytes
コンパイル時間 42 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 29,824 KB
最終ジャッジ日時 2024-11-24 17:50:36
合計ジャッジ時間 15,380 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 24 TLE * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:3: warning: assigned but unused variable - w
Syntax OK

ソースコード

diff #

def main
    lines = STDIN.readlines
    h, w = lines[0].split.map {|x| x.to_i}
    s = []
    h.times {|i| s << lines[i + 1].chomp}
    q = lines[h + 1].chomp.to_i
    q_array = []
    q.times {|i| q_array << lines[h + i + 2].split.map {|x| x.to_i - 1}.to_a}

    q_array.each do |a, b, c, d|
        
        total = 0
        
        a.upto(c) do |y|
            b.upto(d) do |x|
                if s[y][x] == "c"
                    total += search_w s, y, x, a, b, c, d
                end
            end
        end
        
        puts total

    end
end

def search_w(s, y, x, a, b, c, d)
   
   total = 0
   total += search_w_dir y, x, -1, 0, a, b, c, d
   total += search_w_dir y, x, 1, 0, a, b, c, d
   total += search_w_dir y, x, 0, -1, a, b, c, d
   total += search_w_dir y, x, 0, 1, a, b, c, d
   total

end

def search_w_dir(s, y, x, j, i, a, b, c, d)
    
    w_count = 0

    while a <= y && y <= c && b <= x && x <= d
        if s[y][x] == "w"
            w_count += 1
        end

        y += j
        x += i
    end

    w_count * (w_count - 1) / 2
    
end

main()

0