Problem:


Problem D:バトルタウン

  

    あなたは友人たちとゲームを開発することにした．
    ゲームのタイトルは「バトルタウン」．
    戦車による市街戦をテーマにしたゲームだ．
    ゲーム開発の第一歩として，次のようなプロトタイプを開発することにした．
  

  

    このプロトタイプでは，登場する戦車はプレイヤーの戦車のみであり，
    敵の戦車は登場しない．
    プレイヤーの戦車はプレイヤーの入力に従ってマップ上で様々な動作をする．
    以下に，戦車の動作に関する仕様を述べる．
  

  

    マップの構成要素は表1の通りである．
    戦車が移動できるのはマップ内の平地の上だけである．
  

  
  

    表1: マップの構成要素
    
      
文字要素

    
    
      
.平地

      
*レンガの壁

      
#鉄の壁

      
-水

      
^戦車（上向き）

      
v戦車（下向き）

      
<戦車（左向き）

      
>戦車（右向き）

    
  

  

  

    プレイヤーの入力は文字の列で与えられる．
    各文字に対応する動作は表2の通りである．
  

  
  

    表2: プレイヤーの入力に対する動作
    
      
文字動作

    
    
      

	U
	Up: 戦車を上向きに方向転換し，さらに一つ上のマスが平地ならばそのマスに移動する
      

      

	D
	Down: 戦車を下向きに方向転換し，さらに一つ下のマスが平地ならばそのマスに移動する
      

      

	L
	Left: 戦車を左向きに方向転換し，さらに一つ左のマスが平地ならばそのマスに移動する
      

      

	R
	Right: 戦車を右向きに方向転換し，さらに一つ右のマスが平地ならばそのマスに移動する
      

      

	S
	Shoot: 戦車が現在向いている方向に砲弾を発射する
      

    
  

  

  

    砲弾はレンガの壁あるいは鉄の壁にぶつかるか，マップの外に出るまで直進する．
    レンガの壁にぶつかった場合は，砲弾は消滅し，レンガの壁は平地に変化する．
    鉄の壁にぶつかった場合は，砲弾は消滅するが，鉄の壁は変化しない．
    マップの外に出たときは砲弾はそのまま消滅する．
  

  

    あなたの仕事は，初期マップとプレイヤーの入力操作列が与えられたときに，
    最終的なマップの状態を出力するプログラムを作成することである．
  

  
Input

  

    入力の1行目にはデータセット数を表す数 T
    (0 < T ≤ 100) が与えられる．
    この行に引き続き T 個のデータセットが与えられる．
  

  

    各データセットの1行目にはマップの高さと幅を表す整数
    H と W が
    1つの空白文字で区切られて与えられる．
    これらの整数は 2 ≤ H ≤ 20,
    2 ≤ W ≤ 20 を満たす．
    続く H 行はマップの初期状態を表す．
    各行は長さ W の文字列である．
    マップに含まれる文字はすべて表1で定義されたものであり，
    全体でちょうど1つの戦車を含む．
    マップに続く行には入力操作列の長さ N
    (0 < N ≤ 100) が与えられ，
    次の行には入力操作列を表す長さ N の文字列が与えられる．
    入力操作列は表2で定義された文字のみからなる文字列である．
  

  
Output

  

    各データセットに対し，すべての入力操作を終えた後のマップを，
    入力マップと同じ形式で出力せよ．
    データセットの間には1行の空行を出力せよ．
    最後のデータセットの後に余計な空行を出力しないよう注意すること．
  

  
Sample Input

  

4
4 6
*.*..*
*.....
..-...
^.*#..
10
SRSSRRUSSR
2 2
<.
..
12
DDSRRSUUSLLS
3 5
>-#**
.-*#*
.-**#
15
SSSDRSSSDRSSSUU
5 5
v****
*****
*****
*****
*****
44
SSSSDDRSDRSRDSULUURSSSSRRRRDSSSSDDLSDLSDLSSD

  
Output for the Sample Input

  

*....*
......
..-...
..>#..

<.
..

^-#**
.-.#*
.-..#

.....
.***.
..*..
..*..
....v



