input = gets.strip.split(' ').map(&:to_i) @n = input[0] @m = input[1] @c = input[2] @remocon = [] (1..@n * @m).each do |i| @remocon << i end x = (@c - 1) % @m + 1 y = (@c - 1) / @m + 1 #print "#{@c} : #{x},#{y}\n" #x = 1 #y = 3 #p x + (y - 1) * @m def dfs(pos, path) #p pos #p path x = pos[0] y = pos[1] ch = x + (y - 1) * @m [[1, 0], [0, -1], [-1, 0], [0, 1]].each do |dir| dx = dir[0] dy = dir[1] next_x = x + dx next_y = y + dy next if next_x < 1 || next_x > @m || next_y < 1 || next_y > @n next_pos = [next_x, next_y] next_ch = next_x + (next_y - 1) * @m #print "#{pos} -> #{next_pos}\n" #print "#{ch} -> #{next_ch}\n" #next if path.include?(next_pos) next if path.include?(next_ch) #p path #path << next_pos #p path + [next_ch] if (path + [next_ch]).length == @n * @m puts "YES" exit end dfs(next_pos, path + [next_ch]) end end path = [] dfs([x, y], path.clone) puts "NO"