[백준]1198,1260 자바

allnight5·2023년 8월 3일
0

백준

목록 보기
5/7

1198번 문제링크
1260번 문제링크

1198번 삼각형으로 자르기

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Main
{
	public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine()); 
        Point[] points = new Point[n];
        StringTokenizer st;
        for (int i = 0; i < n; i++) {
		    st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            points[i] = new Point(x, y);
        }
        br.close();
        double result = getMaxTriangleArea(points);
        System.out.println(result);
	}
	
    public static double getMaxTriangleArea(Point[] points) {
        int n = points.length;
        double maxArea = 0;
    
        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int k = j + 1; k < n; k++) {
                    double area = getTriangleArea(points[i], points[j], points[k]);
                    maxArea = Math.max(maxArea, area);
                }
            }
        }
    
        return maxArea;
    }

    public static double getTriangleArea(Point p1, Point p2, Point p3) {
        return Math.abs(0.5 * ((p1.x * p2.y + p2.x * p3.y + p3.x * p1.y) 
        - (p2.x * p1.y + p3.x * p2.y + p1.x * p3.y)));
    }

}

class Point {
    int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

1260번 DFS와 BFS

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Queue;
import java.util.LinkedList;

public class Main
{
    public static boolean[] visitDfs;  
	public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken()); 
        int m = Integer.parseInt(st.nextToken());
        int start = Integer.parseInt(st.nextToken());
		visitDfs = new boolean[n];
		
		Point[] point = new Point[n];
		

        for (int i = 0; i < n; i++) {
            point[i] = new Point(i); 
        } 
		
        for (int i = 0; i < m; i++) {
		    st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            point[x-1].list.add(y-1);
            point[y-1].list.add(x-1); 
        }
        br.close();  
        
        dfs(point, start-1);
        System.out.println("");
        bfs(point, start-1, n);
	}
	
	public static void dfs(Point[] point, int current){
        if (visitDfs[current]) {
            return;
        }
    
        visitDfs[current] = true;
        System.out.print(current+1 + " ");
    
        List<Integer> list = point[current].list;
        Collections.sort(list);
        for (int next : list) { 
            dfs(point, next);
        }
    }

	 
	public static void bfs(Point[] point, int start, int n){
	    boolean[] visitBfs = new boolean[n];  
        Queue<Integer> queue = new LinkedList<>(); 
        queue.add(start);
        visitBfs[start] = true;
        
        while (!queue.isEmpty()) {
            int current = queue.poll();
            System.out.print(current + 1 + " ");

            List<Integer> list = point[current].list;
            for (int next : list) {
                if (!visitBfs[next]) {
                    queue.add(next);
                    visitBfs[next] = true;
                }
            }
        }
	}
}

class Point{
    List<Integer> list = new ArrayList<>();
    
    public Point(int number){
        list.add(number);
    }
}
 
profile
공부기록하기

1개의 댓글

comment-user-thumbnail
2023년 8월 3일

이렇게 유용한 정보를 공유해주셔서 감사합니다.

답글 달기