Sample input:
4
4 6
*.*..*
*.....
..-...
^.*#..
10
SRSSRRUSSR
2 2
<.
..
12
DDSRRSUUSLLS
3 5
>-#**
.-*#*
.-**#
15
SSSDRSSSDRSSSUU
5 5
v****
*****
*****
*****
*****
44
SSSSDDRSDRSRDSULUURSSSSRRRRDSSSSDDLSDLSDLSSD


Sample output:
*....*
......
..-...
..>#..

<.
..

^-#**
.-.#*
.-..#

.....
.***.
..*..
..*..
....v

Submission status: Accepted

Solution code:
t = int(input())
for i in range(t):
  h, w = map(int, input().split())
  mp = [list("#" + input() + "#") for _ in range(h)]
  mp.insert(0, ["#"] * (w + 2))
  mp.append(["#"] * (w + 2))
  for y in range(h + 2):
    for x in range(w + 2):
      if mp[y][x] == ">":
        sx, sy, sd = x, y, 0
      if mp[y][x] == "^":
        sx, sy, sd = x, y, 1
      if mp[y][x] == "<":
        sx, sy, sd = x, y, 2
      if mp[y][x] == "v":
        sx, sy, sd = x, y, 3
  mp[sy][sx] = "."
  n = int(input())
  s = input()
  vec = ((1, 0), (0, -1), (-1, 0), (0, 1))
  dic = {"R":0, "U":1, "L":2, "D":3}
  tank = {0:">", 1:"^", 2:"<", 3:"v"}
  for c in s:
    if c == "S":
      dx, dy = vec[sd]
      nx, ny = sx, sy
      while mp[ny][nx] not in ("#", "*"):
        nx += dx
        ny += dy
      if mp[ny][nx] == "*":
        mp[ny][nx] = "."

    else:
      sd = dic[c]
      dx, dy = vec[sd]
      if mp[sy + dy][sx + dx] == ".":
        sx += dx
        sy += dy

  mp[sy][sx] = tank[sd]
  for j in range(1, h + 1):
    print("".join(mp[j][1:-1]))
  if i != t - 1:
    print()

Submission status: Accepted

Solution code:
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
tanks = map(ord, "<^>v")
flat = ord(".")
brick = ord("*")
iron = ord("#")
water = ord("-")

case = input()
for t in xrange(case):
    h, w = map(int, raw_input().split())
    m = [map(ord, raw_input()) for i in xrange(h)]
    n = input()
    s = raw_input()
    x, y = 0, 0
    d = 0
    for i in xrange(h):
        for j in xrange(w):
            if m[i][j] in tanks:
                x = j
                y = i
                d = tanks.index(m[i][j])
                m[i][j] = flat
    for comm in s:
        if comm=="U":
            d = 1
            if y>0 and m[y-1][x]==flat:
                y -= 1
        elif comm=="D":
            d = 3
            if y<h-1 and m[y+1][x]==flat:
                y += 1
        elif comm=="L":
            d = 0
            if x>0 and m[y][x-1]==flat:
                x -= 1
        elif comm=="R":
            d = 2
            if x<w-1 and m[y][x+1]==flat:
                x += 1
        else:
            # "S"
            nx = x + dx[d]
            ny = y + dy[d]
            while 0<=nx<=w-1 and 0<=ny<=h-1:
                obj = m[ny][nx]
                if obj==brick:
                    m[ny][nx] = flat
                    break
                elif obj==iron:
                    break
                nx += dx[d]
                ny += dy[d]
    m[y][x] = tanks[d]
    for i in xrange(h):
        print "".join(map(chr, m[i]))
    if t<case-1:
        print

Submission status: Accepted

Solution code:
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
tanks = "<^>v"
flat = "."
brick = "*"
iron = "#"
water = "-"

case = input()
for t in xrange(case):
    h, w = map(int, raw_input().split())
    m = [list(raw_input()) for i in xrange(h)]
    n = input()
    s = raw_input()
    x, y = 0, 0
    d = 0
    for i in xrange(h):
        for j in xrange(w):
            if m[i][j] in tanks:
                x = j
                y = i
                d = tanks.index(m[i][j])
                m[i][j] = flat
    for comm in s:
        if comm=="U":
            d = 1
            if y>0 and m[y-1][x]==flat:
                y -= 1
        elif comm=="D":
            d = 3
            if y<h-1 and m[y+1][x]==flat:
                y += 1
        elif comm=="L":
            d = 0
            if x>0 and m[y][x-1]==flat:
                x -= 1
        elif comm=="R":
            d = 2
            if x<w-1 and m[y][x+1]==flat:
                x += 1
        else:
            # "S"
            nx = x + dx[d]
            ny = y + dy[d]
            while 0<=nx<=w-1 and 0<=ny<=h-1:
                obj = m[ny][nx]
                if obj==brick:
                    m[ny][nx] = flat
                    break
                elif obj==iron:
                    break
                nx += dx[d]
                ny += dy[d]
    m[y][x] = tanks[d]
    for i in xrange(h):
        print "".join(m[i])
    if t<case-1:
        print

