Problem:
Where's Your Robot?

You have full control over a robot that walks around in a rectangular
field paved with square tiles like a chessboard. There are m
columns of tiles from west to east, and n rows of tiles from
south to north (1 <= m, n <= 100). Each tile is given a pair
of coordinates, such as (i, j), where 1 <= i <= m
and 1 <= j <= n.

Your robot is initially on the center of the tile at (1, 1), that is,
one at the southwest corner of the field, facing straight north. It
can move either forward or backward, or can change its facing
direction by ninety degrees at a time, according to a command you give
to it, which is one of the following.

FORWARD k

Go forward by k tiles to its facing direction (1 <= k < 100).

BACKWARD k

Go backward by k tiles, without changing its facing direction
(1 <= k < 100).

RIGHT

Turn to the right by ninety degrees.

LEFT

Turn to the left by ninety degrees.

STOP

Stop.

While executing either a "FORWARD" or a
"BACKWARD" command, the robot may bump against the
wall surrounding the field. If that happens, the robot gives up the
command execution there and stands at the center of the tile right in
front of the wall, without changing its direction.

After finishing or giving up execution of a given command, your robot
will stand by for your next command.

Input

The input consists of one or more command sequences. Each input line
has at most fifty characters.

The first line of a command sequence contains two integer numbers
telling the size of the field, the first number being the number of
columns and the second being the number of rows. There might be white
spaces (blanks and/or tabs) before, in between, or after the two numbers.
Two zeros as field size indicates the end of input.

Each of the following lines of a command sequence contains a command
to the robot. When a command has an argument, one or more white
spaces are put between them. White spaces may also appear before and
after the command and/or its argument.

A command sequence is terminated by a line containing a
"STOP" command. The next command sequence, if any,
starts from the next line.

Output

The output should be one line for each command sequence in the input.
It should contain two numbers i and j of the coordinate pair
(i, j), in this order, of the tile on which your robot stops.
Two numbers should be separated by one white spaces.

Sample Input

6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0

Output for the Sample Input

6 2
1 1

Solution in JAVA (judge verdict: Accepted)

import java.awt.geom.Point2D;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Main {	
	Scanner in = new Scanner(System.in);
	public static void main(String[] args){
		new Main();
	}
	public Main(){		
		new AOJ1104();
	}
	
	class AOJ1104{
		int x,y,mx,my,muki=3;
		public AOJ1104() {
			while(true){
				x = 1;y = 1;muki=3;
				mx = in.nextInt();
				my = in.nextInt();
				if(mx+my==0)break;
				while(true){
//					System.out.println(x+" "+y);
					if(doit(in.next()))break;
				}
				System.out.println(x+" "+y);
			}
		}
		
		boolean doit(String meirei){
			int num=0;
			if(meirei.equals("FORWARD")||meirei.equals("BACKWARD")){
				num=in.nextInt();
				if(meirei.equals("FORWARD")){
					int nx[] = {1,0,-1,0};
					int ny[] = {0,-1,0,1};
					for(int i=0;i<num;i++){
						if(x+nx[muki]<=0||x+nx[muki]>mx||y+ny[muki]<=0||y+ny[muki]>my)break;
						x+=nx[muki];
						y+=ny[muki];
					}
				}else{
					int nx[] = {-1,0,1,0};
					int ny[] = {0,1,0,-1};
					for(int i=0;i<num;i++){
						if(x+nx[muki]<=0||x+nx[muki]>mx||y+ny[muki]<=0||y+ny[muki]>my)break;
						x+=nx[muki];
						y+=ny[muki];
					}
				}
			}else if(meirei.equals("STOP")){
				return true;
			}else if(meirei.equals("RIGHT")){
				//右=0,下=1,左=2,上=3(向き)
				muki = (muki+1)%4;
			}else if(meirei.equals("LEFT")){
				muki = (muki+3)%4;
			}
			return false;
		}
	}

			
			
	class AOJ2503{
		int MAX = 0;
		public AOJ2503() {
			Scanner in = new Scanner(System.in);
			int n = in.nextInt();
			int m = in.nextInt();

			int[][] cost = new int[n][n];

			int[] dp = new int[n];
			for(int i=0;i<n*n;i++){
				cost[i/n][i%n]=MAX;
			}
			for(int i=0;i<m;i++){
				int a = in.nextInt();
				int b = in.nextInt();
				cost[a][b]=in.nextInt();
			}	
			//			TODO 一番大きいパスを0のポイントからn-1のポイントまでの

			for(int s=1;s<n;s++){
				for(int i=0;i<=s;i++){
					if(cost[i][s]==MAX)continue;
					dp[s]=Math.max(dp[s],dp[i]+cost[i][s]);
				}
			}
			for(int s=0;s<n;s++)for(int i=0;i<n;i++){
				if(cost[s][i]==MAX)continue;
				dp[i]=Math.max(dp[s]+cost[s][i],dp[i]);
			}
			for(int i=0;i<n;i++)System.out.print(dp[i]+" ");
			System.out.println();
			System.out.println(dp[n-1]);
		}
	}
}