Problem:
Problem D: Digits on the Floor

Taro attempts to tell digits to Hanako by putting straight bars on the floor. Taro wants to
express each digit by making one of the forms shown in Figure 1.

Since Taro may not have bars of desired lengths, Taro cannot always make forms exactly as
shown in Figure 1. Fortunately, Hanako can recognize a form as a digit if the connection
relation between bars in the form is kept. Neither the lengths of bars nor the directions of forms
affect Hanako’s perception as long as the connection relation remains the same. For example,
Hanako can recognize all the awkward forms in Figure 2 as digits. On the other hand, Hanako
cannot recognize the forms in Figure 3 as digits. For clarity, touching bars are slightly separated
in Figures 1, 2 and 3. Actually, touching bars overlap exactly at one single point.

Figure 1: Representation of digits

Figure 2: Examples of forms recognized as digits

In the forms, when a bar touches another, the touching point is an end of at least one of them.
That is, bars never cross. In addition, the angle of such two bars is always a right angle.

To enable Taro to represent forms with his limited set of bars, positions and lengths of bars can
be changed as far as the connection relations are kept. Also, forms can be rotated.

Keeping the connection relations means the following.

Figure 3: Forms not recognized as digits (these kinds of forms are not contained in the dataset)

Separated bars are not made to touch.

Touching bars are not made separate.

When one end of a bar touches another bar, that end still touches the same bar. When it
touches a midpoint of the other bar, it remains to touch a midpoint of the same bar on
the same side.

The angle of touching two bars is kept to be the same right angle (90 degrees and -90
degrees are considered different, and forms for 2 and 5 are kept distinguished).

Your task is to find how many times each digit appears on the floor.

The forms of some digits always contain the forms of other digits. For example, a form for
9 always contains four forms for 1, one form for 4, and two overlapping forms for 7. In this
problem, ignore the forms contained in another form and count only the digit of the “largest”
form composed of all mutually connecting bars. If there is one form for 9, it should be interpreted
as one appearance of 9 and no appearance of 1, 4, or 7.

Input

The input consists of a number of datasets. Each dataset is formatted as follows.

n

x1a y1a x1b y1b

x2a y2a x2b y2b

.

.

.

xna yna xnb ynb

In the first line, n represents the number of bars in the dataset. For the rest of the lines, one
line represents one bar. Four integers xa, ya , xb , yb , delimited by single spaces, are given in
each line. xa and ya are the x- and y-coordinates of one end of the bar, respectively. xb and
yb are those of the other end. The coordinate system is as shown in Figure 4. You can assume
1 ≤ n ≤ 1000 and 0 ≤ xa , ya , xb , yb ≤ 1000.

The end of the input is indicated by a line containing one zero.

Figure 4: The coordinate system

You can also assume the following conditions.

More than two bars do not overlap at one point.

Every bar is used as a part of a digit. Non-digit forms do not exist on the floor.

A bar that makes up one digit does not touch nor cross any bar that makes up another
digit.

There is no bar whose length is zero.

Output

For each dataset, output a single line containing ten integers delimited by single spaces. These
integers represent how many times 0, 1, 2, . . . , and 9 appear on the floor in this order. Output
lines must not contain other characters.

Sample Input

9
60 140 200 300
300 105 330 135
330 135 250 215
240 205 250 215
298 167 285 154
30 40 30 90
30 90 150 90
150 90 150 20
30 40 150 40
8
320 20 300 60
320 20 380 50
380 50 240 330
10 50 40 20
10 50 110 150
110 150 180 80
40 20 37 17
37 17 27 27
20
72 222 132 182
204 154 204 54
510 410 520 370
404 54 204 54
530 450 410 450
204 68 404 68
80 110 120 30
130 160 180 60
520 370 320 320
310 360 320 320
120 30 180 60
60 100 80 110
404 154 204 154
80 60 60 100
430 550 590 550
510 410 310 360
430 450 430 550
404 54 404 154
232 202 142 262
142 262 102 202
0

Output for the Sample Input

0 1 0 1 0 0 0 0 0 1
0 0 0 0 0 1 0 1 0 0
1 0 1 0 2 0 0 0 1 0

Solution in C++ (judge verdict: Wrong Answer)

#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef pair<P,P> Edge;
typedef long long ll;

const double PI=4*atan(1.0);
const int MAX_SIZE = 10000;

// 内積
double dot(P a, P b) {
  return (a.real() * b.real() + a.imag() * b.imag());
}
// 外積
double cross(P a, P b) {
  return (a.real() * b.imag() - a.imag() * b.real());
}
// 点cが直線ab上にあるかないか
int is_point_on_line(P a, P b, P c) {
  return EQ( cross(b-a, c-a), 0.0 );
}
// 2直線の直行判定
int is_orthogonal(P a1, P a2, P b1, P b2) {
  return EQ( dot(a1-a2, b1-b2), 0.0 );
}
// 2直線の平行判定
int is_parallel(P a1, P a2, P b1, P b2) {
  return EQ( cross(a1-a2, b1-b2), 0.0 );
}
// 点a,bを通る直線と点cの間の距離
double distance_l_p(P a, P b, P c) {
  return abs(cross(b-a, c-a)) / abs(b-a);
}
// 点a,bを端点とする線分と点cとの距離
double distance_ls_p(P a, P b, P c) {
  if ( dot(b-a, c-a) < EPS ) return abs(c-a);
  if ( dot(a-b, c-b) < EPS ) return abs(c-b);
  return abs(cross(b-a, c-a)) / abs(b-a);
}

