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 DEBUG 0
#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);

	double SOnaiseki =  (taro.first - oni.first) * (syougai.first.first - oni.first) + (taro.second - oni.second) * (syougai.first.second - oni.second);
	double TOnaiseki =  (oni.first - taro.first) * (syougai.first.first - taro.first) + (oni.second - taro.second) * (syougai.first.second - taro.second);
	double SOcosSita = SOnaiseki / (ab * ac);
	double TOcosSita = TOnaiseki / (ab * bc);
	
	if(SOcosSita < 0 || TOcosSita < 0){
		return true;
	}
	//垂線部分の高さ成分
	double SOt = SOnaiseki / (ab * ab);//ac * (1 - cosSita);//(sita * 3.14 / 180.0));
	double TOt = TOnaiseki / (ab * ab);//ac * (1 - cosSita);//(sita * 3.14 / 180.0));
	double SOhc = ac - (SOt * ab);
	double TOhc = bc - (TOt * ab);
	double gaiseki = (syougai.first.first - oni.first) * (taro.second - oni.second) - (taro.first - oni.first) * (syougai.first.second - oni.second);
	double hc = gaiseki / ab;

#if DEBUG
	cout << "SOcosSita1 : " << SOcosSita << endl;
	cout << "TOcosSita2 : " << TOcosSita << endl;
	cout << "SOt : " << SOt << endl;
	cout << "TOt : " << TOt << endl;
	cout << "syougai.second : " << syougai.second << endl;
	cout << "SOhc : " << SOhc << endl;
	cout << "TOhc : " << TOhc << endl;
	cout << "gaiseki : " << gaiseki << endl;
	cout << "hc : " << hc << endl;
#endif
	if(hc < 0) hc *= -1;

	if(hc >= 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;
						}
					}
					*/
					danger = true;
				}
				//二人共円外
				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>
using namespace std;

#define N 100

#define rep(i, n) for(int i=0; i<n; ++i)
#define pow2(x) ((x)*(x))

int dist(int x1, int y1, int x2, int y2) {
	return pow2(x2-x1)+pow2(y2-y1);
}

#define S "Safe"
#define D "Danger"

int main() {
	int n, m;
	int wx[N], wy[N], r[N];
	bool safe;
	int tx, ty, sx, sy;
	while(cin >> n && n) {
		rep(i, n) {
			cin >> wx[i] >> wy[i] >> r[i];
			r[i] *= r[i];
		}
		cin >> m;
		rep(i, m) {
			cin >> tx >> ty >> sx >> sy;
			int dx = sx - tx, dy = sy - ty;
			safe = false;
			rep(j, n) {
				bool isin_t = dist(tx, ty, wx[j], wy[j])<=r[j];
				bool isin_s = dist(sx, sy, wx[j], wy[j])<=r[j];
				if( isin_t ^ isin_s ) {
					safe = true;
					break;
				}
				int t1 = -( dx*(tx-wx[j]) + dy*(ty-wy[j]) ), t0 = pow2(dx) + pow2(dy);
				if(!isin_s && !isin_t && 0<=t1 && t1<=t0 && pow2( dx*(ty-wy[j]) - dy*(tx-wx[j]) ) <= t0*r[j]) {
					safe = true;
					break;
				}
			}
			cout << (safe ? S : D) << endl;
		}
	}
	return 0;
}

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

using namespace std;

#define EPS 1e-8

class Point{
public:
  double x,y;
  Point(double x = 0.0,double y = 0.0) : x(x),y(y) {}
  Point operator + (Point p){ return Point(x+p.x, y+p.y); }
  Point operator - (Point p){ return Point(x-p.x, y-p.y); }
  Point operator * (double a){ return Point(x*a, y*a); }
  Point operator / (double a){ return Point(x/a, y/a); }
  double abs(){ return sqrt(norm()); }
  double norm(){ return x*x + y*y; }
};

typedef Point Vector;
double dot(Vector a,Vector b){
  return a.x*b.x + a.y*b.y;
}
double cross(Vector a,Vector b){
  return a.x*b.y - a.y*b.x;
}

class Segment{
public:
  Vector t,s;
  Segment(Vector t,Vector s) : t(t),s(s) {}
  Point projection(Point &p){
    Vector b = s - t;
    double d = dot(p-t,b) / b.norm();
    return t + b*d;
  }
};

class Circle{
public:
  double x,y,r;
  Circle(double x = 0.0,double y = 0.0,double r = 0.0) : x(x),y(y),r(r) {}
};

int N;
Point t,s;
Circle ci[300];

bool check(Point p){
  Vector a = s - t;
  Vector b = p - t;
  if(cross(a,b) > EPS) return false;
  if(cross(a,b) < -EPS) return false;
  if(dot(a,b) < -EPS) return false;
  if(a.norm() < b.norm()) return false;
  return true;
}

bool judge(){
  Segment seg = Segment(t,s);

  for(int i = 0 ; i < N ; i++){
    Circle c = ci[i]; Point cp = Point(c.x,c.y);
    Point p = seg.projection(cp);
    Point pp = p - cp;

    if(pp.abs()-EPS < c.r && check(p)){
      Vector a = t - cp;
      Vector b = s - cp;

      if((a.abs() > c.r && b.abs() < c.r) ||
         (a.abs() < c.r && b.abs() > c.r) ||
         (a.abs() > c.r && b.abs() > c.r)){
        return true;
      }
    }
  }
  return false;
}

int main(){
  int M;
  while(cin >> N,N){
    for(int i = 0 ; i < N ; i++){
      cin >> ci[i].x >> ci[i].y >> ci[i].r;
    }
    cin >> M;
    for(int i = 0 ; i < M ; i++){
      cin >> t.x >> t.y >> s.x >> s.y; 
      if(judge()){
        cout << "Safe" << endl;
      }else{
        cout << "Danger" << endl;
      }
    }
  }

  return 0;
}