728x90
2023.08.09 - [Coding Test/programmers] - [프로그래머스] 교점에 별 만들기
import java.util.*;
class Solution {
public static class Point {
public final long x,y;
private Point(long x, long y) {
this.x = x;
this.y = y;
}
}
// 교점구하기
private Point intersection(long a1, long b1, long c1, long a2, long b2, long c2) {
double x = (double) (b1*c2-c1*b2)/(a1*b2-b1*a2);
double y = (double) (c1*a2-a1*c2)/(a1*b2-b1*a2);
if (x%1!=0 || y%1!=0) return null;
return new Point((long)x,(long)y);
}
// 최대좌표찾기
private Point getMax(List<Point> points){
long x = Long.MIN_VALUE;
long y = Long.MIN_VALUE;
for (Point p: points){
if (p.x>x) x = p.x;
if (p.y>y) y = p.y;
}
return new Point(x,y);
}
// 최소좌표찾기
private Point getMin(List<Point> points){
long x = Long.MAX_VALUE;
long y = Long.MAX_VALUE;
for (Point p: points){
if (p.x<x) x = p.x;
if (p.y<y) y = p.y;
}
return new Point(x,y);
}
public String[] solution(int[][] line) {
List<Point> points = new ArrayList<>();
for (int i=0; i<line.length; i++) {
for (int j=0; j<line.length; j++) {
Point temp = intersection(line[i][0], line[i][1], line[i][2],
line[j][0], line[j][1], line[j][2]); // 교점 구하기
if (temp!=null){
points.add(temp);
}
}
}
// 최대최소 좌표 구하기
Point maxpoint = getMax(points);
Point minpoint = getMin(points);
// 가로세로 길이
int w = (int)(maxpoint.x - minpoint.x +1);
int h = (int)(maxpoint.y - minpoint.y +1);
System.out.println(w+", "+h);
char[][] arr = new char[h][w];
for (char[] row: arr) {
Arrays.fill(row, '.');
}
// 좌표 -> 2차원배열
for (Point p: points) {
int x = (int)(p.x - minpoint.x); // 최소좌표를 0점
int y = (int)(maxpoint.y - p.y); // 최대좌표와 뒤집기 (y축반대)
arr[y][x] = '*';
}
String[] answer = new String[arr.length];
for (int i=0; i<answer.length; i++) {
answer[i] = new String(arr[i]);
}
return answer;
}
}
728x90