Submission status: Accepted

Solution code:
def move(act,y,x,direction):#戦車の動き
    #print("x,y",y,x)
    #print("M,H",M,H)
    if x<0 or x == M or y<0 or y == H:
        return s_map
    if act == 'U':#Up
        if y == 0:
            s_map[y][x] = '^'
        else:
            if s_map[y-1][x] == '.':#下が平地なら
                s_map[y][x],s_map[y-1][x] = '.','^'#現在地と下を入れ替える
            else:
                s_map[y][x] = '^'
    elif act == 'D':#Down
        if y == H-1:
            s_map[y][x] = 'v'
        else:
            if s_map[y+1][x] == '.':#下が平地なら
                s_map[y][x],s_map[y+1][x] = '.','v'#現在地と下を入れ替える
            else:
                s_map[y][x] = 'v'

    elif act == 'L':#Left
        if x == 0:
            s_map[y][x] = '<'
        else:
            if s_map[y][x-1] == '.':#左が平地なら
                s_map[y][x],s_map[y][x-1] = '.','<'#現在地と左を入れ替える
            else:
                s_map[y][x] = '<'

    elif act == 'R':#Right
        if x == M-1:
            s_map[y][x] = '>'
        else:
            if s_map[y][x+1] == '.':#右が平地なら
                s_map[y][x],s_map[y][x+1] = '.','>'#現在地と右を入れ替える
            else:
                s_map[y][x] = '>'

    else:#Shoot
        now_dir = s_map[y][x]
        ma = shoot(y,x,now_dir)
        return ma
    
def shoot(y,x,now_dir):
    if x<0 or y<0 or x==M or y==H:
        return s_map
    if now_dir == '>':#戦車が右を向いていたら
        while True:
            #再帰かな x+=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y,x+1,now_dir)
            break

    elif now_dir == '<':#戦車が左を向いていたら
        while True:
            #再帰かな x-=1　左の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y,x-1,now_dir)
            break
       
    elif now_dir == '^':
        while True:
            #再帰かな y-=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y-1,x,now_dir)
            break

    elif now_dir == 'v':
        while True:
            #再帰かな y+=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y+1,x,now_dir)
            break

def search_index(s_map):#現在地のインデックスと方向を確認
    direction = ['^','v','>','<']
    for i in range(len(direction)):
        for j in range(len(s_map)):
            for k in range(len(s_map[0])):
                if direction[i] in s_map[j][k]:
                    x,y,d = j,k,direction[i]

    return x,y,d


if __name__== '__main__':
    T = int(input())#全体回数
    all_map=[]
    for i in range(T):
        s_map = []
        H,M = map(int,input().split())#マップの縦幅横幅
        for j in range(H):#マップ作成
            s_map.append([i for i in input()])
        N = int(input())#何回処理を行うか
        action = input()#アクション文字列
        for k in action:
            x,y,direction = search_index(s_map)
            #print("Now state (x,y)",y,x)
            #print(direction)
            #print("action = ",k)
            m  = move(k,x,y,direction)
        all_map.append(s_map)
    for j in range(len(all_map)):
        for i in range(len(all_map[j])):
            print(''.join(all_map[j][i]))
        if j != len(all_map)-1:
            print("")

Submission status: Accepted

Solution code:
def move(y, x):
    p[2], p[3] = y, x
    if a[p[0] + y][p[1] + x] == ".":p[0] += y;p[1] += x

def shoot():
    th = p[0] + p[2]
    tw = p[1] + p[3]
    while 1:
        if a[th][tw] == "*":a[th][tw] = ".";break
        elif a[th][tw] == "#":break
        else: th += p[2]; tw += p[3]

