Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/ruby
- require 'json'
- require 'stringio'
- class Array
- def coordinates(elem)
- each_with_index do |subarray, i|
- j = subarray.index(elem)
- return i,j if j
- end
- end
- end
- def min(*values)
- return values.min
- end
- def countLuck(matrix, k)
- max_x = matrix.length - 1
- max_y = matrix[0].length - 1
- visited = Array.new(max_x+1){Array.new(max_y+1,false)}
- min_wand_waves = 1000
- define_method(:takeStep) do |x,y,w|
- #puts "ts #{x},#{y}"
- wand_waves = w
- visited[x][y]=true
- steps = []
- if matrix[x][y] == "*"
- #puts "ww #{min_wand_waves}, #{wand_waves}"
- min_wand_waves = min(min_wand_waves, wand_waves)
- elsif matrix[x][y] == "." || matrix[x][y] == "M"
- up = [-1,0]
- down = [1,0]
- left = [0,-1]
- right = [0,1]
- updownleftright = [up,down,left,right]
- updownleftright.each do |i|
- next_x = x + i[0]
- next_y = y + i[1]
- next if next_x > max_x || next_y > max_y || matrix[next_x][next_y] == "X" || next_x < 0 || next_y < 0 || visited[next_x][next_y]
- #puts "udlr #{x},#{y},#{next_x},#{next_y}"
- steps.push([next_x,next_y])
- end
- wand_waves += 1 if steps.length > 1
- steps.each do |xy|
- #puts "steps #{xy[0]}, #{xy[1]}, #{wand_waves}"
- takeStep(xy[0],xy[1],wand_waves)
- end
- end
- end
- start_point = matrix.coordinates('M')
- takeStep(start_point[0],start_point[1],0)
- if k == min_wand_waves
- return "Impressed"
- else
- return "Oops!"
- end
- end
- fptr = File.open(ENV['OUTPUT_PATH'], 'w')
- t = gets.to_i
- t.times do |t_itr|
- nm = gets.rstrip.split
- n = nm[0].to_i
- m = nm[1].to_i
- matrix = Array.new(n)
- n.times do |i|
- matrix_item = gets.to_s.rstrip
- matrix[i] = matrix_item
- end
- k = gets.to_i
- result = countLuck matrix, k
- fptr.write result
- fptr.write "\n"
- end
- fptr.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement