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

Solution in JAVA (judge verdict: Accepted)



import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		// ????????????????????°?????\???
		Scanner in = new Scanner(System.in);
		int dataSet = in.nextInt();

		// ????????????????????°?????????????????????
		for(int i = 0; i < dataSet; i++){

			// ?????????????????¨?¨????????????\???
			int height = in.nextInt();
			int width = in.nextInt();

			// ????????????????????????????´???????????¬??????????
			String[][] townMap = new String[height][width];

			// ???????????????????????????
			for(int j = 0; j < height; j ++){

				// ???????????????????????\???
				String mapData = in.next();

				// ??\????????????????????????????????????
				townMap[j] = mapData.split("");

			}

			// ?????£????????????????????§????????????
			Field field = new Field(townMap);
			// ??????????????????????????????
			int[] tankPosition = field.tankPosition();
			// ????????????????????????
			String tankDirection = townMap[tankPosition[0]][tankPosition[1]];
			// ?????????????????§???????????????
			Tank tank = new Tank(tankPosition, tankDirection);

			// ??????????????????????????°?????\???
			int commandCount = in.nextInt();

			// ??\????????????????????????????????\??????????????????????????????1????????????????????????????????????????´?
			String commands = in.next();
			char[] commandArray = commands.toCharArray();

			// ???????????°?????????????????????
			for(int j = 0; j < commandCount; j++){

				// ??????????????????????????´???
				if(commandArray[j] == 'S'){

					// ???????????????????????????
					tank.shoot(tankDirection, field);

				// ?§???????????????´???
				} else {

					// ???????§????????????????
					tank.move(commandArray[j], field);
					tankPosition = field.tankPosition();
					tankDirection = townMap[tankPosition[0]][tankPosition[1]];

				}

			}

			// ????????????
			int j = 0;
			int k = 0;

			// ???????????????????????????
			for(j = 0; j < height; j++) {

				for(k = 0; k < width; k++) {

					if(k < width - 1) {

						System.out.print(townMap[j][k]);

					// ???????????¨??????????????\??????
					} else {

						System.out.println(townMap[j][k]);

					}
				}
			}

			// ??????????????????????????¨????????????????????\??????
			if(i < dataSet - 1) {

				System.out.println();

			}
		}
	}
}

// ?????£??????????????????
class Field{

	// ?????£??????????????°
	String[][] field;

	// ?????????????????????
	Field(String[][] field){

		this.field = new String[field.length][];

		for(int i = 0; i < field.length; i++) {

			this.field[i] = field[i];

			for(int j = 0; j < field[i].length; j++) {

				this.field[i][j] = field[i][j];

			}
		}
	}

	// ??????????????????????´¢??????????????????
	int[] tankPosition() {

		int[] tankPosition = new int[2];

		for(int i = 0; i < this.field.length; i++) {

			for(int j = 0; j < this.field[i].length; j++) {

				if(field[i][j].equals("^") || field[i][j].equals("v")
						|| field[i][j].equals("<") || field[i][j].equals(">")) {

					tankPosition[0] = i;
					tankPosition[1] = j;
				}
			}
		}

		return tankPosition;

	}
}

// ???????????????
class Tank{

	// ?????£??????????????°
	int[] tankPosition = new int[2];
	String tankDirection;

	// ?????????????????????
	Tank(int[] tankPosition, String tankDirection) {

		this.tankPosition[0] = tankPosition[0];
		this.tankPosition[1] = tankPosition[1];
		this.tankDirection = tankDirection;
	}

