BOJ::1004번 어린왕자

https://www.acmicpc.net/problem/1004



풀이 : 


시작점과 끝점이 몇개의 원에 둘러쌓여 있는지 찾고 cnt++ 해주면 된다.

조심해야 할것은 시작점과 끝점 둘다 한 원 안에 있는 경우 카운트가 변하지 않는다.


C++ 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <math.h>
using namespace std;
 
int t, sx, sy, ex, ey;
 
//(sx,sy)가 (x,y,r)에 둘러 쌓여 있다면 true, 아니면 false
bool checkPoint(int x,int sx,int y,int sy,int r) {
    if (pow((x - sx), 2+ pow((y - sy), 2< pow(r, 2)) {
        return true;
    }
    return false;
}
 
int main() {
    cin >> t;
 
    for (int k = 0; k < t; k++) {
        cin >> sx >> sy >> ex >> ey;
        int n,x,y,r,cnt=0;
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> x >> y >> r;
            if (checkPoint(x, sx, y, sy, r) && checkPoint(x, ex, y, ey, r)) {
                continue;
            }
            else if (checkPoint(x, sx, y, sy, r)) {
                cnt++;
            }
            else if (checkPoint(x, ex, y, ey, r)) {
                cnt++;
            }
        }
 
        cout << cnt << endl;
    }
 
}
cs





JAVA 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
class Main{
    
    static int t,n,sx,sy,ex,ey,x,y,r;
    
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        t=Integer.parseInt(br.readLine());
        
        for(int k = 0 ; k < t ; k++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            sx = Integer.parseInt(st.nextToken());
            sy = Integer.parseInt(st.nextToken());
            ex = Integer.parseInt(st.nextToken());
            ey = Integer.parseInt(st.nextToken());
            n=Integer.parseInt(br.readLine());
            int cnt=0;
            for(int i = 0 ; i < n ; i++){
                st = new StringTokenizer(br.readLine());
                x=Integer.parseInt(st.nextToken());
                y=Integer.parseInt(st.nextToken());
                r=Integer.parseInt(st.nextToken());
                if(checkPoint(x,sx,y,sy,r) && checkPoint(x,ex,y,ey,r)){
                    continue;
                }
                else if(checkPoint(x,sx,y,sy,r)){
                    cnt++;
                }
                else if(checkPoint(x,ex,y,ey,r)){
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
    }
    
    public static boolean checkPoint(int x,int sx,int y,int sy,int r){
        if(Math.pow((x-sx), 2+ Math.pow((y-sy), 2< Math.pow(r, 2)){
            return true;
        }
        return false;
    }
}
cs


'BOJ::문제풀이' 카테고리의 다른 글

1149 RGB거리  (0) 2017.12.30
1065 한수  (0) 2017.12.30
1049 기타줄  (0) 2017.12.30
1012 유기농배추  (0) 2017.12.30
1009 분산처리  (0) 2017.12.30

+ Recent posts