Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	x := make([]int, 100)
	y := make([]int, 100)
	r := make([]int, 100)
	for scanner.Scan() {
		a, _ := strconv.Atoi(scanner.Text())
		if a == 0 {
			break
		}
		for i := 0; i < a; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmpx, _ := strconv.Atoi(input[0])
			tmpy, _ := strconv.Atoi(input[1])
			tmpr, _ := strconv.Atoi(input[2])
			x[i], y[i], r[i] = tmpx, tmpy, tmpr
		}
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(input[0])
			d, _ := strconv.Atoi(input[1])
			e, _ := strconv.Atoi(input[2])
			f, _ := strconv.Atoi(input[3])
			ok := false
			for j := 0; j < a; j++ {
				if (c-x[j])*(c-x[j])+(d-y[j])*(d-y[j]) >= r[j]*r[j] || (e-x[j])*(e-x[j])+(f-y[j])*(f-y[j]) >= r[j]*r[j] {
					if (e-c)*(x[j]-c)+(f-d)*(y[j]-d) < 0 {
						if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d) <= r[j]*r[j] {
							ok = true
						}
					} else {
						if (e-c)*(x[j]-c)+(f-d)*(y[j]-d)>(e-c)*(e-c)+(f-d)*(f-d) {
							if (x[j]-e)*(x[j]-e)+(y[j]-f)*(y[j]-f)<=r[j]*r[j] {
								ok = true
							}
						} else {
							if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d)-((e-c)*(x[j]-c)+(f-d)*(y[j]-d))*((e-c)*(x[j]-c)+(f-d)*(y[j]-d))/((e-c)*(e-c)+(f-d)*(f-d))<=r[j]*r[j] {
								ok = true
							}
						}
					}

				}
			}
			if ok {
				fmt.Println("Safe")
			} else {
				fmt.Println("Danger")
			}
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <math.h>

#define NITENKAN(a, b) sqrt(((a.first - b.first) * (a.first - b.first)) + ((a.second - b.second) * (a.second - b.second)))

using namespace std;

bool checkFound(pair<double, double> taro, pair<double, double> oni, pair<pair<double, double>, double> syougai){
	double ac = NITENKAN(syougai.first, oni);
	double ab = NITENKAN(taro, oni);
	double bc = NITENKAN(syougai.first, taro);

	//太郎、鬼間の線分と太郎円、鬼円間の線分を比較して後者が大きければその時点で障害にならないと判断
	if(ac >= ab || bc >= ab){
		return true;
	}

	double naiseki =  (taro.first - oni.first) * (syougai.first.first - oni.first) + (taro.second - oni.second) * (syougai.first.second - oni.second);
	double cosSita = naiseki / (ab * ac);
	//垂線部分の高さ成分
	double t = ac * (1 - cosSita);//(sita * 3.14 / 180.0));
	//値が合わず悔し紛れで調整
	t *= syougai.second;

	if(t > syougai.second){
		return true;
	}
	else{
		return false;
	}
}

int main(){
	int n = 0;

	while(cin >> n && n != 0){
		int m = 0;
		bool danger = false;
		vector<pair<pair<double, double>, double> > syougai(n);

		for(int i = 0; i < n; i++){
			cin >> syougai[i].first.first >> syougai[i].first.second >> syougai[i].second;
		}
		
		cin >> m;
		vector<pair<double, double> > taro(m);
		vector<pair<double, double> > oni(m);
		for(int j = 0; j < m; j++){
			//cinと比較、結果出力を書く
			cin >> taro[j].first >> taro[j].second >> oni[j].first >> oni[j].second;

			//太郎と鬼の現在位置における各障害物の影響を見る
			for(int l = 0; l < n; l++){
				//二人共円内
				if((NITENKAN(taro[j], syougai[l].first) < syougai[l].second) && (NITENKAN(oni[j], syougai[l].first) < syougai[l].second)){
					//円内に円がある可能性があるので他の障害物の影響を見る
					for(int a = l + 1; a < n; a++){
						danger = checkFound(taro[j], oni[j], syougai[a]);
						//障害物により安全とわかった段階で抜ける
						if(!danger){
							break;
						}
					}
				}
				//二人共円外
				else if((NITENKAN(taro[j], syougai[l].first) > syougai[l].second) &&  (NITENKAN(oni[j], syougai[l].first) > syougai[l].second)){
					danger = checkFound(taro[j], oni[j], syougai[l]);
				}
				else{
					danger = false;
				}
				if(!danger){
					break;		
				}
			}

			if(!danger){
				cout << "Safe" << endl;
			}
			else{
				cout << "Danger" << endl;
			}

		}
	}
}

C++: #include<iostream>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<string>

#define EPS 1e-10
#define PI 3.141592654

class Point{
	double x;
	double y;
public:
	Point( double a , double b ):x(a),y(b){}
	double X() const{ return x; }
	double Y() const{ return y; }
	double Norm() const{ return sqrt( pow( x , 2 ) + pow( y , 2 ) ); }
};

