Problem:
Problem F: Mysterious Maze

A robot in a two-dimensional maze again. The maze has an entrance and an exit this time, though.

Just as in the previous problem, the maze is made up of H × W grid cells, its upper side faces north, and each cell is either empty or wall. Unlike the previous, on the other hand, one of the empty cells is connected to the entrance and another to the exit.

The robot is rather complex - there is some control, but not full. It is associated with a controller that has two buttons, namely forward and turn. The forward button moves the robot forward to the next cell, if possible. The robot can not move into a wall nor outside the maze. The turn button turns the robot as programmed. Here the program is a finite sequence of N commands, each of which is either 'L' (indicating a left turn) or 'R' (a right turn). The first turn follows the first command; the second turn follows the second command; similar for the following turns. The turn button stops working once the commands are exhausted; the forward button still works in such a case though. The robot always turns by 90 degrees at once.

The robot is initially put on the entrance cell, directed to the north. Your mission is to determine whether it can reach the exit cell if controlled properly.

Input

The input is a sequence of datasets. Each dataset is formatted as follows.

H W N

s1 ... sN

c1,1c1,2...c1,W

...

cH,1cH,2...cH,W

The first line of a dataset contains three integers H, W and N (1 ≤ H, W ≤ 1,000, 1 ≤ N ≤ 1,000,000).

The second line contains a program of N commands.

Each of the following H lines contains exactly W characters. Each of these characters represents a cell of the maze. "." indicates empty, "#" indicates a wall, "S" indicates an entrance, and "G" indicates an exit. There is exactly one entrance cell and one exit cell.

The end of input is indicated by a line with three zeros.

Output

For each dataset, output whether the robot can reach the exit in a line: "Yes" if it can or "No" otherwise (without quotes).

Sample Input

2 2 1
L
G.
#S
2 2 2
RR
G.
.S
3 3 6
LLLLLL
G#.
...
.#S
0 0 0

Output for the Sample Input

Yes
No
Yes

Solution in Ruby (judge verdict: Runtime Error)

class Heap
	require 'forwardable'
	extend Forwardable

	def initialize
		@array = []
		@index = {}
	end

	def add(obj, key)
		@array << [obj, key]
		@index[obj] = @array.size - 1
		lift_up(@array.size - 1)
	end

	def swap(i, j)
		u = @array[i]
		v = @array[j]
		@index[u[0]], @index[v[0]] = j, i
		@array[i], @array[j] = v, u
	end

	def lift_up(i)
		return if i == 0
		j = (i - 1) / 2
		if (@array[i][1] <=> @array[j][1]) < 0
			swap(i, j)
			lift_up(j)
		end
	end

	def del_min
		raise "heap is empty!" if empty?
		min = @array[0]
		if size > 1
			swap(0, @array.size-1)
			@array.pop
			lift_down(0)
		else
			@array.pop
		end
		@index.delete(min[0])
		min
	end

	def inc_key(obj, diff)
		i = @index[obj]
		@array[i][1] += diff
		lift_down(i)
	end

	def dec_key(obj, diff)
		i = @index[obj]
		@array[i][1] -= diff
		lift_up(i)
	end

	def update_key(obj, new_key)
		i = @index[obj]
		return unless i
		return if (new_key <=> @array[i][1]) >= 0
		@array[i][1] = new_key
		lift_up(i)
	end

	def min_obj
		@array[0][0]
	end

	def min_key
		@array[0][1]
	end

	def lift_down(i)
		j = 2 * i + 1
		k = j + 1
		if k >= size
			return if j >= size
			if (@array[i][1] <=> @array[j][1]) > 0
				swap(i, j)
				lift_down(j)
			end
		else
			l = ((@array[j][1] <=> @array[k][1]) < 0 ? j : k)
			if (@array[i][1] <=> @array[l][1]) > 0
				swap(i, l)
				lift_down(l)
			end
		end
	end

	def include?(obj)
		@index.include?(obj)
	end

	def_delegators :@array, :empty?, :size
end

dij = [
	[-1, 0],
	[0, 1],
	[1, 0],
	[0, -1],
]

loop do
	h, w, n = gets.split.map(&:to_i)
	break if h == 0
	commands = gets.chomp
	a = (0..h-1).map { gets.chomp.split("") }

	si, sj, gi, gj = nil
	(0..h-1).each do |i|
		(0..w-1).each do |j|
			if a[i][j] == ?S
				si, sj = i, j
			end
			if a[i][j] == ?G
				gi, gj = i, j
			end
		end
	end

	pq = Heap.new

	i, j = si, sj
	loop do
		pq.add([i, j, 0], 0)
		i -= 1
		break if i < 0 || a[i][j] == ?#
	end

	min_command = {}
	loop do
		u, c = pq.del_min
		min_command[u] = c
		i, j, dir = u
		if i == gi && j == gj
			break
		end

		rest_dir = (0..3).to_a - [dir]
		(c..commands.size-1).each do |k|
			dir += 1 if commands[k] == ?R
			dir -= 1 if commands[k] == ?L
			dir %= 4
			next if ! rest_dir.include?(dir)
			rest_dir.delete(dir)

			di, dj = dij[dir]
			step = 1
			loop do
				new_i, new_j = i + step * di, j + step * dj
				break if new_i < 0 || new_i >= a.size
				break if new_j < 0 || new_j >= a[0].size
				break if a[new_i][new_j] == ?#
				v = [new_i, new_j, k+1]

				next if min_command[v]
				if pq.include?(v)
					pq.update_key(v, k+1)
				else
					pq.add(v, k+1)
				end
				step += 1
			end
			
			break if rest_dir.empty?
		end
		break if pq.empty?
	end

	if (0..3).any? {|d| min_command[[gi, gj, d]]}
		puts 'Yes'
	else
		puts 'No'
	end
end