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 <cmath>
using namespace std;

struct Circle{
	double x, y, r;
};

struct Segment{
	double x1, y1, x2, y2;
	double LengthSquared(){ return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); }
};

double CrossProduct2D(double x1, double y1, double x2, double y2){
	return x1 * y2 - x2 * y1;
}

double DotProduct2D(double x1, double y1, double x2, double y2){
	return x1 * x2 + y1 * y2;
}

bool isSegmentCircleIntersect(Circle c, Segment s){
	double r1 = (s.x1 - c.x) * (s.x1 - c.x) + (s.y1 - c.y) * (s.y1 - c.y) - c.r * c.r;
	double r2 = (s.x2 - c.x) * (s.x2 - c.x) + (s.y2 - c.y) * (s.y2 - c.y) - c.r * c.r;
	double d = CrossProduct2D(s.x2 - s.x1, s.y2 - s.y1, c.x - s.x1, c.y - s.y1) / sqrt(s.LengthSquared());
	if(r1 < 0 && r2 < 0)return false;
	if(d > c.r) return false;
	if(DotProduct2D(s.x2 - s.x1, s.y2 - s.y1, c.x - s.x1, c.y - s.y1) * DotProduct2D(s.x2 - s.x1, s.y2 - s.y1, c.x - s.x2, c.y - s.y2) <= 0) return true;
	if((c.x - s.x1) * (c.x - s.x1) + (c.y - s.y1) * (c.y - s.y1) <= c.r * c.r || (c.x - s.x2) * (c.x - s.x2) + (c.y - s.y2) * (c.y - s.y2) <= c.r * c.r) return true;
	return false;
}

