Skip to content

Commit

Permalink
Add Post: [백준] 20055번_컨테이어 벨트 위의 로봇(Java)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaerim1001 committed Apr 7, 2024
1 parent a51d94e commit 074ab2c
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: "[백준] 20055번_컨베이어 벨트 위의 로봇(Java)"
date: 2024-04-07
categories: [Algorithm, 문제풀이]

tags: [Algorithm, CodingTest]
math: true
---

### 문제 풀이
백준의 20055번 문제입니다.
[문제 출처](https://www.acmicpc.net/problem/20055)

### (1) 문제 파악

![1](/assets/img/posts/2024-04-07/q2.png){: width="1000"}

### (2) 문제 풀이 방법 생각하기
구현 문제이므로 문제에 나온 순서대로 차근히 구현해주면 됩니다!

#### [1] 로봇과 함께 회전하기

```java
int temp = belt[2*N-1];

for(int i=2*N-1; i>0; i--) {
belt[i] = belt[i-1];
}
belt[0] = temp;

for(int i=N-1; i>0; i--) {
robot[i] = robot[i-1];
}
robot[0] = false;
```

#### [2] 로봇 이동 시키기

```java
if(robot[N-1]) robot[N-1] = false; // N번에 있는 로봇은 내리는 자리에 있으므로 내려줍니다.

for(int i=N-2; i>=0; i--) {
if(robot[i]) {
if(!robot[i+1] && belt[i+1]>0) {
robot[i] = false;
robot[i+1] = true;
belt[i+1] -= 1;
if(belt[i+1] == 0) count++;
}
}
}
```

#### [3] 로봇 올리기

```java
if(belt[0] > 0) {
belt[0] -= 1;
if(belt[0] == 0) count++;
robot[0] = true;
}
```

### (3) 구현

```java
import java.io.*;
import java.util.*;

public class Main {
static int N, K;
static int answer = 0;
static int[] belt;
static boolean[] robot;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
belt = new int[2*N];
robot = new boolean[N];

st = new StringTokenizer(br.readLine());
for(int i=0; i<2*N; i++) {
belt[i] = Integer.parseInt(st.nextToken());
}

simulation();

System.out.println(answer);
br.close();
}

private static void simulation() {
int count = 0;
while(count<K) {
answer++;
// 1. 로봇과 함께 회전하기
int temp = belt[2*N-1];
for(int i=2*N-1; i>0; i--) {
belt[i] = belt[i-1];
}
belt[0] = temp;

for(int i=N-1; i>0; i--) {
robot[i] = robot[i-1];
}
robot[0] = false;

// 2. 로봇 옮기기
if(robot[N-1]) robot[N-1] = false;
for(int i=N-2; i>=0; i--) {
if(robot[i]) {
if(!robot[i+1] && belt[i+1]>0) {
robot[i] = false;
robot[i+1] = true;
belt[i+1] -= 1;
if(belt[i+1] == 0) count++;
}
}
}

// 3. 로봇 올리기
if(belt[0] > 0) {
belt[0] -= 1;
if(belt[0] == 0) count++;
robot[0] = true;
}
}
}
}
```
Binary file added assets/img/posts/2024-04-07/q2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 074ab2c

Please sign in to comment.