	// ??????????§??????????????????????
	void move(char command, Field field) {

		// ??\????????????????????¨?????????
		switch(command) {

		// ???????§????????????????
		case 'U':

			// ???????????°???????§????
			if(tankPosition[0] != 0 && field.field[tankPosition[0] - 1][tankPosition[1]].equals(".")){

				// ??????????????´????????????????????????????????????
				field.field[tankPosition[0] - 1][tankPosition[1]] = "^";
				// ??????????????´???????????°???????????????
				field.field[tankPosition[0]][tankPosition[1]] = ".";
				// ?????????????????¨??°?????£??\
				tankPosition[0] = tankPosition[0] - 1;

			} else {

				// ???????§?????????????????????¨??°??§????????????????????????
				field.field[tankPosition[0]][tankPosition[1]] = "^";

			}

			break;

		case 'D':

			// ???????????°???????§????
			if(tankPosition[0] != field.field.length - 1 && field.field[tankPosition[0] + 1][tankPosition[1]].equals(".")){

				// ??????????????´????????????????????????????????????
				field.field[tankPosition[0] + 1][tankPosition[1]] = "v";
				// ??????????????´???????????°???????????????
				field.field[tankPosition[0]][tankPosition[1]] = ".";
				// ?????????????????¨??°?????£??\
				tankPosition[0] = tankPosition[0] + 1;

			} else {

				// ???????§?????????????????????¨??°??§????????????????????????
				field.field[tankPosition[0]][tankPosition[1]] = "v";

			}

			break;

		case 'L':

			// ???????????°???????§????
			if(tankPosition[1] != 0 && field.field[tankPosition[0]][tankPosition[1] - 1].equals(".")){

				// ??????????????´????????????????????????????????????
				field.field[tankPosition[0]][tankPosition[1] - 1] = "<";
				// ??????????????´???????????°???????????????
				field.field[tankPosition[0]][tankPosition[1]] = ".";
				// ?????????????????¨??°?????£??\
				tankPosition[1] = tankPosition[1] - 1;

			} else {

				// ???????§?????????????????????¨??°??§????????????????????????
				field.field[tankPosition[0]][tankPosition[1]] = "<";

			}

			break;

		case 'R':

			// ???????????°???????§????
			if(tankPosition[1] != field.field[0].length - 1 && field.field[tankPosition[0]][tankPosition[1] + 1].equals(".")){

				// ??????????????´????????????????????????????????????
				field.field[tankPosition[0]][tankPosition[1] + 1] = ">";
				// ??????????????´???????????°???????????????
				field.field[tankPosition[0]][tankPosition[1]] = ".";
				// ?????????????????¨??°?????£??\
				tankPosition[1] = tankPosition[1] + 1;

			} else {

				// ???????§?????????????????????¨??°??§????????????????????????
				field.field[tankPosition[0]][tankPosition[1]] = ">";

			}

			break;

		}
	}

	// ???????????????????????????
	void shoot(String tankDirection, Field field) {

		// ??????????????¨??°??¨??????(????°???????)?????????????????????
		switch(tankDirection) {

		// ????°?????????????
		case "^":

			// ????°????????????????????????????????????\????§????
			for(int i = tankPosition[0] - 1; i >= 0; i--) {

				// ???????£??????¶???????????¨???????¶???????
				if(field.field[i][tankPosition[1]].equals("#")) {

					break;

				// ??¬?????¬????£??????¶???????????¨??????????????°????????£??????????¶???????
				} else if(field.field[i][tankPosition[1]].equals("*")) {

					field.field[i][tankPosition[1]] = ".";
					break;

				}
			}

			break;

		// ????°?????????????
		case "v":

			for(int i = tankPosition[0] + 1; i < field.field.length; i++) {

				if(field.field[i][tankPosition[1]].equals("#")) {

					break;

				} else if(field.field[i][tankPosition[1]].equals("*")) {

					field.field[i][tankPosition[1]] = ".";
					break;

				}
			}

			break;

		// ????°?????????????
		case "<":

			for(int i = tankPosition[1] - 1; i >= 0; i--) {

				if(field.field[tankPosition[0]][i].equals("#")) {

					break;

				} else if(field.field[tankPosition[0]][i].equals("*")) {

					field.field[tankPosition[0]][i] = ".";
					break;

				}
			}

			break;

		// ????°?????????????
		case ">":

			for(int i = tankPosition[1] + 1; i < field.field[0].length; i++) {

				if(field.field[tankPosition[0]][i].equals("#")) {

					break;

				} else if(field.field[tankPosition[0]][i].equals("*")) {

					field.field[tankPosition[0]][i] = ".";
					break;

				}
			}

			break;

		}
	}
}


//===========================??????=============================================

