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 <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <cassert>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

struct P{
    P(){}
    P(ll _r, ll _i):r(_r), i(_i){}
    void real(const ll& v){r = v;}
    void imag(const ll& v){i = v;}
    ll r, i;
};

ll real(const P& p){return p.r;}
ll imag(const P& p){return p.i;}
ll norm(const P& p){ll r = real(p), i = imag(p); return r * r + i * i;}

P operator+(const P& lhs, const P& rhs){
    return P(real(lhs)+real(rhs), imag(lhs)+imag(rhs));
}
P operator-(const P& lhs, const P& rhs){
    return P(real(lhs)-real(rhs), imag(lhs)-imag(rhs));
}
P operator-(const P& p){
    return P(-real(p), -imag(p));
}

ll cross(const P& lhs, const P& rhs){
    return real(lhs)*imag(rhs) - imag(lhs)*real(rhs);
}

ll dot(const P& lhs, const P& rhs){
    return real(lhs)*real(rhs) + imag(lhs)*imag(rhs);
}

ll distance(const P& a, const P& b, const P& p){
    ll c = cross(p-a, b-a);
    return c * c;
}

bool doesLineCrossCircle(const P& a, const P& b, const P& cc, const ll& radius){
    if(distance(a, b, cc) > radius * radius * norm(b-a)){return false;}
    if(norm(a-cc) > radius * radius && norm(b-cc) > radius * radius){
        return dot(cc-a, b-a) * dot(cc-b, b-a) <= 0;
    }
    return (norm(a-cc) < radius * radius) ^ (norm(b-cc) < radius * radius);
}

int N, M;
P wps[100100]; ll wrs[100100];

int main(){
    while(std::cin >> N, N){
        assert(N < 100100);
        REP(i, N){
            ll re, im;
            std::cin >> re >> im >> wrs[i];

            wps[i].real(re); wps[i].imag(im);
        }

        std::cin >> M;
    
        REP(i, M){
            P taro, oni;
            ll re, im;
            std::cin >> re >> im;
            taro.real(re); taro.imag(im);
            std::cin >> re >> im;
            oni.real(re); oni.imag(im);

            bool f = false;
            REP(j, N){
                // assert(norm(taro-wps[j]) != wrs[j] * wrs[j] &&
                //       norm(oni-wps[j]) != wrs[j] * wrs[j]);
                if(real(taro) == real(oni) &&
                   imag(taro) == imag(oni)){
                    continue;
                }
                if(doesLineCrossCircle(taro, oni, wps[j], wrs[j])){
                    f = true;
                }
            }

            if(f){
                puts("Safe");
            }else{
                puts("Danger");
            }
        }
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Wall { int x, y, r; };

bool inside(Wall w, double x, double y) {
	return w.r * w.r >= (w.x - x) * (w.x - x) + (w.y - y) * (w.y - y);
}

int main() {
	int n, wx, wy, r, m, tx, ty, sx, sy;
	while (cin >> n, n) {
		vector<Wall> w;
		for (int i=0; i<n; ++i) {
			cin >> wx >> wy >> r;
			w.push_back((Wall){wx, wy, r});
		}
		cin >> m;
		for (int i=0; i<m; ++i) {
			cin >> tx >> ty >> sx >> sy;
			bool safe = false;
			for (int j=0; j<n && !safe; ++j) {
				int k = inside(w[j], tx, ty) + inside(w[j], sx, sy);
				if (k == 0) {
					double dx, dy, x, y, minx, maxx, miny, maxy;
					dx = (sx - tx) / 10000.0;
					dy = (sy - ty) / 10000.0;
					x = tx;
					y = ty;
					minx = min(sx, tx);
					maxx = max(sx, tx);
					miny = min(sy, ty);
					maxy = max(sy, ty);
					while ( ! safe && minx <= x && x <= maxx && miny <= y && y <= maxy) {
						if (inside(w[j], x, y)) {
							safe = true;
							break;
						}
						x += dx;
						y += dy;
					}
				} else if (k == 1) {
					safe = true;
				}
			}
			cout << (safe ? "Safe" : "Danger") << endl;
		}
	}
	return 0;
}

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

using namespace std;

int main() {
 int i,j,n,m,f;
 int a,b,c,d,r,x1,x2,y1,y2,mx[101],my[101],mr[101];
 vector<int> tx,ty,sx,sy;
 while(cin >> n) {
	 if (n==0) break;
	 for (i=0;i<n;i++)  cin >> mx[i] >> my[i] >> mr[i];
	 cin >> m;
	 for (i=0;i<m;i++) { cin >> a >> b >> c >> r; tx.push_back(a); ty.push_back(b); sx.push_back(c); sy.push_back(r);}
	 for (i=0;i<m;i++) { 
		 for (j=0,f=0;j<n;j++) {
		  x1=tx[i]-mx[j]; y1=ty[i]-my[j]; x2=sx[i]-mx[j]; y2=sy[i]-my[j]; r=mr[j];
		  a=x1*x1+y1*y1; b=x2*x2+y2*y2;
		  if ( a<r*r && b<r*r) continue;
		  if ((a<r*r && b>r*r) || (a>r*r && b<r*r)) f=1;
		  a=x2-x1; b=y2-y1;
		  c=b*b*(r*r-x1*x1)+a*a*(r*r-y1*y1)+a*b*y1*x1;
		  d=(a*(-x1)+b*(-y1))*((-a)*(-x2)+(-b)*(-y2));
		  if (c>=0 && d>=0) f=1; 
		 }
		 if (f==1) cout << "Safe" << endl; else cout << "Danger" << endl;
	 }
	 tx.clear(); ty.clear(); sx.clear(); sy.clear();
 }
 return 0;
}

C++: #include <iostream>
#include <cmath>
#include <vector>
using namespace std;

double distpp(double x1, double y1, double x2, double y2){
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

double distpl(double xp, double yp,
              double x1, double y1,
              double x2, double y2){
    return abs((y1-y2)*(xp-x1)-(x1-x2)*(yp-y1))
           /distpp(x1, y1, x2, y2);
}

int main(){
    int n, m, i, j;
    while( cin >> n, n ){
        vector<double> wx(n),wy(n),r(n);
        for(i=0;i<n;i++){
            cin>>wx[i]>>wy[i]>>r[i];
        }
        cin >> m;
        double tx, ty, sx, sy;
        for(i=0;i<m;i++){
            cin>>tx>>ty>>sx>>sy;
            bool safe = false;
            for(j=0;j<n;j++){
                double d1 = distpp(wx[j],wy[j],tx,ty),
                       d2 = distpp(wx[j],wy[j],sx,sy);
                if( (d1-r[j])*(d2-r[j]) < 0 ){
                    safe = true; break;
                }
                if( d1 < r[j] ) continue;
                if( distpl(wx[j],wy[j],tx,ty,sx,sy) <= r[j] &&
                    ((sx-tx)*(sx-wx[j])+(sy-ty)*(sy-wy[j]))*
                    ((sx-tx)*(tx-wx[j])+(sy-ty)*(ty-wy[j])) < 0 ){
                    safe = true; break;
                }
            }
            if( safe ){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
    return 0;
}