#1498875

Solution for 0121: Seven Puzzle by iwamot

Source Code Status Test Cases
    Policy: public     Reviewed: 79    
00.15 sec    10848 KB    23 lines     371 bytes    2015-08-31 23:43
s = '01234567'
memo = {s => 0}
q = [s]

mv = [-4, 4, 1, -1]
while p = q.shift
  i = p.index('0')
  mv.each do |m|
    j = i + m
    next if j < 0 || j > 7 || (i == 3 && j == 4) || (i == 4 && j == 3)
    pn = p.dup
    pn[i], pn[j] = pn[j], pn[i]
    next if memo[pn]
    memo[pn] = memo[p] + 1
    q.push pn
  end
end

while s = gets
  puts memo[s.chomp.delete(' ')]
end


Compile Error Logs:
You are not authorized to see the message.

Status
Judge: 1/1 Ruby CPU: 00.15 sec Memory: 10848 KB Length: 371 B 2015-08-31 23:43 2015-08-31 23:43
Results for testcases
Case # Verdict CPU Time Memory In Out Case Name
Case #1: : Accepted 00.15 sec 10848 KB
< prev | / | next >  
 
Judge Input #  ( | ) Judge Output #  ( | )


Comments
 
 Under Construction.
 
Categories
 
 
Free Tags