結果

問題 No.608 God's Maze
ユーザー fine
提出日時 2017-12-08 01:36:44
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 1,583 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-11-29 05:35:32
合計ジャッジ時間 11,662 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 54 WA * 11
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i - 1
s = gets.chomp
m = s.size
l = -1
n.times do |i|
    if s[i] == ?#
        l = i
        break
    end
end
r = -1
(m-1).downto(n+1) do |i|
    if s[i] == ?#
        r = i
        break
    end   
end

ans = 1000000000
if l != -1
    ans1 = 0
    s1 = ?. * m
    l.upto(n-1) do |i|
        s1[i] = ?#
        ans1 += 1
    end
    cnt = 0
    m.times do |i|
        if s[i] != s1[i]
           cnt += 1
        end
    end
    (l + 1).upto(m - 1) do |i|
        if cnt == 0
            break
        end
        s1[i] = (s1[i] == ?. ? ?# : ?.)
        ans1 += 1
        if s1[i] != s[i]
            ans1 += 2
            s1[i] = s[i]
            s1[i + 1] = (s1[i + 1] == ?. ? ?# : ?.)
            if s1[i + 1] == s[i + 1]
                cnt -= 1
            else
                cnt += 1
            end
        else
            cnt -= 1
        end
    end
    ans = [ans, ans1].min
end

if r != -1
    ans2 = 0
    s2 = ?. * m
    r.downto(n+1) do |i|
        s2[i] = ?#
        ans2 += 1
    end
    cnt = 0
    m.times do |i|
        if s[i] != s2[i]
           cnt += 1
        end
    end
    (r - 1).downto(0) do |i|
        if cnt == 0
            break
        end
        s2[i] = (s2[i] == ?. ? ?# : ?.)
        ans2 += 1
        if s2[i] != s[i]
            ans2 += 2
            s2[i] = s[i]
            s2[i - 1] = (s2[i - 1] == ?. ? ?# : ?.)
            if s2[i - 1] == s[i - 1]
                cnt -= 1
            else
                cnt += 1
            end
        else
            cnt -= 1
        end
    end
    ans = [ans, ans2].min
end
p ans
0