typedef Point Vector;

class Circle{
	Point o;
	double r;
public:
	Circle( double x , double y , double rad ):o(x,y),r(rad){}
	Point O(){ return o; }
	double R(){ return r; }
};

double getDistance(const Point& a, const Point& b)
{
	return sqrt( pow(a.X() - b.X() , 2) + pow(a.Y() - b.Y() , 2));
}

double getAreaOfTri( const Point& a , const Point& b , const Point& c )
{
	double ab = getDistance( a , b );
	double bc = getDistance( b , c );
	double ca = getDistance( c , a );
	double s = ( ab + bc + ca ) / 2.0;
	double Area = sqrt( s * (s - ab) * (s - bc) * (s - ca) );
	if(s - ab < EPS || s - bc < EPS || s - ca < EPS){
		Area = 0;
	}
	return Area;
}

double getDistancePointLine( const Point& o , const Point& a , const Point& b )
{
	double area = getAreaOfTri( o , a , b );
	double ab = getDistance( a , b );
	double h = 2 * area / ab;
	return h;
}

Point getMiddlePoint( const Point& a , const Point& b )
{
	double middleX = (a.X() + b.X()) / 2.0;
	double middleY = (a.Y() + b.Y()) / 2.0;
	Point middle( middleX , middleY );
	return middle;
}

double getDotProduct( const Vector& a , const Vector& b )
{
	return a.X() * b.X() + a.Y() * b.Y();
}

double getAngle( const Vector& a , const Vector& b )
{
	double cosAngleAB = getDotProduct( a , b ) / ( a.Norm() * b.Norm() );
	if( cosAngleAB < 0 ){
		cosAngleAB += EPS;
	}
	else if( cosAngleAB > 0 ){
		cosAngleAB -= EPS;
	}
	double angleAB = acos( cosAngleAB );
	return angleAB;
}

bool isWall( const Point& t , const Point& s , Circle wall )
{
	Vector ts( s.X() - t.X() , s.Y() - t.Y() );
	Vector to( wall.O().X() - t.X() , wall.O().Y() - t.Y() );
	Vector st( t.X() - s.X() , t.Y() - s.Y() );
	Vector so( wall.O().X() - s.X() , wall.O().Y() - s.Y() );

	double angleOTS = getAngle( ts , to );
	double angleOST = getAngle( st , so );

	// 壁になるときにtrueを返す。
	if( wall.R() > getDistance( t , wall.O() ) && wall.R() > getDistance( s , wall.O() ) ){
		return false;
	}
	else if( wall.R() < getDistance( t , wall.O() ) && wall.R() < getDistance( s , wall.O() ) ){
		if( !(angleOTS < PI/2  && angleOST < PI/2  && getDistancePointLine( wall.O() , t , s ) <= wall.R() + EPS ) ){
			return false;
		}
	}
	else{
		return true;
	}
}

int main(void)
{
	int n, m;
	std::vector<Circle> wall;
	while(1){
		std::cin >> n;
		if( n == 0 ){
			break;
		}
		for( int i = 0 ; i < n ; i++ ){
			int x, y, r;
			std::cin >> x >> y >> r;
			Circle cir( x , y , r );
			wall.push_back(cir);
		}
		std::cin >> m;
		for( int i = 0 ; i < 256*256 ; i++ ){
			int tx, ty, sx, sy;
			std::cin >> tx >> ty >> sx >> sy;
			Point t(tx, ty);
			Point s(sx, sy);
			int flag = 0;
			for( int j = 0 ; j < n ; j++ ){
				if( isWall( t , s , wall[j] ) ){
					flag = 1;
					break;
				}
			}
			std::cout << (flag == 1 ? "Safe":"Danger") << std::endl;
		}
	}

	return 0;
}

C++: //36
#include<iostream>
#include<cmath>
#include<complex>

using namespace std;

typedef complex<double> P;

double cr(P a,P b){
  return a.real()*b.imag()-a.imag()*b.real();
}

int main(){
  for(int n;cin>>n,n;){
    P w[100];
    double r[100];
    for(int i=0;i<n;i++){
      double x,y;
      cin>>x>>y>>r[i];
      w[i]=P(x,y);
    }
    int m;
    cin>>m;
    while(m--){
      P t,s,l;
      double tx,ty,sx,sy;
      cin>>tx>>ty>>sx>>sy;
      t=P(tx,ty);
      s=P(sx,sy);
      l=t-s;
      int i;
      for(i=0;i<n;i++){
	if((abs(w[i]-t)-r[i])*(abs(w[i]-s)-r[i])<0)break;
	if(abs(w[i]-t)>r[i]&&cr((t-s)*P(0,1),w[i]-s)*cr((s-t)*P(0,1),w[i]-t)>0&&abs(w[i]-s)*sin(fabs(arg(w[i]-s)-arg(l)))<=r[i]+1e-9)break;
      }
      cout<<((i<n)?"Safe":"Danger")<<endl;
    }
  }
  return 0;
}