/*public class MainBatlleTown {

	public static void main(String[] args) {

		// ????????????????????°?????\???
		Scanner in = new Scanner(System.in);
		int dataSet = in.nextInt();

		// ????????????????????°?????????????????????
		for(int i = 0; i < dataSet; i++){

			// ?????????????????¨?¨????????????\???
			int height = in.nextInt();
			int width = in.nextInt();

			// ????????????????????????????´???????????¬??????????
			String[][] townMap = new String[height][width];

			// ???????????????????????????
			for(int j = 0; j < height; j ++){

				// ???????????????????????\???
				String mapData = in.next();

				// ??\????????????????????????????????????
				townMap[j] = mapData.split("");

			}

			// ??????????????????????????????
			int tankPositionH = 0;
			int tankPositionW = 0;

			for(int j = 0; j < height; j++) {

				for(int k = 0; k < width; k++) {

					if(townMap[j][k].equals("^") || townMap[j][k].equals("v")
							|| townMap[j][k].equals("<") || townMap[j][k].equals(">")) {

						tankPositionH = j;
						tankPositionW = k;

					}
				}
			}

			// ??????????????????????????°?????\???
			int commandCount = in.nextInt();

			// ??\????????????????????????????????\??????????????????????????????1????????????????????????????????????????´?
			String commands = in.next();
			char[] commandArray = commands.toCharArray();

			// ?????????????????§???????????????
			Tank tank = new Tank();

			// ???????????°?????????????????????
			for(int j = 0; j < commandCount; j++){

				// ??????????????????????????´???
				if(commandArray[j] == 'S'){

					// ???????????????????????????
					tank.shoot(townMap, height, width, tankPositionH, tankPositionW);


				// ??????????§?????????´???
				} else if(commandArray[j] == 'U' || commandArray[j] == 'D'){

					// ?§??????????????????§??????????§????(?????????)
					tankPositionH = tank.move(commandArray[j], townMap, height, width, tankPositionH, tankPositionW);

				// ?¨????????§?????????´???
				} else {

					// ?§??????????????????§??????????§????(?¨???????)
					tankPositionW = tank.move(commandArray[j], townMap, height, width, tankPositionH, tankPositionW);

				}
			}

			// ????????????
			int j = 0;
			int k = 0;

			// ???????????????????????????
			for(j = 0; j < height; j++) {

				for(k = 0; k < width; k++) {

					if(k < width - 1) {

						System.out.print(townMap[j][k]);

					// ???????????¨??????????????\??????
					} else {

						System.out.println(townMap[j][k]);

					}
				}
			}

			// ??????????????????????????¨????????????????????\??????
			if(i < dataSet - 1) {

				System.out.println();

			}
		}
	}
}


// ???????????????
class Tank{

	// ??????????§??????????????????????
	int move(char command, String[][] townMap, int height, int width, int tankPositionH, int tankPositionW){

		// ?????????(??????????????¨??°)?????\???????????°
		int tankPosition = 0;

		// ??\????????????????????¨?????????
		switch(command){

		// ???????§????????????????
		case 'U':

			// ???????????°???????§????
			if(tankPositionH != 0 && townMap[tankPositionH - 1][tankPositionW].equals(".")) {

				// ?????¨??°????????????????????????????????????????????????
				townMap[tankPositionH - 1][tankPositionW] = "^";
				// ?????¨??°????????°???????????????
				townMap[tankPositionH][tankPositionW] = ".";

				// ?§???????????????¨??°?????????????????£??\
				tankPosition = tankPositionH - 1;

			// ???????????°?????????????????°?????????????????´
			} else {

				// ???????????´
				townMap[tankPositionH][tankPositionW] = "^";
				// ?????¨??°?????????????????£??\
				tankPosition = tankPositionH;

			}

			break;

		// ???????§????????????????
		case 'D':

			if(tankPositionH != height - 1 && townMap[tankPositionH + 1][tankPositionW].equals(".")) {

				townMap[tankPositionH + 1][tankPositionW] = "v";
				townMap[tankPositionH][tankPositionW] = ".";

				tankPosition = tankPositionH + 1;

			} else {

				townMap[tankPositionH][tankPositionW] = "v";
				tankPosition = tankPositionH;

			}

			break;

		// ??????????????????
		case 'L':

			if(tankPositionW != 0 && townMap[tankPositionH][tankPositionW - 1].equals(".")) {

				townMap[tankPositionH][tankPositionW - 1] = "<";
				townMap[tankPositionH][tankPositionW] = ".";

				tankPosition = tankPositionW - 1;

			} else {

				townMap[tankPositionH][tankPositionW] = "<";
				tankPosition = tankPositionW;

			}

			break;

		// ??????????????????
		case 'R':

			if(tankPositionW != width - 1 && townMap[tankPositionH][tankPositionW + 1].equals(".")) {

				townMap[tankPositionH][tankPositionW + 1] = ">";
				townMap[tankPositionH][tankPositionW] = ".";

				tankPosition = tankPositionW + 1;

			} else {

				townMap[tankPositionH][tankPositionW] = ">";
				tankPosition = tankPositionW;

			}

			break;
		}

		// ?????´??????????????¨??°???????????????
		return tankPosition;

	}


	// ???????????????????????????
	void shoot(String[][] townMap, int height, int width, int tankPositionH, int tankPositionW) {

		// ??????????????¨??°??¨??????(????°???????)?????????????????????
		switch(townMap[tankPositionH][tankPositionW]) {

		// ????°?????????????
		case "^":

			// ????°????????????????????????????????????\????§????
			for(int i = tankPositionH - 1; i >= 0; i--) {

				// ???????£??????¶???????????¨???????¶???????
				if(townMap[i][tankPositionW].equals("#")) {

					break;

				// ??¬?????¬????£??????¶???????????¨??????????????°????????£??????????¶???????
				} else if(townMap[i][tankPositionW].equals("*")) {

					townMap[i][tankPositionW] = ".";
					break;

				}
			}

			break;

		// ????°??????????
		case "v":

			for(int i = tankPositionH + 1; i < height; i++) {

				if(townMap[i][tankPositionW].equals("#")) {

					break;

				} else if(townMap[i][tankPositionW].equals("*")) {

					townMap[i][tankPositionW] = ".";
					break;

				}
			}

			break;

		// ????°?????????????
		case "<":

			for(int i = tankPositionW - 1; i >= 0; i--) {

				if(townMap[tankPositionH][i].equals("#")) {

					break;

				} else if(townMap[tankPositionH][i].equals("*")) {

					townMap[tankPositionH][i] = ".";
					break;

				}
			}

			break;

		// ????°?????????????
		case ">":

			for(int i = tankPositionW + 1; i < width; i++) {

				if(townMap[tankPositionH][i].equals("#")) {

					break;

				} else if(townMap[tankPositionH][i].equals("*")) {

					townMap[tankPositionH][i] = ".";
					break;

				}
			}

			break;

		}
	}
}*/

