728x90
로봇이 청소하는 작동하는 규칙이 있을때 청소하는 칸의 수를 구하는 문제입니다.
총 4가지 작동 규칙이 있는데 해당 규칙대로 움직이게 만들어주면 됩니다.
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n, m;
int y, x;
int nextY, nextX;
int ndy, ndx;
int d; // 북0, 동1, 남2, 서3
int[][] arr;
int []dy = {-1, 0, 1, 0};
int []dx = {0, 1, 0, -1};
int ans = 0;
boolean chk;
n = sc.nextInt();
m = sc.nextInt();
y = sc.nextInt();
x = sc.nextInt();
d = sc.nextInt();
arr = new int[n][m];
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
arr[i][j] = sc.nextInt();
}
}
while(true) {
chk = false;
arr[y][x] = 2;
++ans;
for(int i=1; i<=4; ++i){
nextY = y + dy[(4 + d - i) % 4];
nextX = x + dx[(4 + d - i) % 4];
if(arr[nextY][nextX] == 0){
y = nextY;
x = nextX;
d = (4 + d - i) % 4;
chk = true;
break;
}
}
if(chk) {
continue;
}
ndy = dy[(d + 2) % 4];
ndx = dx[(d + 2) % 4];
if(arr[y + ndy][x + ndx] == 1) {
break;
} else {
y += ndy;
x += ndx;
--ans;
}
}
System.out.printf("%d%n", ans);
}
}
'알고리즘' 카테고리의 다른 글
[Java] 백준 3085번 사탕 게임 (2) | 2023.01.18 |
---|---|
[JavaScript] 프로그래머스 - 이모티콘 할인행사 (0) | 2023.01.17 |
[Java] 백준 14888번 연산자 끼워넣기 (0) | 2023.01.17 |
[Java] SWEA - View (0) | 2023.01.16 |
[JavaScript] 프로그래머스 - 귤 고르기 (0) | 2023.01.16 |