Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 2528번 사다리 (gold 3 본문
import java.io.*;
import java.util.*;
class Point{
int s;
int e;
int direction;
public Point(int s ,int e,int direction){
this.s=s;
this.e=e;
this.direction=direction;
}
}
public class Main{
static ArrayList<Point> arr=new ArrayList<>();
static int l;
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.valueOf(st.nextToken());
l=Integer.valueOf(st.nextToken());
for(int i=0; i<n; i++){
st=new StringTokenizer(br.readLine());
int length=Integer.valueOf(st.nextToken());
int direction=Integer.valueOf(st.nextToken());
if(direction==1){
arr.add(new Point(l-length , l, 1));
}
else{
arr.add(new Point(0,length,0));
}
}
//전체적으로, 이동이 가능한 경우는 층 수를 올리고,
//불가한 경우는 층의 발판들을 이동하고 시간을 ++
//이후 다시 위의 매커니즘을 반복하면 답을 구할 수 있다.
//사다리를 올릴 수 있는 영역, 즉 위와, 아래층이 서로의 영역안에 포함되는 경우를
//잘 분기하여 판단하는 게 중요한 문제. 이를 중점으로 구현하면 된다.
//0층에서 시작. n-1일 경우 종료
int now=0;
int answer=0;
while(true){
if(now==n-1){
break;
}
//올라갈 수 있는 경우
if(possible(now)){
now++; //한 층올라가기
continue;
}
//불가한 경우.
else{
answer++; //시간 1초 더함.
move(now); //현재층 부터 그 위로 이동하기.
}
}
System.out.println(answer);
}
//1초 지나면서 이동하기.
public static void move(int floor){
for(int i=floor; i<arr.size(); i++){
Point temp=arr.get(i);
if(temp.direction==0){
if(temp.e==l){
temp.direction=1;
}
temp.s+=1;
temp.e+=1;
}
else{
if(temp.s==0){
temp.direction=0;
}
temp.s-=1;
temp.e-=1;
}
}
}
//사다리 타고 올라갈 수 있는지.
public static boolean possible(int now){
Point down=arr.get(now);
Point up=arr.get(now+1);
//위층이 아래층 영역을 포함하는 경우
//아래층이 위층 영역을 포함하는 경우.
if((up.s<=down.s && down.e<=up.e) || (down.s<=up.s && up.e<=down.e) ){
return true;
}
//양끝에서 점이 겹치거나 일부 포함.
else if((up.s<=down.e && down.e<=up.e) || down.s<=up.e && up.e<=down.e){
return true;
}
//위와 동일
else if((up.s<=down.s && down.s<=up.e) || (up.s<=down.e && down.e<=up.e)){
return true;
}
else{
return false;
}
}
}
'백준공부 > java' 카테고리의 다른 글
[백준] 2042번 구간 합 구하기 (gold 1 (0) | 2024.05.27 |
---|---|
[백준] 1234번 크리스마스 트리 (gold 2 (0) | 2024.05.23 |
[백준] 15684번 사다리 조작 (gold 3 (0) | 2024.05.22 |
[백준] 20366번 같이 눈사람 만들래? (gold 3 (0) | 2024.05.21 |
[백준] 14431번 소수마을 (gold 3 (0) | 2024.05.21 |