結果

問題 No.331 CodeRunnerでやれ
ユーザー LeonardoneLeonardone
提出日時 2015-12-24 22:59:17
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 3,340 bytes
コンパイル時間 545 ms
コンパイル使用メモリ 11,484 KB
実行使用メモリ 32,296 KB
平均クエリ数 21.82
最終ジャッジ日時 2023-09-23 08:36:25
合計ジャッジ時間 7,104 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 259 ms
30,980 KB
testcase_01 AC 264 ms
31,704 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#! ruby
# yukicoder My Practice
# author: Leonardone @ NEETSDKASU
############################################################
def gs() gets.chomp end
def gi() gets.to_i end
def gf() gets.to_f end
def gss() gs.split end
def gis() gss.map(&:to_i) end
def gfs() gss.map(&:to_f) end
def nmapf(n,f) n.times.map{ __send__ f } end
def ngs(n) nmapf n,:gs end
def ngi(n) nmapf n,:gi end
def ngss(n) nmapf n,:gss end
def ngis(n) nmapf n,:gis end
def arr2d(h,w,v=0) h.times.map{[v] * w} end
def for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end
def nsum(n) n * (n + 1) / 2 end
def vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end
############################################################

=begin

最大でも20x20なんで
うまくやればTLEなんて起こらないはずなのに

壁伝いだと以下に気をつけるべき

ループに捕まると抜け出せない
##########
#.........
#..####..#
#.v#..#..#
#..####..#
#........#
##########

行き止まりも恐い
##########
#.#.......
#.#.######
#.#......#
#.#.######
#......v.#
##########

下手すると上下左右に単機ループがあるかもしれない
##########
#.........
#...#....#
#.#.v.#..#
#........#
#...#....#
#........#
##########


真面目に組むなら
マッピング・不明マスへのルート検索・ナビ
の繰り返しでゴールできるはずだがm
めっちゃ面倒そう


=end


UNKNOWN_CELL = 0
WALL_CELL = 1
SPACE_CELL = 2

f = arr2d(60, 60)
x = 30
y = 30
dx = 1
dy = 0
f[y][x] = SPACE_CELL

def rotr(dx, dy) [-dy, dx] end
def rotl(dx, dy) [dy, -dx] end
def rcell(f, x, y, dx, dy)
    rx, ry = rotr(dx, dy)
    f[y + ry][x + rx]
end
def lcell(f, x, y, dx, dy)
    lx, ly = rotl(dx, dy)
    f[y + ly][x + lx]
end

def mapping(f, x, y, dx, dy, d)
    (1..d).each do |i|
        f[y + dy * i][x + dx * i] = SPACE_CELL
    end
    f[y + dy * d.succ][x + dx * d.succ] = WALL_CELL
    f
end

root = []

def seachRoot(f, x, y, dx, dy)
    m = arr2d(60, 60, false)
    w = [[x, y, dx, dy, []]]
    while !w.empty?
        v = []
        w.each do |x, y, dx, dy, r|
            next if m[y][x]
            next if !(9..51).include?(x)
            next if !(9..51).include?(y)
            m[y][x] = true
            case f[y][x]
            when UNKNOWN_CELL
                return r.size > 1 ? r[0..-2] : r[0..-1]
            when SPACE_CELL
                v << [x + dx, y + dy, dx, dy, r + ["F"]]
                nx, ny = rotr(dx, dy)
                v << [x + nx, y + ny, nx, ny, r + ["R", "F"]]
                nx, ny = rotl(dx, dy)
                v << [x + nx, y + ny, nx, ny, r + ["L", "F"]]
                nx, ny = rotl(nx, ny)
                v << [x + nx, y + ny, nx, ny, r + ["L", "L", "F"]]
            end
        end
        w = v
    end
    return []
end

loop {
    s = STDIN.gets
    break if s[0] == "M"
    if s.size > 2
        STDOUT.puts "F"
    else
        d = s.to_i
        f = mapping(f, x, y, dx, dy, d)
        if root.empty?
            root = seachRoot(f, x, y, dx, dy)
        end
        nxt = root.pop
        puts nxt
        case nxt
        when "F"
            x += dx
            y += dy
        when "R"
            dx, dy = rotr(dx, dy)
        when "L"
            dx, dy = rotl(dx, dy)
        when "B"
            x -= dx
            y -= dy
        end
    end
    STDOUT.flush
}
0