Problem:
Problem B: Polygonal Line Search

Multiple polygonal lines are given on the xy-plane.
Given a list of polygonal lines and a template,
you must find out polygonal lines
which have the same shape as the template.

A polygonal line consists of several line segments parallel to x-axis or
y-axis. It is defined by a list of xy-coordinates of vertices from the start-point to the end-point in order,
and always turns 90 degrees at each vertex.
A single polygonal line does not pass the same point twice.

Two polygonal lines have the same shape when they
fully overlap each other only with rotation and translation
within xy-plane (i.e. without magnification or a flip).
The vertices given in reverse order from the start-point to the end-point is the
same as that given in order.

Figure 1 shows examples of polygonal lines.
In this figure, polygonal lines A and B have the same shape.

Write a program that answers polygonal lines which have the same shape as
the template.

Figure 1: Polygonal lines

Input

The input consists of multiple datasets.
The end of the input is indicated by a line which contains a zero.

A dataset is given as follows.

n

Polygonal line0

Polygonal line1

Polygonal line2

...

Polygonal linen

n is the number of polygonal lines for the object of search on
xy-plane.
n is an integer, and 1 <= n <= 50.
Polygonal line0 indicates the template.

A polygonal line is given as follows.

m

x1 y1

x2 y2

...

xm ym

m is the number of the vertices of a polygonal line (3 <= m <= 10).

xi and yi, separated by a space,
are the x- and y-coordinates
of a vertex, respectively (-10000 < xi < 10000, -10000 <yi < 10000).

Output

For each dataset in the input, your program should report numbers assigned to the polygonal lines that have the same shape as the template, in ascending order.
Each number must be written in a separate line without any other characters such as leading or trailing spaces.

Five continuous "+"s must be placed in a line at the end of each dataset.

Sample Input

5
5
0 0
2 0
2 1
4 1
4 0
5
0 0
0 2
-1 2
-1 4
0 4
5
0 0
0 1
-2 1
-2 2
0 2
5
0 0
0 -1
2 -1
2 0
4 0
5
0 0
2 0
2 -1
4 -1
4 0
5
0 0
2 0
2 1
4 1
4 0
4
4
-60 -75
-60 -78
-42 -78
-42 -6
4
10 3
10 7
-4 7
-4 40
4
-74 66
-74 63
-92 63
-92 135
4
-12 22
-12 25
-30 25
-30 -47
4
12 -22
12 -25
30 -25
30 47
3
5
-8 5
-8 2
0 2
0 4
8 4
5
-3 -1
0 -1
0 7
-2 7
-2 16
5
-1 6
-1 3
7 3
7 5
16 5
5
0 1
0 -2
8 -2
8 0
17 0
0

Output for the Sample Input

1
3
5
+++++
3
4
+++++
+++++

