class Yukicoder attr_accessor :check_field DY = [-1, 0, 0, 1] DX = [ 0,-1, 1, 0] def initialize @field = [ [ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9,10,11,12], [13,14,15, 0] ] @check_field = Array.new(4){ Array.new(4, false) } @current = [] 4.times do |y| list = gets.chomp.split.map(&:to_i) @current << list if list.index(0) @curY = y @curX = list.index(0) end end loop do check_field[@curY][@curX] = true if check puts "Yes" break end unless move puts "No" break end end end def move 4.times do |i| ny = @curY + DY[i] nx = @curX + DX[i] if is_inside?(ny,nx) && !check_field[ny][nx] && @current[ny][nx] != @field[ny][nx] @current[@curY][@curX], @current[ny][nx] = @current[ny][nx], @current[@curY][@curX] @curY = ny @curX = nx return true end end false end def exist_diff?(y, x) 4.times do |i| ny = y + DY[i] nx = x + DX[i] if is_inside?(ny,nx) && @current[ny][nx] != @field[ny][nx] return true end end false end def check @curY == 3 && @curX == 3 && !exist_diff?(@curY, @curX) end def is_inside?(y, x) 0 <= y && y < 4 && 0 <= x && x < 4 end end Yukicoder.new