[실버2] 2477. 참외밭 (Java)
https://www.acmicpc.net/problem/2477
2477번: 참외밭
첫 번째 줄에 1m2의 넓이에 자라는 참외의 개수를 나타내는 양의 정수 K (1 ≤ K ≤ 20)가 주어진다. 참외밭을 나타내는 육각형의 임의의 한 꼭짓점에서 출발하여 반시계방향으로 둘레를 돌면서 지
www.acmicpc.net
화이팅 ★★★★★
※ 문제를 요약하면 아래와 같습니다.
▶ 참외밭의 모양이 ㄱ, ┏, ┗, ┛ 모양 의 육각형으로 존재
▶ 육각형의 각 변이 주어졌을 때, 넓이를 구하고 1m^2당 참외 갯수를 곱하여 총 참외갯수 구하기
ex) 아래 그림은 면적이 6800m^2가 나오고 참외의 갯수가 7개라면 총 참외갯수는 7 * 6800 = 47600
▶ 1, 2, 3, 4 라는 값을 각각 동,서,남,북의 방향으로 표현
ex) 1, 50이면 현재 좌표에서 동쪽으로 50만큼 이동하여 변이 그려짐
▶ 여자는 주어진 스위치의 번호를 기준으로 양 옆의 스위치 상태가 다를 때 까지 스위치들을 반대로 변경
▶ Input : 첫째줄에 참외의 갯수
둘째줄 부터 방향과 변의 길이가 6번 주어짐 (육각형)
▶ Output : 참외의 총 갯수
◈ Input
7
4 50
2 160
3 30
1 60
3 20
1 100
◈ Output
47600
◎ 코드 작성 전, 아래와 같이 솔루션을 정리하였습니다.
▶ 빈공간이 없다고 가정, 가장 큰 가로 길이와 가장 큰 세로길이를 곱하여 직사각형의 넓이 구하기
▶ 직사각형의 넓이 - 빈공간의 넓이 == 참외밭의 넓이
▶ 빈공간의 가로 인덱스와 세로 인덱스 == 직사각형의 가로 인덱스, 세로 인덱스와 2만큼 차이남.
ex) 직사각형 가로 인덱스 == 1, 빈공간 가로 인덱스 == 3 ( 1+2 == 3)
직사각형 세로 인덱스 == 0, 빈공간 가로 인덱스 == 4 ( 0-2 == 4 (-2 + 6) )
▶ 만약 가장 큰 가로와 가장 큰 세로가 각각 시작 점이면 인덱스 방향을 반대로 구함
▶ 위의 방법으로 빈공간의 가로와 세로를 구한 뒤, 곱하여 넓이를 구함
▶ 직사각형의 넓이 - 빈공간의 넓이 구현
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int nFruit = Integer.parseInt(bReader.readLine());
int[] narrWay = new int[6];
int[] narrLen = new int[6];
// 입력 받구~
for(int i = 0; i < 6; i++) {
StringTokenizer st = new StringTokenizer(bReader.readLine());
int nWay = Integer.parseInt(st.nextToken());
int nLength = Integer.parseInt(st.nextToken());
narrWay[i] = nWay;
narrLen[i] = nLength;
}
int nHigherWidth = 0;
int nHigherHeight = 0;
int nWidthIndex = 0;
int nHeightIndex = 0;
for(int i = 0 ; i < 6; i++) {
// 가로일 때
if(narrWay[i] == 1 || narrWay[i] == 2) {
if(nHigherWidth < narrLen[i]) {
nHigherWidth = narrLen[i];
nWidthIndex = i;
}
}
// 세로 일 때
if(narrWay[i] == 3 || narrWay[i] == 4) {
if(nHigherHeight < narrLen[i]) {
nHigherHeight = narrLen[i];
nHeightIndex = i;
}
}
}
// 연속으로 붙어있지 않으면 ?
boolean bWay = nWidthIndex < nHeightIndex;
// 둘의 인덱스가 0과 5일 경우 방향 반대
if(Math.abs(nWidthIndex - nHeightIndex) > 1)
bWay = !bWay;
if(bWay) {
nWidthIndex-=2;
nHeightIndex+=2;
if(nWidthIndex < 0)
nWidthIndex += 6;
if(nHeightIndex >= 6)
nHeightIndex -= 6;
} else {
nWidthIndex+=2;
nHeightIndex-=2;
if(nHeightIndex < 0)
nHeightIndex += 6;
if(nWidthIndex >= 6)
nWidthIndex -= 6;
}
int nSmallArea = narrLen[nHeightIndex] * narrLen[nWidthIndex];
int nFruitArea = nHigherWidth * nHigherHeight - nSmallArea;
int nResult = nFruitArea * nFruit;
bWriter.write(String.valueOf(nResult));
bWriter.flush();
bWriter.close();
}
}
읽어주셔서 감사합니다!
Good Luck! (피드백 감사합니다!)