int main(){
	int n, m, wx, wy, r, tx, ty, sx, sy, t;
	vector< Circle > circles;
	vector< Segment > segments;
	
	while(cin >> n, n){
		circles.resize(n);
		for(int i = 0; i < n; ++i){
			cin >> wx >> wy >> r;
			circles[i].x = wx;
			circles[i].y = wy;
			circles[i].r = r;
		}
		
		cin >> m;
		segments.resize(m);
		for(int i = 0; i < m; ++i){
			cin >> tx >> ty >> sx >> sy;
			segments[i].x1 = tx;
			segments[i].y1 = ty;
			segments[i].x2 = sx;
			segments[i].y2 = sy;
			t = 0;
			
			for(int j = 0; j < n; ++j){
				t += (isSegmentCircleIntersect(circles[j], segments[i]) ? 1 : 0);
			}
			
			cout << (t ? "Safe" : "Danger") << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
using namespace std;
#define EPS 1e-7
int n, m, wall[100][3], pp[4];
bool jud;
int main(){
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> wall[i][0] >> wall[i][1] >> wall[i][2];
	}
	cin >> m;
	for(int i = 0; i < m; i++){
		for(int j = 0; j < 4; j++){
			cin >> pp[j];
		}
		double dx = pp[0] - pp[2], dy = pp[1] - pp[3], kmk, spn;
		if(dx)
			kmk = dy / dx;
		else{
			for(int j = 0; j < n; j++){
				double a = 1, b = -2 * wall[j][1], c = (pp[0] - wall[j][0]) * (pp[0] - wall[j][0]) + wall[j][1] * wall[j][1] - wall[j][2] * wall[j][2];
				double D = b * b - 4 * a * c;
				if(D < -EPS){
					continue;
				}
				else if(D < 0){
					D = 0;
				}
				if(D >= 0){
					double aa = (sqrt(D) - b) / (2 * a);
					if(aa >= pp[1] && aa <= pp[3]){
						jud = 1;
						break;
					}
					if(aa >= pp[3] && aa <= pp[1]){
						jud = 1;
						break;
					}
					aa = (-sqrt(D) - b) / (2 * a);
				//	cout << aa << endl;
					if(aa >= pp[1] && aa <= pp[3]){
						jud = 1;
						break;
					}
					if(aa >= pp[3] && aa <= pp[1]){
						jud = 1;
						break;
					}
				} 
			}
			if(jud)
				cout << "Safe" << endl;
			else
				cout << "Danger" << endl;
			continue;
		}
		spn = -kmk * pp[0] + pp[1];
		jud = 0;
		for(int j = 0; j < n; j++){
			double a = 1 + kmk * kmk, b = -2 * wall[j][0] + 2 * kmk * spn - 2 * kmk * wall[j][1], c = wall[j][0] * wall[j][0] + wall[j][1] * wall[j][1] + spn * spn - 2 * spn * wall[j][1] - wall[j][2] * wall[j][2];
				double D = b * b - 4 * a * c;
				if(D < -EPS){
					continue;
				}
				else if(D < 0){
					D = 0;
				}
				if(D >= 0){
				double aa = (sqrt(D) - b) / (2 * a);
				if(aa >= pp[0] && aa <= pp[2]){
					jud = 1;
					break;
				}
				if(aa >= pp[2] && aa <= pp[0]){
					jud = 1;
						break;
				}
				aa = (-sqrt(D) - b) / (2 * a);
				if(aa >= pp[0] && aa <= pp[2]){
					jud = 1;
					break;
				}
				if(aa >= pp[2] && aa <= pp[0]){
					jud = 1;
					break;
				}
			}
		}
		if(jud)
			cout << "Safe" << endl;
		else
			cout << "Danger" << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<cmath>
#include<cstdlib>

using namespace std;

struct Point{int x,y;};
struct Circle{Point p;int r;};

/* _a,b©ç£ÌñæðÔ·Ö */
inline int DistanceSquared( Point a, Point b ){ return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); }

/* ¾YASA éÇÌîñ©çA»êçÌÊuÖWªSafe©Danger©²×éÖ */
bool IsSafePlace( Circle c, Point taro, Point dem ){
	double vx1, vy1, vx2, vy2, alpha1, alpha2;
	int seki = (taro.x-c.p.x)*(dem.x-taro.x) + (taro.y-c.p.y)*(dem.y-taro.y);
	int D = seki*seki - DistanceSquared(taro,dem)*(DistanceSquared(c.p,taro)-c.r*c.r);

	if( D < 0 )
		return false;

	alpha1 = (-seki-sqrt((double)D)) / DistanceSquared(taro, dem);
	alpha2 = (-seki+sqrt((double)D)) / DistanceSquared(taro, dem);

	vx1 = (taro.x - c.p.x) + alpha1 * ( dem.x - taro.x );
	vy1 = (taro.y - c.p.y) + alpha1 * ( dem.y - taro.y );
	vx2 = (taro.x - c.p.x) + alpha2 * ( dem.x - taro.x );
	vy2 = (taro.y - c.p.y) + alpha2 * ( dem.y - taro.y );

	if( taro.x == c.p.x && taro.y == c.p.y || dem.x == c.p.x && dem.y == c.p.y){
		if( DistanceSquared( taro, dem ) > c.r * c.r )
			return true;
		else
			return false;
	}else if(
		( (vx1*(taro.y-c.p.y) - vy1*(taro.x-c.p.x)) * (vx1*(dem.y-c.p.y) - vy1*(dem.x-c.p.x)) > 0 ) &&
		( (vx2*(taro.y-c.p.y) - vy2*(taro.x-c.p.x)) * (vx2*(dem.y-c.p.y) - vy2*(dem.x-c.p.x)) > 0 ) )
		return false;
	else
		return true;
}
int main(void){
	while(true){
		int n, m;
		vector<Circle> vWall;

		cin >> n;
		
		if( n == 0 )
			break;

		for(int i = 0; i < n; ++i){
			Circle tc;
			cin >> tc.p.x >> tc.p.y >> tc.r;
			vWall.push_back( tc );
		}

		cin >> m;

		for(int i = 0; i < m; ++i){
			bool bDanger = true;

			Point p_taro, p_daemon;
			cin >> p_taro.x >> p_taro.y >> p_daemon.x >> p_daemon.y;
			for( unsigned int j = 0; j < vWall.size() && bDanger; ++j ){
				if( IsSafePlace( vWall[j], p_taro, p_daemon ) ){
					bDanger = false;
					cout << "Safe\n";
				}
			}

			if( bDanger )
				cout << "Danger\n";
		}
	}
}