Solution in JAVA (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String line;
			while(true){
				/* input */
				line = br.readLine();

				int c = Integer.parseInt(line);
				if(c==0) return;

				Line[] d = new Line[c+1];

				//line 0
				int ln = Integer.parseInt(br.readLine());
				ArrayList<Coordinate> cd = new ArrayList<Coordinate>();

				for(int l=0;l<ln;l++){
					cd.add(new Coordinate(br.readLine()));
				}
				d[0] = new Line(cd);


				//line 1--c
				for(int k=1;k<=c;k++){
					ln = Integer.parseInt(br.readLine());
					cd = new ArrayList<Coordinate>();

					for(int l=0;l<ln;l++){
						cd.add(new Coordinate(br.readLine()));
					}
					d[k] = new Line(cd);
				}

				/* processing */
				//set the initial points to (0,0)
				for(int a=0;a<=c;a++){
					d[a] = d[a].setOrigin();
				}

				//find identical lines
				ArrayList<Integer> ans = new ArrayList<Integer>();

				for(int a=1;a<=c;a++){
					if(compareLines(d[0],d[a])){
						ans.add(a);
					}
				}
				
				for(int i : ans){
					System.out.println(i);
				}
				System.out.println("+++++");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static boolean compareLines(Line a, Line b){
		if(compareLines_rec(a,b)){
			return true;
		} else {
			return compareLines_rec(a,b.reverse());
		}
	}

	public static boolean compareLines_rec(Line a, Line b){
		if(a.points.size()!=b.points.size()) return false;

		//compare the lengths
		for(int i=0;i<a.points.size();i++){
			for(int j=0;j<a.points.size()-1;j++){
				if(a.lengths()[j]!=b.lengths()[j]){
					return false;
				}
			}
		}

		a = a.setOrigin();
		b = b.setOrigin();
		
		
		int rotate = 0;
		//check if it's rotated based on the first point
		if(a.points.get(1).x!=0){
			if(b.points.get(1).x==a.points.get(1).x){
				rotate = 0;
			} else if(b.points.get(1).y==-a.points.get(1).x){
				rotate = 1;
			} else if(b.points.get(1).x==-a.points.get(1).x){
				rotate = 2;
			} else if(b.points.get(1).y==a.points.get(1).x){
				rotate = 3;
			}
		} else {
			if(b.points.get(1).y==a.points.get(1).y){
				rotate = 0;
			} else if(b.points.get(1).x==-a.points.get(1).y){
				rotate = 1;
			} else if(b.points.get(1).y==-a.points.get(1).y){
				rotate = 2;
			} else if(b.points.get(1).x==a.points.get(1).y){
				rotate = 3;
			}
		}

		b = b.rotate(rotate);
		//b.print();

		for(int i=0;i<a.points.size();i++){
			if(a.points.get(i).x!=b.points.get(i).x) return false;
			if(a.points.get(i).y!=b.points.get(i).y) return false;
		}

		return true;
	}

	static class Coordinate{
		public int x;
		public int y;

		public Coordinate(String str){
			this.x = Integer.parseInt(str.split(" ")[0]);
			this.y = Integer.parseInt(str.split(" ")[1]);
		}

		public Coordinate(int x, int y){
			this.x = x;
			this.y = y;
		}

		public void output(){
			System.out.println(this.x + "," + this.y);
		}

		public void modify(int x, int y){
			this.x = x;
			this.y = y;
		}
	}

	static class Line{
		public ArrayList<Coordinate> points;

		public Line(){
			this.points = null;
		}

		public Line(ArrayList<Coordinate> c){
			this.points = c;
		}
		
		public void print(){
			for(Coordinate c : this.points){
				System.out.println(c.x + "," + c.y);
			}
		}
		
		public Line reverse(){
			Collections.reverse(this.points);

			return this;	
		}

		public Line setOrigin(){
			//set the initial points to (0,0)
			int x0 = this.points.get(0).x;
			int y0 = this.points.get(0).y;
			if(x0==0&&y0==0){
				return this;
			}
			for(int b=0;b<this.points.size();b++){
				this.points.get(b).modify(this.points.get(b).x-x0,this.points.get(b).y-y0);
			}
			return this;
		}

		public int[] lengths(){
			int[] lengths = new int[this.points.size()-1];
			for(int i=0;i<this.points.size()-1;i++){
				lengths[i] = Math.abs(this.points.get(i).x-this.points.get(i+1).x)+Math.abs(this.points.get(i).y-this.points.get(i+1).y);
			}
			return lengths;
		}

		public Line rotate(int r){
			switch(r){
			case 1:
				for(int i=1;i<this.points.size();i++){
					int x = this.points.get(i).x;
					int y = this.points.get(i).y;
					this.points.get(i).modify(-y,x);
				}
				break;
			case 2:
				for(int i=1;i<this.points.size();i++){
					int x = this.points.get(i).x;
					int y = this.points.get(i).y;
					this.points.get(i).modify(-x,-y);
				}
				break;
			case 3:
				for(int i=1;i<this.points.size();i++){
					int x = this.points.get(i).x;
					int y = this.points.get(i).y;
					this.points.get(i).modify(y,-x);
				}
				break;
			default:
				break;
			}
			return this;
		}
	}
}