https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$ 둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽
www.acmicpc.net
📌 - 로봇 청소기
- DFS, BFS 문제로 풀어야 하는지 고민했는데, 단순 구현으로 풀어도 시간복잡도가 초과되진 않아서 단순 구현으로 품
- 이동, 방향전환 함수를 따로 만들어서 로봇 청소기의 움직임을 조금 더 보기쉽도록 구현함
구현문제는 주어진대로 구현만 하면 되는데, 로봇 청소기는 문제를 이해하는데 생각보다 조금 오래 걸렸던 것 같다.
현재 위치 sX,sY를 만들고, 현재 보는 방향 location변수를 만들어서 관리를 하고 조건에 맞게 청소기를 움직임
👉 - java 코드
import java.io.*;
import java.util.*;
public class Main {
static int N,M,result,sX,sY,location;
static boolean check;
static int[][] arr;
static int[] dx = {-1,0,1,0};
static int[] dy = {0,1,0,-1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
result = 0;
check = true;
st = new StringTokenizer(br.readLine());
sX = Integer.parseInt(st.nextToken());
sY = Integer.parseInt(st.nextToken());
location = Integer.parseInt(st.nextToken());
arr = new int[N][M];
for (int i =0 ;i<N;i++){
st = new StringTokenizer(br.readLine());
for (int j = 0; j<M;j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
while (check){
if(arr[sX][sY] == 0){
arr[sX][sY] = 2;
result ++;
}
int test=0;
for ( int i=0;i<4;i++){
int nx = sX+dx[i];
int ny = sY+dy[i];
if (0<=nx && nx < N && 0<=ny && ny<M && arr[nx][ny] ==0){
test++;
}
}
if (test >0){
rotate(location);
if (0 <= sX + (dx[location]) && sX + (dx[location]) < N && 0 <= sY + (dy[location]) && sY + (dy[location]) < M && arr[sX+dx[location]][sY+dy[location]] == 0) {
move(sX + dx[location], sY + dy[location]);
}
}else{
if (0 <= sX + (dx[location] * -1) && sX + (dx[location] * -1) < N && 0 <= sY + (dy[location] * -1) && sY + (dy[location] * -1) < M && arr[sX+(dx[location])*-1][sY+(dy[location]*-1)] != 1) {
move(sX + dx[location] * -1, sY +dy[location] * -1);
} else {
System.out.println(result);
return;
}
}
}
}
public static void move(int x, int y){
sX = x;
sY = y;
}
public static void rotate(int num) {
switch (num) {
case 0:
location = 3;
break;
case 1:
location = 0;
break;
case 2:
location = 1;
break;
case 3:
location = 2;
break;
}
}
}