int n;
P lines[1001][2];
bool passed[1001];

class Point{
public:
  P p;
  bool operator<(const Point &o)const{
    if(EQ(p.real(),o.p.real())){
      if(EQ(p.imag(),o.p.imag()))return false;
      else return p.imag()<o.p.imag();
    }
    return p.real()<o.p.real();
  }
  Point(P _p){
    p=_p;
  }
  Point(){}
};
set<Point> tp;
set<Point> cp;

void dfs(int now,int &connect,int &touch
	 ,int &lineCnt,vector<P> &tmp,bool fst,int prv){
  lineCnt++;
  passed[now]=true;
  bool isMove=false;
  bool used[2];
  used[0]=used[1]=false;
  // どちらの端点を見るか
  for(int j=0;j<2;j++){
    for(int i=0;i<n;i++){
      if(i==prv||i==now)continue;
      // 交点を持つ線分に移動
      for(int k=0;k<2;k++){
	if(EQV(lines[now][j],lines[i][k])){
	  //connect++;
	  cp.insert(Point(lines[i][k]));
	  if(!passed[i]){
	    dfs(i,connect,touch,lineCnt,tmp,false,i);
	    isMove=true;
	  }
	}
      }
      if(EQ(distance_ls_p(lines[i][0],lines[i][1],
			  lines[now][j]),0)
	 &&!EQV(lines[i][0],lines[now][j])
	 &&!EQV(lines[i][1],lines[now][j])){
	tp.insert(Point(lines[now][j]));
	//touch++;
	if(!passed[i]){
	  dfs(i,connect,touch,lineCnt,tmp,false,i);
	  isMove=true;
	}
      }
    }
  }
  for(int i=0;i<n;i++){
    if(i==now||i==prv)continue;
    for(int j=0;j<2;j++){
      if(EQ(distance_ls_p(lines[now][0],lines[now][1],
			  lines[i][j]),0)
	 &&!EQV(lines[now][0],lines[i][j])
	 &&!EQV(lines[now][1],lines[i][j])){
	tp.insert(Point(lines[i][j]));
	//touch++;
	if(!passed[i]){
	  dfs(i,connect,touch,lineCnt,tmp,false,i);
	  isMove=true;
	}
      }
    }
  }
  
  // これ以上動くことができないならば,
  if(!isMove&&prv!=-1){
    tmp.clear();
    if(EQV(lines[now][0],lines[prv][0])){
      tmp.push_back(lines[now][1]);
      tmp.push_back(lines[now][0]);
      tmp.push_back(lines[prv][1]);
    }
    else if(EQV(lines[now][1],lines[prv][0])){
      tmp.push_back(lines[now][0]);
      tmp.push_back(lines[now][1]);
      tmp.push_back(lines[prv][1]);
    }
    if(EQV(lines[now][0],lines[prv][1])){
      tmp.push_back(lines[now][1]);
      tmp.push_back(lines[now][0]);
      tmp.push_back(lines[prv][0]);
    }
    else if(EQV(lines[now][1],lines[prv][1])){
      tmp.push_back(lines[now][0]);
      tmp.push_back(lines[now][1]);
      tmp.push_back(lines[prv][0]);
    }
  }
}

int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++){
      int x1,x2,y1,y2;
      cin>>x1>>y1>>x2>>y2;
      lines[i][0]=P(x1,y1);
      lines[i][1]=P(x2,y2);
    }
    int ans[11];
    memset(ans,0,sizeof(ans));
    memset(passed,0,sizeof(passed));
    for(int i=0;i<n;i++){
      if(passed[i])continue;
      int connect=0;
      int touch=0;
      int lineCnt=0;
      vector<P> tmp;
      tp.clear();
      cp.clear();
      dfs(i,connect,touch,lineCnt,tmp,true,-1);
      connect=cp.size();
      touch=tp.size();
      cout<<lineCnt<<" "<<connect<<" "<<touch<<endl;
      if(lineCnt==1)ans[1]++;
      else if(lineCnt==3){
	if(connect==2)ans[7]++;
	else ans[4]++;
      }
      else if(lineCnt==4){
	if(connect==4)
	  ans[0]++;
	else if(connect==2&&touch==1)
	  ans[3]++;
	else if(connect==3)
	  ans[9]++;
      }
      else{
	if(connect==4&&touch==2)
	  ans[8]++;
	else if(connect==4&&touch==1)
	  ans[6]++;
	else{
	  if(cross(tmp[0]-tmp[1],tmp[2]-tmp[0])<0)
	    ans[5]++;
	  else
	    ans[2]++;
	}
      }
    }
    for(int i=0;i<10;i++){
      cout<<ans[i];
      if(i==9)cout<<endl;
      else cout<<" ";
    }
  }

  return 0;
}