for u in range(int(input())):
    if u > 0:print()
    h, w = map(int, input().split())
    a = [["#"] * (w + 2)] + [["#"] + list(input()) + ["#"] for _ in range(h)] + [["#"] * (w + 2)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if a[i][j] == ">":p = [i, j, 0, 1];a[i][j] = ".";break
            elif a[i][j] == "^":p = [i, j, -1, 0];a[i][j] = ".";break
            elif a[i][j] == "<":p = [i, j, 0, -1];a[i][j] = ".";break
            elif a[i][j] == "v":p = [i, j, 1, 0];a[i][j] = ".";break
    n, c = int(input()), input()
    for i in range(n):
        if c[i] == "U":move(-1, 0)
        elif c[i] == "R":move(0, 1)
        elif c[i] == "L":move(0, -1)
        elif c[i] == "D":move(1, 0)
        else:shoot()
    if (p[2], p[3]) == (0, 1):a[p[0]][p[1]] = ">"
    elif (p[2], p[3]) == (-1, 0):a[p[0]][p[1]] = "^"
    elif (p[2], p[3]) == (0, -1):a[p[0]][p[1]] = "<"
    else:a[p[0]][p[1]] = "v"
    for i in a[1:-1]:print("".join(i[1:-1]))

Submission status: Accepted

Solution code:
for _ in range(input()):
	if _ > 0: print
	H,W = map(int,raw_input().split())
	A = [list(raw_input()) for i in range(H)]
	for h in range(H):
		for w in range(W):
			if A[h][w] in "^<v>":
				x,y = w,h
				d = A[h][w]
				A[h][w] = "."
	input()
	cmd = raw_input()
	for c in cmd:
		if   c == "S":
			if d == "^":
				for h in range(y-1,-1,-1):
					if A[h][x] == "*":
						A[h][x] = "."
						break
					if A[h][x] == "#":
						break
			if d == "<":
				for w in range(x-1,-1,-1):
					if A[y][w] == "*":
						A[y][w] = "."
						break
					if A[y][w] == "#":
						break
			if d == "v":
				for h in range(y+1,H):
					if A[h][x] == "*":
						A[h][x] = "."
						break
					if A[h][x] == "#":
						break
			if d == ">":
				for w in range(x+1,W):
					if A[y][w] == "*":
						A[y][w] = "."
						break
					if A[y][w] == "#":
						break
		elif c == "U":
			d = "^"
			if y-1 >= 0 and A[y-1][x] == ".":
				y = y-1
		elif c == "L":
			d = "<"
			if x-1 >= 0 and A[y][x-1] == ".":
				x = x-1
		elif c == "D":
			d = "v"
			if y+1 <  H and A[y+1][x] == ".":
				y = y+1
		elif c == "R":
			d = ">"
			if x+1 <  W and A[y][x+1] == ".":
				x = x+1
	A[y][x] = d
	for h in range(H):
		print "".join(A[h])

Submission status: Accepted

Solution code:
def check(x,y):
	if A[y][x] == "*":
		A[y][x] = "."
		return True
	if A[y][x] == "#":
		return True
	return False

for _ in range(input()):
	if _ > 0: print
	H,W = map(int,raw_input().split())
	A = [list(raw_input()) for i in range(H)]
	for h in range(H):
		for w in range(W):
			if A[h][w] in "^<v>":
				x,y = w,h
				d = A[h][w]
				A[h][w] = "."
	input()
	cmd = raw_input()
	for c in cmd:
		if   c == "S":
			if d == "^":
				for h in range(y-1,-1,-1):
					if check(x,h): break
			if d == "<":
				for w in range(x-1,-1,-1):
					if check(w,y): break
			if d == "v":
				for h in range(y+1,H):
					if check(x,h): break
			if d == ">":
				for w in range(x+1,W):
					if check(w,y): break
		elif c == "U":
			d = "^"
			if y-1 >= 0 and A[y-1][x] == ".": y = y-1
		elif c == "L":
			d = "<"
			if x-1 >= 0 and A[y][x-1] == ".": x = x-1
		elif c == "D":
			d = "v"
			if y+1 <  H and A[y+1][x] == ".": y = y+1
		elif c == "R":
			d = ">"
			if x+1 <  W and A[y][x+1] == ".": x = x+1
	A[y][x] = d
	for h in range(H):
		print "".join(A[h])

Submission status: Accepted

Solution code:
T = int(input())
for i in range(T) :
    H, W = map(int, input().split())
    Map = []
    for h in range(H) :
        tmp = list(input())
        Map.append(tmp)
        if "^" in tmp :
            Me_h = h
            Me_w = tmp.index("^")
        if ">" in tmp :
            Me_h = h
            Me_w = tmp.index(">")
        if "v" in tmp :
            Me_h = h
            Me_w = tmp.index("v")
        if "<" in tmp :
            Me_h = h
            Me_w = tmp.index("<")
    N = int(input())
    Meirei = list(input())
    
    for m in range(N) :
        if Meirei[m] == "U" :
            if Me_h > 0 and Map[Me_h - 1][Me_w] == "." :
                Map[Me_h - 1][Me_w] = "^"
                Map[Me_h][Me_w] = "."
                Me_h -= 1
            else :
                Map[Me_h][Me_w] = "^"
        elif Meirei[m] == "D" :
            if Me_h < H-1 and Map[Me_h + 1][Me_w] == "." :
                Map[Me_h + 1][Me_w] = "v"
                Map[Me_h][Me_w] = "."
                Me_h += 1
            else :
                Map[Me_h][Me_w] = "v"
        elif Meirei[m] == "L" :
            if Me_w > 0 and Map[Me_h][Me_w - 1] == "." :
                Map[Me_h][Me_w - 1] = "<"
                Map[Me_h][Me_w] = "."
                Me_w -= 1
            else :
                Map[Me_h][Me_w] = "<"
        elif Meirei[m] == "R" :
            if Me_w < W-1 and Map[Me_h][Me_w + 1] == "." :
                Map[Me_h][Me_w + 1] = ">"
                Map[Me_h][Me_w] = "."
                Me_w += 1
            else :
                Map[Me_h][Me_w] = ">"
        else :
            if Map[Me_h][Me_w] == "^" :
                h = Me_h
                w = Me_w
                while Map[h][w] != "*" and Map[h][w] != "#" and 0 < h :
                    h -= 1
                if Map[h][w] == "*" :
                    Map[h][w] = "."
            elif Map[Me_h][Me_w] == "v" :
                h = Me_h
                w = Me_w
                while Map[h][w] != "*" and Map[h][w] != "#" and h < H -1 :
                    h += 1
                if Map[h][w] == "*" :
                    Map[h][w] = "."
            elif Map[Me_h][Me_w] == ">" :
                h = Me_h
                w = Me_w
                while Map[h][w] != "*" and Map[h][w] != "#" and w < W -1 :
                    w += 1
                if Map[h][w] == "*" :
                    Map[h][w] = "."
            elif Map[Me_h][Me_w] == "<" :
                h = Me_h
                w = Me_w
                while Map[h][w] != "*" and Map[h][w] != "#" and w > 0 :
                    w -= 1
                if Map[h][w] == "*" :
                    Map[h][w] = "."
                    
    for h in range(H) :
        for w in range(W) :
            print(Map[h][w], sep="", end="")
        print()
    if i != T -1 :
        print()

Submission status: WA: Presentation Error

Solution code:
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
tanks = map(ord, "<^>v")
flat = ord(".")
brick = ord("*")
iron = ord("#")
water = ord("-")

for t in xrange(input()):
    h, w = map(int, raw_input().split())
    m = [map(ord, raw_input()) for i in xrange(h)]
    n = input()
    s = raw_input()
    x, y = 0, 0
    d = 0
    for i in xrange(h):
        for j in xrange(w):
            if m[i][j] in tanks:
                x = j
                y = i
                d = tanks.index(m[i][j])
                m[i][j] = flat
    for comm in s:
        if comm=="U":
            d = 1
            if y>0 and m[y-1][x]==flat:
                y -= 1
        elif comm=="D":
            d = 3
            if y<h-1 and m[y+1][x]==flat:
                y += 1
        elif comm=="L":
            d = 0
            if x>0 and m[y][x-1]==flat:
                x -= 1
        elif comm=="R":
            d = 2
            if x<w-1 and m[y][x+1]==flat:
                x += 1
        else:
            # "S"
            nx = x + dx[d]
            ny = y + dy[d]
            while 0<=nx<=w-1 and 0<=ny<=h-1:
                obj = m[ny][nx]
                if obj==brick:
                    m[ny][nx] = flat
                    break
                elif obj==iron:
                    break
                nx += dx[d]
                ny += dy[d]
    m[y][x] = tanks[d]
    for i in xrange(h):
        print "".join(map(chr, m[i]))

Submission status: WA: Presentation Error

Solution code:
def move(act,y,x,direction):#戦車の動き
    #print("x,y",y,x)
    #print("M,H",M,H)
    if x<0 or x == M or y<0 or y == H:
        return s_map
    if act == 'U':#Up
        if y == 0:
            s_map[y][x] = '^'
        else:
            if s_map[y-1][x] == '.':#下が平地なら
                s_map[y][x],s_map[y-1][x] = '.','^'#現在地と下を入れ替える
            else:
                s_map[y][x] = '^'
    elif act == 'D':#Down
        if y == H-1:
            s_map[y][x] = 'v'
        else:
            if s_map[y+1][x] == '.':#下が平地なら
                s_map[y][x],s_map[y+1][x] = '.','v'#現在地と下を入れ替える
            else:
                s_map[y][x] = 'v'

    elif act == 'L':#Left
        if x == 0:
            s_map[y][x] = '<'
        else:
            if s_map[y][x-1] == '.':#左が平地なら
                s_map[y][x],s_map[y][x-1] = '.','<'#現在地と左を入れ替える
            else:
                s_map[y][x] = '<'

    elif act == 'R':#Right
        if x == M-1:
            s_map[y][x] = '>'
        else:
            if s_map[y][x+1] == '.':#右が平地なら
                s_map[y][x],s_map[y][x+1] = '.','>'#現在地と右を入れ替える
            else:
                s_map[y][x] = '>'

    else:#Shoot
        now_dir = s_map[y][x]
        ma = shoot(y,x,now_dir)
        return ma
    
def shoot(y,x,now_dir):
    if x<0 or y<0 or x==M or y==H:
        return s_map
    if now_dir == '>':#戦車が右を向いていたら
        while True:
            #再帰かな x+=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y,x+1,now_dir)
            break

    elif now_dir == '<':#戦車が左を向いていたら
        while True:
            #再帰かな x-=1　左の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y,x-1,now_dir)
            break
       
    elif now_dir == '^':
        while True:
            #再帰かな y-=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y-1,x,now_dir)
            break

    elif now_dir == 'v':
        while True:
            #再帰かな y+=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y+1,x,now_dir)
            break

def search_index(s_map):#現在地のインデックスと方向を確認
    direction = ['^','v','>','<']
    for i in range(len(direction)):
        for j in range(len(s_map)):
            for k in range(len(s_map[0])):
                if direction[i] in s_map[j][k]:
                    x,y,d = j,k,direction[i]

    return x,y,d


if __name__== '__main__':
    T = int(input())#全体回数
    all_map=[]
    for i in range(T):
        s_map = []
        H,M = map(int,input().split())#マップの縦幅横幅
        for j in range(H):#マップ作成
            s_map.append([i for i in input()])
        N = int(input())#何回処理を行うか
        action = input()#アクション文字列
        for k in action:
            x,y,direction = search_index(s_map)
            #print("Now state (x,y)",y,x)
            #print(direction)
            #print("action = ",k)
            m  = move(k,x,y,direction)
        all_map.append(s_map)
    for j in range(len(all_map)):
        for i in range(len(all_map[j])):
            print(''.join(all_map[j][i]))
        if j != len(all_map)-1:
            print("\n")

Submission status: WA: Presentation Error

Solution code:
def move(act,y,x,direction):#戦車の動き
    #print("x,y",y,x)
    #print("M,H",M,H)
    if x<0 or x == M or y<0 or y == H:
        return s_map
    if act == 'U':#Up
        if y == 0:
            s_map[y][x] = '^'
        else:
            if s_map[y-1][x] == '.':#下が平地なら
                s_map[y][x],s_map[y-1][x] = '.','^'#現在地と下を入れ替える
            else:
                s_map[y][x] = '^'
    elif act == 'D':#Down
        if y == H-1:
            s_map[y][x] = 'v'
        else:
            if s_map[y+1][x] == '.':#下が平地なら
                s_map[y][x],s_map[y+1][x] = '.','v'#現在地と下を入れ替える
            else:
                s_map[y][x] = 'v'

    elif act == 'L':#Left
        if x == 0:
            s_map[y][x] = '<'
        else:
            if s_map[y][x-1] == '.':#左が平地なら
                s_map[y][x],s_map[y][x-1] = '.','<'#現在地と左を入れ替える
            else:
                s_map[y][x] = '<'

    elif act == 'R':#Right
        if x == M-1:
            s_map[y][x] = '>'
        else:
            if s_map[y][x+1] == '.':#右が平地なら
                s_map[y][x],s_map[y][x+1] = '.','>'#現在地と右を入れ替える
            else:
                s_map[y][x] = '>'

    else:#Shoot
        now_dir = s_map[y][x]
        ma = shoot(y,x,now_dir)
        return ma
    
def shoot(y,x,now_dir):
    if x<0 or y<0 or x==M or y==H:
        return s_map
    if now_dir == '>':#戦車が右を向いていたら
        while True:
            #再帰かな x+=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y,x+1,now_dir)
            break

    elif now_dir == '<':#戦車が左を向いていたら
        while True:
            #再帰かな x-=1　左の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y,x-1,now_dir)
            break
       
    elif now_dir == '^':
        while True:
            #再帰かな y-=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y-1,x,now_dir)
            break

    elif now_dir == 'v':
        while True:
            #再帰かな y+=1　右の地面を見ていく
            if s_map[y][x] == '*':
                s_map[y][x] = '.'
                break
            elif s_map[y][x] == '#':
                break
            else:
                shoot(y+1,x,now_dir)
            break

def search_index(s_map):#現在地のインデックスと方向を確認
    direction = ['^','v','>','<']
    for i in range(len(direction)):
        for j in range(len(s_map)):
            for k in range(len(s_map[0])):
                if direction[i] in s_map[j][k]:
                    x,y,d = j,k,direction[i]

    return x,y,d


if __name__== '__main__':
    T = int(input())#全体回数
    all_map=[]
    for i in range(T):
        s_map = []
        H,M = map(int,input().split())#マップの縦幅横幅
        for j in range(H):#マップ作成
            s_map.append([i for i in input()])
        N = int(input())#何回処理を行うか
        action = input()#アクション文字列
        for k in action:
            x,y,direction = search_index(s_map)
            #print("Now state (x,y)",y,x)
            #print(direction)
            #print("action = ",k)
            m  = move(k,x,y,direction)
        all_map.append(s_map)
    for j in range(len(all_map)):
        for i in range(len(all_map[j])):
            print(''.join(all_map[j][i]))
        if j != len(all_map)-1:
            print(" ")

Submission status: WA: Presentation Error

Solution code:
def move(y, x):
    p[2], p[3] = y, x
    if a[p[0] + y][p[1] + x] == ".":p[0] += y;p[1] += x

def shoot():
    th = p[0] + p[2]
    tw = p[1] + p[3]
    while 1:
        if a[th][tw] == "*":a[th][tw] = ".";break
        elif a[th][tw] == "#":break
        else: th += p[2]; tw += p[3]

for _ in range(int(input())):
    h, w = map(int, input().split())
    a = [["#"] * (w + 2)] + [["#"] + list(input()) + ["#"] for _ in range(h)] + [["#"] * (w + 2)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if a[i][j] == ">":p = [i, j, 0, 1];a[i][j] = ".";break
            elif a[i][j] == "^":p = [i, j, -1, 0];a[i][j] = ".";break
            elif a[i][j] == "<":p = [i, j, 0, -1];a[i][j] = ".";break
            elif a[i][j] == "v":p = [i, j, 1, 0];a[i][j] = ".";break
    n, c = int(input()), input()
    for i in range(n):
        if c[i] == "U":move(-1, 0)
        elif c[i] == "R":move(0, 1)
        elif c[i] == "L":move(0, -1)
        elif c[i] == "D":move(1, 0)
        else:shoot()
    if (p[2], p[3]) == (0, 1):a[p[0]][p[1]] = ">"
    elif (p[2], p[3]) == (-1, 0):a[p[0]][p[1]] = "^"
    elif (p[2], p[3]) == (0, -1):a[p[0]][p[1]] = "<"
    else:a[p[0]][p[1]] = "v"
    for i in a[1:-1]:print("".join(i[1:-1]))
    print()