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 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 BACKWARD k

Go backward by k tiles, without changing its facing direction
(1 k 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



Sample input:
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0


Sample output:
6 2
1 1

Submission status: Wrong Answer

Solution code:
moves = [
    {'F': lambda i, j, x: (i, min(n, j + x)), 'B': lambda i, j, x: (i, max(1, j - x))},
    {'F': lambda i, j, x: (min(m, i + x), j), 'B': lambda i, j, x: (max(1, i - x), j)},
    {'F': lambda i, j, x: (i, max(1, j - x)), 'B': lambda i, j, x: (i, min(n, j + x))},
    {'F': lambda i, j, x: (max(m, i - x), j), 'B': lambda i, j, x: (min(1, i + x), j)}]
turns = {'R': lambda i: (i + 1) % 4, 'L': lambda i: (i - 1) % 4}

while True:
    m, n = map(int, input().split())
    if not m:
        break
    i, j = 1, 1
    cci = 0
    while True:
        cmd = input()
        ch = cmd[0]
        if ch in 'FB':
            i, j = moves[cci][ch](i, j, int(cmd.split()[1]))
        elif ch in 'RL':
            cci = turns[ch](cci)
        else:
            print(i, j)
            break

Submission status: Wrong Answer

Solution code:
while True:
    w,h=map(int,raw_input().split())
    if w==h==0:break
    Board=[[0]*w for i in range(h)]
    mv=((0,1),(1,0),(0,-1),(-1,0))
    pos=0
    nx=ny=1
    while True:
        command=raw_input()
        if command=="STOP":
            break
        if command[-1].isdigit():
            d,l=command.split()
            if d=="FORWARD":
                for i in range(int(l)):
                    nx=nx+mv[pos][0] if 1<=nx+mv[pos][0]<=w else nx
                    ny=ny+mv[pos][1] if 1<=ny+mv[pos][1]<=h else ny
            elif d=="BACKWARD":
                for i in range(int(l)):
                    nx=nx-mv[pos][0] if 1<=nx+mv[pos][0]<=w else nx
                    ny=ny-mv[pos][1] if 1<=ny+mv[pos][1]<=h else ny
        else:
            if command=="LEFT":
                pos=pos-1 if pos!=0 else 3
            elif command=="RIGHT":
                pos=(pos+1)%4
    print nx,ny

Submission status: Wrong Answer

Solution code:
while True:
    w,h=map(int,raw_input().split())
    if w==h==0:break
    mv=((0,1),(1,0),(0,-1),(-1,0))
    pos=0
    nx=ny=1
    while True:
        command=raw_input()
        if command=="STOP":
            break
        if command[-1].isdigit():
            d,l=command.split()
            if d=="FORWARD":
                nx+=mv[pos][0]*int(l)
                ny+=mv[pos][1]*int(l)
            elif d=="BACKWARD":
                nx-=mv[pos][0]*int(l)
                ny-=mv[pos][1]*int(l)
            nx=min(nx,w)
            nx=max(nx,0)
            ny=min(ny,h)
            ny=max(ny,0)
        else:
            if command=="LEFT":
                pos=pos-1 if pos!=0 else 3
            elif command=="RIGHT":
                pos=(pos+1)%4
    print nx,ny

Submission status: Wrong Answer

Solution code:
import sys

checksize=0
place=[]
face=0 #N=0 E=1 S=2 W=3
X=0
Y=0
for l in sys.stdin:
    line=l.split()
    if line[0]=="0":
        break
    elif checksize==0:
        checksize=1
        A=map(int,line)
        X=A[0]
        Y=A[1]
        place=[1,1]
    elif line[0]=="STOP":
        checksize=0
        print str(place[0])+" "+str(place[1])
    elif line[0]=="LEFT":
        face=(face-1)%4
    elif line[0]=="RIGHT":
        face=(face+1)%4
    elif line[0]=="FORWARD":
        susumu=int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    elif line[0]=="BACKWARD":
        susumu=-int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    if place[0]<1:
        place[0]=1
    if place[0]>X:
        place[0]=X
    if place[1]<1:
        place[1]=1
    if place[1]>Y:
        place[1]=Y

Submission status: Wrong Answer

Solution code:
def go(n,x,dx,l):
	return min(n,max(0,x+l*dx))

while 1:
	m,n = map(int,raw_input().split())
	if m == 0: break
	x = y = 1
	dx,dy = 0,1
	while 1:
		com = raw_input().split()
		s = com[0][0]
		if   s == "S": break
		elif s == "F":
			l = int(com[1])
			x,y = go(m,x,dx,l), go(n,y,dy,l)
		elif s == "B":
			l = int(com[1])
			x,y = go(m,x,-dx,l),go(n,y,-dy,l)
		elif s == "R":
			dx,dy = dy,-dx
		elif s == "L":
			dx,dy = -dy,dx
	print x,y

Submission status: Accepted

Solution code:
# AOJ 1104: Where's Your Robot?
# Python3 2018.7.14 bal4u

# N,E,S,W -> 0,1,2,3
f = [[0,1],[1,0],[0,-1],[-1,0]]
b = [[0,-1],[-1,0],[0,1],[1,0]]

while True:
	w, h = map(int, input().split())
	if w == 0: break
	r, c, d = 1, 1, 0
	while True:
		buf = input().split()
		if   buf[0] == "STOP": break
		elif buf[0] == "RIGHT": d = (d+1)%4
		elif buf[0] == "LEFT": d = (d+3)%4
		else:
			if buf[0] == "FORWARD":
				r2 = r + int(buf[1])*f[d][1]
				c2 = c + int(buf[1])*f[d][0]
			else:   # BACKWARD k
				r2 = r + int(buf[1])*b[d][1]
				c2 = c + int(buf[1])*b[d][0]
			if r2 < 1: r2 = 1
			if r2 > h: r2 = h
			if c2 < 1: c2 = 1
			if c2 > w: c2 = w
			r, c = r2, c2
	print(c, r)

Submission status: Accepted

Solution code:
def numchange(boo):
    if boo:return 1
    else: return -1

while True:
    m,n = map(int,input().split())
    if m==0: break

    yaxis = 1
    xaxis = 1
    rotation = 0
    while True:
        lnr = input()
        if lnr == "STOP": break
        elif lnr == "RIGHT": rotation = (rotation+1)%4
        elif lnr == "LEFT": rotation = (rotation+3)%4
        else:
            movequeue = lnr.split()
            moveamount = int(movequeue[1]) * numchange((rotation//2)==0) * numchange(movequeue[0]=="FORWARD")
            if rotation%2==0:
                yaxis += moveamount
                if yaxis <= 0:
                    yaxis = 1
                elif yaxis > n:
                    yaxis = n
            else:
                xaxis += moveamount
                if xaxis <= 0:
                    xaxis = 1
                elif xaxis > m:
                    xaxis = m
    print(xaxis,yaxis)

Submission status: Accepted

Solution code:
moves = [
    {'F': lambda i, j, x: (i, min(n, j + x)), 'B': lambda i, j, x: (i, max(1, j - x))},
    {'F': lambda i, j, x: (min(m, i + x), j), 'B': lambda i, j, x: (max(1, i - x), j)},
    {'F': lambda i, j, x: (i, max(1, j - x)), 'B': lambda i, j, x: (i, min(n, j + x))},
    {'F': lambda i, j, x: (max(1, i - x), j), 'B': lambda i, j, x: (min(m, i + x), j)}]
turns = {'R': lambda i: (i + 1) % 4, 'L': lambda i: (i - 1) % 4}

while True:
    m, n = map(int, input().split())
    if not m:
        break
    i, j = 1, 1
    cci = 0
    while True:
        cmd = input()
        ch = cmd[0]
        if ch in 'FB':
            i, j = moves[cci][ch](i, j, int(cmd.split()[1]))
        elif ch in 'RL':
            cci = turns[ch](cci)
        else:
            print(i, j)
            break

Submission status: Accepted

Solution code:
while True:
    col, row = map(int, input().split())
    if col == 0:
        break
    else:
        robot = [1, 1]
        direction = "up"
        while True:
            str = input()
            if str == "STOP":
                print(robot[0], robot[1])
                break
            elif str == "LEFT":
                if direction == "up":
                    direction = "left"
                elif direction == "left":
                    direction = "down"
                elif direction == "down":
                    direction = "right"
                else:           # right
                    direction = "up"
            elif str == "RIGHT":
                if direction == "up":
                    direction = "right"
                elif direction == "left":
                    direction = "up"
                elif direction == "down":
                    direction = "left"
                else:           # right
                    direction = "down"
            else:
                di, step = str.split()
                step = int(step)
                if direction == "up":
                    if di == "FORWARD":
                        robot[1] = min(robot[1] + step, row)
                    else:
                        robot[1] = max(robot[1] - step, 1)
                elif direction == "left":
                    if di == "FORWARD":
                        robot[0] = max(robot[0] - step, 1)
                    else:
                        robot[0] = min(robot[0] + step, col)
                elif direction == "down":
                    if di == "FORWARD":
                        robot[1] = max(robot[1] - step, 1)
                    else:
                        robot[1] = min(robot[1] + step, row)
                else:           # right
                    if di == "FORWARD":
                        robot[0] = min(robot[0] + step, col)
                    else:
                        robot[0] = max(robot[0] - step, 1)

Submission status: Accepted

Solution code:
while True:
    w,h=map(int,raw_input().split())
    if w==h==0:break
    mv=((0,1),(1,0),(0,-1),(-1,0))
    pos=0
    nx=ny=1
    while True:
        command=raw_input()
        if command=="STOP":
            break
        if command[-1].isdigit():
            d,l=command.split()
            if d=="FORWARD":
                nx+=mv[pos][0]*int(l)
                ny+=mv[pos][1]*int(l)
            elif d=="BACKWARD":
                nx-=mv[pos][0]*int(l)
                ny-=mv[pos][1]*int(l)
            nx=min(nx,w)
            nx=max(nx,1)
            ny=min(ny,h)
            ny=max(ny,1)
        else:
            if command=="LEFT":
                pos=pos-1 if pos!=0 else 3
            elif command=="RIGHT":
                pos=(pos+1)%4
    print nx,ny

Submission status: Accepted

Solution code:
def go(n,x,dx,l):
	return min(n,max(1,x+l*dx))

while 1:
	m,n = map(int,raw_input().split())
	if m == 0: break
	x = y = 1
	dx,dy = 0,1
	while 1:
		com = raw_input().split()
		s = com[0][0]
		if   s == "S": break
		elif s == "F":
			l = int(com[1])
			x,y = go(m,x,dx,l), go(n,y,dy,l)
		elif s == "B":
			l = int(com[1])
			x,y = go(m,x,-dx,l),go(n,y,-dy,l)
		elif s == "R":
			dx,dy = dy,-dx
		elif s == "L":
			dx,dy = -dy,dx
	print x,y

Submission status: Accepted

Solution code:
while 1:
	m,n = map(int,raw_input().split())
	if m == 0: break
	x = y = 1
	dx,dy = 0,1
	while 1:
		com = raw_input().split()
		s = com[0][0]
		if   s == "S": break
		elif s == "R":
			dx,dy = dy,-dx
		elif s == "L":
			dx,dy = -dy,dx
		else:
			d = 1 if s == "F" else -1
			l = d*int(com[1])
			x,y = min(m,max(1,x+l*dx)),min(n,max(1,y+l*dy))
	print x,y

Submission status: Runtime Error

Solution code:
import sys

checksize=0
place=[]
face=0 #N=0 E=1 S=2 W=3
X=0
Y=0
for l in sys.stdin:
    line=l.split()
    if checksize==0:
        checksize=1
        A=map(int,line)
        X=A[0]
        Y=A[1]
        place=[1,1]
    elif line[0]=="STOP":
        checksize=0
    elif line[0]=="LEFT":
        face=(face-1)%4

    elif line[0]=="RIGHT":
        face=(face+1)%4

    elif line[0]=="FORWARD":
        susumu=int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)

    elif line[0]=="BACKWARD":
        susumu=-int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)

    if place[0]<0:
        place[0]=0
    if place[0]>X:
        place[0]=X
    if place[1]<0:
        place[1]=0
    if place[1]>Y:
        place[1]=Y

Submission status: Runtime Error

Solution code:
import sys

checksize=0
place=[]
face=0 #N=0 E=1 S=2 W=3
X=0
Y=0
for l in sys.stdin:
    line=l.split()
    if checksize==0:
        checksize=1
        A=map(int,line)
        X=A[0]
        Y=A[1]
        place=[1,1]
    elif line[0]=="STOP":
        checksize=0
        print str(place[0])+" "+str(place[1])
    elif line[0]=="LEFT":
        face=(face-1)%4
    elif line[0]=="RIGHT":
        face=(face+1)%4
    elif line[0]=="FORWARD":
        susumu=int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    elif line[0]=="BACKWARD":
        susumu=-int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    if place[0]<0:
        place[0]=0
    if place[0]>X:
        place[0]=X
    if place[1]<0:
        place[1]=0
    if place[1]>Y:
        place[1]=Y

Submission status: Runtime Error

Solution code:
import sys

checksize=0
place=[]
face=0 #N=0 E=1 S=2 W=3
X=0
Y=0
for l in sys.stdin:
    line=l.split()
    if checksize==0:
        checksize=1
        A=map(int,line)
        X=A[0]
        Y=A[1]
        place=[1,1]
    elif line[0]=="STOP":
        checksize=0
        print str(place[0])+" "+str(place[1])
    elif line[0]=="LEFT":
        face=(face-1)%4
    elif line[0]=="RIGHT":
        face=(face+1)%4
    elif line[0]=="FORWARD":
        susumu=int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    elif line[0]=="BACKWARD":
        susumu=-int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    if place[0]<1:
        place[0]=1
    if place[0]>X:
        place[0]=X
    if place[1]<1:
        place[1]=1
    if place[1]>Y:
        place[1]=Y

Submission status: Runtime Error

Solution code:
import sys

checksize=0
place=[]
face=0 #N=0 E=1 S=2 W=3
X=0
Y=0
for l in sys.stdin:
    line=l.split()
    if line[0]==0:
        break
    elif checksize==0:
        checksize=1
        A=map(int,line)
        X=A[0]
        Y=A[1]
        place=[1,1]
    elif line[0]=="STOP":
        checksize=0
        print str(place[0])+" "+str(place[1])
    elif line[0]=="LEFT":
        face=(face-1)%4
    elif line[0]=="RIGHT":
        face=(face+1)%4
    elif line[0]=="FORWARD":
        susumu=int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    elif line[0]=="BACKWARD":
        susumu=-int(line[1])
        place[(face+1)%2]=place[(face+1)%2]+susumu*(1-(face/2)*2)
    if place[0]<1:
        place[0]=1
    if place[0]>X:
        place[0]=X
    if place[1]<1:
        place[1]=1
    if place[1]>Y:
        place[1]=Y