Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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
Archives
Today
Total
관리 메뉴

csct3434

[level 2] 교점에 별 만들기 - 87377 본문

프로그래머스

[level 2] 교점에 별 만들기 - 87377

csct3434 2024. 3. 7. 06:33

문제 링크

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

class Solution {

    public String[] solution(int[][] line) {
        Set<Point> points = getPoints(line);
        return draw(points);
    }

    private Set<Point> getPoints(int[][] line) {
        HashSet<Point> points = new HashSet<>();

        for (int i = 0; i < line.length; i++) {
            double A = line[i][0];
            double B = line[i][1];
            double E = line[i][2];

            // y = -A/Bx + -C/B
            for (int j = i + 1; j < line.length; j++) {
                double C = line[j][0];
                double D = line[j][1];
                double F = line[j][2];

                if (A * D - B * C == 0) {
                    continue;
                }

                double x = (B * F - E * D) / (A * D - B * C);
                double y = (E * C - A * F) / (A * D - B * C);

                if (x % 1 == 0 && y % 1 == 0) {
                    points.add(new Point((long) x, (long) y));
                }
            }
        }

        return points;
    }

    private String[] draw(Set<Point> points) {
        long minX = Long.MAX_VALUE;
        long maxX = Long.MIN_VALUE;
        long minY = Long.MAX_VALUE;
        long maxY = Long.MIN_VALUE;

        for (Point point : points) {
            minX = Math.min(minX, point.x);
            maxX = Math.max(maxX, point.x);
            minY = Math.min(minY, point.y);
            maxY = Math.max(maxY, point.y);
        }

        char[][] map = new char[(int) (maxY - minY + 1)][(int) (maxX - minX + 1)];
        for (char[] line : map) {
            Arrays.fill(line, '.');
        }

        for (Point point : points) {
            map[(int) (maxY - point.y)][(int) (point.x - minX)] = '*';
        }

        String[] result = new String[(int) (maxY - minY + 1)];
        for (int i = 0; i < map.length; i++) {
            result[i] = new String(map[i]);
        }
        return result;
    }

    private static class Point {

        long x, y;

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

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Point point = (Point) o;
            return x == point.x && y == point.y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }
}

1. 교점 공식 (Ax + By + E = 0, Cx + Dy + F = 0)

 

2. 이상하게 틀리는 것 같다면, 자료형의 범위가 문제일 수 있다.