@target=STDIN.read.split("\n").map{|l| l.split.map(&:to_i);} cur_y=nil,cur_x=nil (0...4).each{|r| (0...4).each{|c| if @target[r][c]==0 cur_y = r cur_x = c end } } @ma=Array.new(4).map{Array.new(4)} (0..15).each{|i| @ma[i/4][i%4]=i+1 } @ma[3][3]=0 @used=Array.new(16,false) def go(cur_y, cur_x) #puts "cur_y=#{cur_y}, cur_x=#{cur_x}" #puts "ma=#{@ma}" if @ma==@target puts "Yes" exit 0 end dx=[0,1,0,-1]; dy=[1,0,-1,0]; (0...4).each{|i| y2=cur_y+dy[i] x2=cur_x+dx[i] if(y2<0 || y2>3) next end if(x2<0 || x2>3) next end if(@used[@ma[y2][x2]]==true) next end @used[@ma[y2][x2]]=true @ma[cur_y][cur_x],@ma[y2][x2] = @ma[y2][x2],@ma[cur_y][cur_x] go(y2,x2) @ma[cur_y][cur_x],@ma[y2][x2] = @ma[y2][x2],@ma[cur_y][cur_x] @used[@ma[y2][x2]]=false } end go(3,3) puts "No"