Submission #35755332


Source Code Expand

H, W, r_s, c_s = gets.chomp.split.map(&:to_i)

x_walls = Hash.new {|hash, key| hash[key] = []}
y_walls = Hash.new {|hash, key| hash[key] = []}

n = gets.chomp.to_i
n.times do
  r_w, c_w = gets.chomp.split.map(&:to_i)
  x_walls[r_w] << c_w
  y_walls[c_w] << r_w
end

x_walls.each do |_, walls|
  walls.sort!
end

y_walls.each do |_, walls|
  walls.sort!
end

def move(x, y, direction, length, x_wall, y_wall)
  new_x, new_y = x, y

  if direction == "L"
    tmp_y = [1, new_y - length].max
    if x_wall[new_x].length > 0
      # 現在位置(new_y)のすぐ右側にある壁を探す
      next_wall_index = x_wall[new_x].bsearch_index { |pos| pos > new_y }
      if next_wall_index.nil?
        # 現在位置より右の壁がないので、一番右端の壁をチェック
        previous_index = x_wall[new_x].length - 1
        tmp_y = [tmp_y, x_wall[new_x][previous_index] + 1].max
      elsif next_wall_index > 0
        # 進行方向に壁がありそうなら検証
        previous_index = next_wall_index - 1
        tmp_y = [tmp_y, x_wall[new_x][previous_index] + 1].max
      end
      # 現在位置より右の壁が一番最初の壁=進行方向には壁がないので壁チェック不要
    end
    new_y = tmp_y
  elsif direction == "R"
    tmp_y = [new_y + length, W].min
    # 現在位置(new_y)のすぐ右側にある壁を探す
    wall_pos = x_wall[new_x].bsearch { |pos| pos > new_y }
    unless wall_pos.nil?
      # 進行方向に壁があるなら検証
      tmp_y = [tmp_y, wall_pos - 1].min
    end
    new_y = tmp_y
  elsif direction == "U"
    tmp_x = [1, new_x - length].max
    if y_wall[new_y].length > 0
      next_wall_index = y_wall[new_y].bsearch_index { |pos| pos > new_x }
      if next_wall_index.nil?
        previous_index = y_wall[new_y].length - 1
        tmp_x = [tmp_x, y_wall[new_y][previous_index] + 1].max
      elsif next_wall_index > 0
        previous_index = next_wall_index - 1
        tmp_x = [tmp_x, y_wall[new_y][previous_index] + 1].max
      end
    end
    new_x = tmp_x
  else # direction == "D"
    tmp_x = [new_x + length, H].min
    wall_pos = y_wall[new_y].bsearch { |pos| pos > new_x }
    unless wall_pos.nil?
      tmp_x = [tmp_x, wall_pos - 1].min
    end
    new_x = tmp_x
  end

  [new_x, new_y]
end

q = gets.chomp.to_i
x, y = r_s, c_s
q.times do
  direction, length = gets.chomp.split
  length = length.to_i

  x, y = move(x, y, direction, length, x_walls, y_walls)

  puts "#{x} #{y}"
end

Submission Info

Submission Time
Task D - LRUD Instructions
User thatblue
Language Ruby (2.7.1)
Score 400
Code Size 2563 Byte
Status AC
Exec Time 1093 ms
Memory 57264 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 29
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 312 ms 14220 KiB
001.txt AC 411 ms 18588 KiB
002.txt AC 335 ms 14024 KiB
003.txt AC 834 ms 39244 KiB
004.txt AC 750 ms 38816 KiB
005.txt AC 845 ms 38792 KiB
006.txt AC 740 ms 39276 KiB
007.txt AC 375 ms 14152 KiB
008.txt AC 941 ms 56008 KiB
009.txt AC 216 ms 24008 KiB
010.txt AC 458 ms 35024 KiB
011.txt AC 543 ms 45044 KiB
012.txt AC 872 ms 46612 KiB
013.txt AC 1014 ms 57204 KiB
014.txt AC 1093 ms 57264 KiB
015.txt AC 1026 ms 57216 KiB
016.txt AC 1045 ms 57184 KiB
017.txt AC 619 ms 18620 KiB
018.txt AC 595 ms 19048 KiB
019.txt AC 582 ms 19280 KiB
020.txt AC 583 ms 19240 KiB
021.txt AC 583 ms 18788 KiB
022.txt AC 584 ms 18948 KiB
023.txt AC 585 ms 19292 KiB
024.txt AC 608 ms 19252 KiB
025.txt AC 602 ms 19328 KiB
026.txt AC 593 ms 19032 KiB
example0.txt AC 53 ms 14228 KiB
example1.txt AC 57 ms 14232 KiB