//=======================================================================

			//???????????°?????????????????????
			/*for(int j = 0; j < commandCount; j++){

				if(commandArray[j] == 'S'){

					// ?????????????????????????????§??????????????????
					tank.shoot(townMap, height, width, tankPositionH, tankPositionW);


				} else if(commandArray[j] == 'U' || commandArray[j] == 'D'){

					// ????????????????????????
					// String direction = tank.changeDirection(commandArray[j]);
					tankPositionH = tank.changeDirection(commandArray[j], townMap, height, width, tankPositionH, tankPositionW);

				} else {

					tankPositionW = tank.changeDirection(commandArray[j], townMap, height, width, tankPositionH, tankPositionW);


					// ????????????????????????????????¨??????????????´
					switch(direction) {

					case "^":

						if(tankPositionH != 0 && townMap[tankPositionH - 1][tankPositionW].equals(".")) {

							townMap[tankPositionH - 1][tankPositionW] = "^";
							townMap[tankPositionH][tankPositionW] = ".";

							tankPositionH = tankPositionH - 1;

						} else {

							townMap[tankPositionH][tankPositionW] = "^";

						}

						break;

					case "v":

						if(tankPositionH != height - 1 && townMap[tankPositionH + 1][tankPositionW].equals(".")) {

							townMap[tankPositionH + 1][tankPositionW] = "v";
							townMap[tankPositionH][tankPositionW] = ".";

							tankPositionH = tankPositionH + 1;

						} else {

							townMap[tankPositionH][tankPositionW] = "v";

						}

						break;

					case "<":

						if(tankPositionW != 0 && townMap[tankPositionH][tankPositionW - 1].equals(".")) {

							townMap[tankPositionH][tankPositionW - 1] = "<";
							townMap[tankPositionH][tankPositionW] = ".";

							tankPositionW = tankPositionW - 1;

						} else {

							townMap[tankPositionH][tankPositionW] = "<";

						}

						break;

					case ">":

						if(tankPositionW != width - 1 && townMap[tankPositionH][tankPositionW + 1].equals(".")) {

							townMap[tankPositionH][tankPositionW + 1] = ">";
							townMap[tankPositionH][tankPositionW] = ".";

							tankPositionW = tankPositionW + 1;

						} else {

							townMap[tankPositionH][tankPositionW] = ">";

						}

						break;
					}*/


				//String direction = tank.changeDirection(commandArray[j]);

				//String direction = null;

				/*
				switch(direction){
				case 'U':

					direction = "^";
					break;

				case 'D':

					direction = "v";
					break;

				case 'L':

					direction = "<";
					break;

				case 'R':

					direction = ">";
					break;

				}

				return direction;

			}*/