알고리즘

백준 5566번 주사위 게임 (JAVA)

박카스마시며코딩 2023. 4. 5. 22:58

https://www.acmicpc.net/problem/5566

 

5566번: 주사위 게임

상근이는 혼자 보드 게임을 하고 있다. 이 보드 게임의 보드는 N칸으로 이루어져 있고, 출발점은 1칸, 도착점은 N칸이다. 각 칸에는 지시 사항이 적혀있다. 지시 사항은 말을 얼만큼 이동해야 하

www.acmicpc.net

 

 

package BOJ.simulation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_5566 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = stoi.apply(st.nextToken());
        int m = stoi.apply(st.nextToken());
        int[] map = new int[n];
        for(int i = 0 ; i < n ; i++){
            map[i] = stoi.apply(br.readLine());
        }
        int now = 0;
        int result = 0;
        for(int i = 1 ; i <= m ; i++){
            int dice = stoi.apply(br.readLine());
            now += dice;
            if(now >= n-1){
                result = i;
                break;
            }
            now += map[now];
            if(now < 0){
                now = 0;
            }
            if(now >= n-1){
                result = i;
                break;
            }
        }
        System.out.println(result);
    }
}