#!/usr/bin/python3 from collections import defaultdict import sys sys.setrecursionlimit(250) class Game: def __init__(self, w, h): self.w = w self.h = h self.field = defaultdict(lambda: None) # links to robots indexed by coordinates self.robots = [] #list of robots indexed by id def command(self, robot, *rcmd): self.robots[robot].command(*rcmd) def isfield(self, pos): return 0 <= pos[0] < self.w and 0 <= pos[1] < self.h class Robot: def __init__(self, game, pos, rot): self.game = game self.pos = pos self.rot = rot if game.field[pos]: raise ValueError game.field[pos] = self self.num = len(game.robots) game.robots.append(self) def next(self, dir): # return coordinates of the field in the given direction dx, dy = { 0: (0, -1), # up 1: (1, 0), # right 2: (0, 1), # down 3: (-1, 0), # left }[dir] return (self.pos[0] + dx, self.pos[1] + dy) def command(self, cmd, iters): if cmd == 'L': self.rot = (self.rot - iters) % 4 elif cmd == 'P': self.rot = (self.rot + iters) % 4 elif cmd == 'K': self.step(iters) def step(self, iters=1): for i in range(iters): if not self.move(self.rot): break # make steps as long as it is possible def set_pos(self, pos): assert not self.game.field[pos] del self.game.field[self.pos] self.pos = pos self.game.field[pos] = self def move(self, dir): """Move robot in direction ``dir``. Return True on success, False otherwise.""" next = self.next(dir) if not self.game.isfield(next): return False # hit a wall if self.game.field[next]: # there is another robot in the way if not self.game.field[next].move(dir): return False # that robot is not pushable self.set_pos(next) # nothing failed, robot can move return True def main(): w, h, nrobots, ncommands = map(int, input().split()) game = Game(w, h) for i in range(nrobots): x, y, rot = input().split() x, y = ( int(n) for n in (x,y) ) assert 0 <= x < game.w assert 0 <= y < game.h rot = 'SVJZ'.index(rot) # translate letters to numbers Robot(game, (x,y), rot) for i in range(ncommands): robot, cmd, iters = input().split() robot = int(robot) assert 0 <= robot < len(game.robots) iters = int(iters) assert 1 <= iters <= 10**6 assert cmd in 'LPK' game.command(robot, cmd, iters) for robot in game.robots: print(robot.pos[0], robot.pos[1], 'SVJZ'[robot.rot]) if __name__ == '__main__': main()