[랜덤 마라톤] Farm robot(13871)

lhs's avatar
Dec 25, 2024
[랜덤 마라톤] Farm robot(13871)
notion image
notion image

1. 문제 풀이 아이디어

  • 로봇의 현재 위치를 계산하면서, 고장난 충전소의 위치에 도달할 때마다 결과값을 증가시키는 방식으로 문제를 해결한다.

2. 나의 정답 코드

public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine()); int n = Integer.parseInt(stringTokenizer.nextToken()); int c = Integer.parseInt(stringTokenizer.nextToken()); int s = Integer.parseInt(stringTokenizer.nextToken()); stringTokenizer = new StringTokenizer(bufferedReader.readLine()); int result = s == 1 ? 1 : 0; int rx = 1; for (int i = 0; i < c; i++) { int x = Integer.parseInt(stringTokenizer.nextToken()); rx = rx + x; if (rx > n) rx = 1; if (rx == 0) rx = n; if (s == rx) result++; } System.out.println(result); bufferedReader.close(); } }

3. 정리

  • 초기 결과값은 시작 위치가 고장난 충전소인 경우를 고려해 s가 1이라면 1, 아니면 0으로 설정한다.
  • rx는 로봇의 현재 위치를 나타내며, 명령마다 위치를 업데이트한다.
  • 경로가 초과하거나 0이 되는 경우를 각각 1 또는 n으로 조정한다.
  • 로봇이 고장난 충전소 s에 도달할 때마다 결과값을 증가시키는 방식으로 문제를 해결한다.
Share article

LHS's Study Space