import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Collections; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author cteam056 */ public class Validate { /** * @param args the command line arguments */ static StringTokenizer tokenizer = new StringTokenizer(""); static BufferedReader reader; static int x, y; static String nextToken() throws Exception { if (tokenizer.hasMoreTokens()) { return tokenizer.nextToken(); } String line = reader.readLine(); if (line != null) { tokenizer = new StringTokenizer(line); return nextToken(); } return null; } public static void main(String[] args) throws Exception { reader = new BufferedReader(new InputStreamReader(System.in)); while (true) { String token = nextToken(); if (token == null) { return; } x = y = 0; SortedSet pic1 = new TreeSet(); int numLines = Integer.parseInt(token); for (int i = 0; i < numLines; i++) { processCommand(pic1); } x = y = 0; SortedSet pic2 = new TreeSet(); numLines = Integer.parseInt(nextToken()); for (int i = 0; i < numLines; i++) { processCommand(pic2); } Line first1 = pic1.first(); Line first2 = pic2.first(); int offX = first1.x1 - first2.x1; int offY = first1.y1 - first2.y1; for (Line line : pic2) { line.x1 += offX; line.x2 += offX; line.y1 += offY; line.y2 += offY; } if (pic1.equals(pic2)) { System.out.println("YES"); } else { System.out.println("NO"); } } } private static void processCommand(SortedSet pic) throws Exception { String mode = nextToken(); int relX = Integer.parseInt(nextToken()); int relY = Integer.parseInt(nextToken()); if ("M".equals(mode)) { x += relX; y += relY; } else { assert mode.equals("L"); Line line = new Line(x, y, x + relX, y + relY); x += relX; y += relY; line = mergeLines(pic, line); pic.add(line); } } static Line mergeLines(SortedSet pic, Line line) { Line toAdd = line; SortedSet copy = new TreeSet(pic); for (Line existing : copy) { boolean f = false; int xlen = existing.x2 - existing.x1; if (xlen == 0) { if (line.x1 == line.x2 && line.x1 == existing.x1) { pic.remove(existing); existing.merge(line); toAdd = mergeLines(pic, existing); } continue; } int m = (line.x1 - existing.x1) % (xlen); int t = (line.x1 - existing.x1) / (xlen); if (m == 0) { if ((existing.y2 - existing.y1) * t + existing.y1 == line.y1) { if (t == 0 || t == 1) { f = true; } m = (line.x2 - existing.x1) % xlen; t = (line.x2 - existing.x1) / xlen; if (m == 0) { if ((existing.y2 - existing.y1) * t + existing.y1 == line.y2) { if (t == 0 || t == 1 || f) { pic.remove(existing); existing.merge(line); toAdd = mergeLines(pic, existing); } } } } } } return toAdd; } static class Line implements Comparable { public int x1, y1, x2, y2; @Override public int hashCode() { int hash = 7; hash = 59 * hash + this.x1; hash = 59 * hash + this.y1; hash = 59 * hash + this.x2; hash = 59 * hash + this.y2; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Line other = (Line) obj; if (this.x1 != other.x1) { return false; } if (this.y1 != other.y1) { return false; } if (this.x2 != other.x2) { return false; } if (this.y2 != other.y2) { return false; } return true; } public Line(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; norm(); } @Override public int compareTo(Line o) { if (x1 != o.x1) { return Integer.compare(x1, o.x1); } else if (x2 != o.x2) { return Integer.compare(x2, o.x2); } else if (y1 != o.y1) { return Integer.compare(y1, o.y1); } else if (y2 != o.y2) { return Integer.compare(y2, o.y2); } return 0; } @Override public String toString() { return "Line{" + "x1=" + x1 + ", y1=" + y1 + ", x2=" + x2 + ", y2=" + y2 + '}'; } private void norm() { if (x1 > x2) { int t = x2; x2 = x1; x1 = t; } if (y1 > y2) { int t = y2; y2 = y1; y1 = t; } } private void merge(Line line) { if (line.x1 < x1 || line.y1 < y1) { x1 = line.x1; y1 = line.y1; } if (line.x2 > x2 || line.y2 > y2) { x2 = line.x2; y2 = line.y2; } norm(); } } }